├── .gitattributes ├── .gitignore ├── .mailmap ├── Jenkinsfile ├── README.md ├── cmpfiles ├── chapter01 │ ├── cmp_hello_world.pdf │ ├── cmp_quick_brown_fox.pdf │ ├── cmp_rick_astley.pdf │ └── cmp_united_states.pdf ├── chapter02 │ ├── cmp_axes.pdf │ ├── cmp_grid_lines.pdf │ ├── cmp_star_wars.pdf │ └── cmp_star_wars_crawl.pdf ├── chapter03 │ ├── cmp_new_york_times.pdf │ ├── cmp_premier_league.pdf │ └── cmp_ufo.pdf ├── chapter04 │ ├── cmp_create_and_fill.pdf │ ├── cmp_fill_form.pdf │ ├── cmp_flatten_form.pdf │ ├── cmp_job_application.pdf │ ├── cmp_line_annotation.pdf │ ├── cmp_link_annotation.pdf │ ├── cmp_text_annotation.pdf │ └── cmp_textmarkup_annotation.pdf ├── chapter05 │ ├── cmp_add_content.pdf │ ├── cmp_change_page.pdf │ ├── cmp_edited_job_application.pdf │ └── cmp_filled_out_job_application.pdf ├── chapter06 │ ├── cmp_88th_oscar_combined_documents.pdf │ ├── cmp_88th_oscar_combined_documents_xy_pages.pdf │ ├── cmp_88th_oscar_the_revenant_nominations_TOC.pdf │ ├── cmp_combined_forms.pdf │ ├── cmp_fill_out_and_merge_forms.pdf │ ├── cmp_fill_out_flatten_forms_merge.pdf │ ├── cmp_fill_out_flatten_forms_smart_merge.pdf │ ├── cmp_the_golden_gate_bridge_nup.pdf │ ├── cmp_the_golden_gate_bridge_scale_shrink.pdf │ └── cmp_the_golden_gate_bridge_tiles.pdf └── chapter07 │ ├── cmp_merged_PDFA-1a_documents.pdf │ ├── cmp_quick_brown_fox_PDFA-1a.pdf │ ├── cmp_quick_brown_fox_PDFA-1b.pdf │ ├── cmp_quick_brown_fox_PDFUA.pdf │ └── cmp_united_states_PDFA-3a.pdf ├── findbugs-filter.xml ├── pom.xml └── src ├── main ├── java │ └── tutorial │ │ ├── chapter01 │ │ ├── C01E01_HelloWorld.java │ │ ├── C01E02_RickAstley.java │ │ ├── C01E03_QuickBrownFox.java │ │ └── C01E04_UnitedStates.java │ │ ├── chapter02 │ │ ├── C02E01_Axes.java │ │ ├── C02E02_GridLines.java │ │ ├── C02E03_StarWars.java │ │ └── C02E04_StarWarsCrawl.java │ │ ├── chapter03 │ │ ├── C03E01_NewYorkTimes.java │ │ ├── C03E02_PremierLeague.java │ │ └── C03E03_UFO.java │ │ ├── chapter04 │ │ ├── C04E01_01_TextAnnotation.java │ │ ├── C04E01_02_LinkAnnotation.java │ │ ├── C04E01_03_LineAnnotation.java │ │ ├── C04E01_04_TextMarkupAnnotation.java │ │ ├── C04E02_JobApplication.java │ │ ├── C04E03_CreateAndFill.java │ │ ├── C04E04_FillForm.java │ │ └── C04E05_FlattenForm.java │ │ ├── chapter05 │ │ ├── C05E01_AddAnnotationsAndContent.java │ │ ├── C05E02_FillAndModifyForm.java │ │ ├── C05E03_AddContent.java │ │ └── C05E04_ChangePage.java │ │ ├── chapter06 │ │ ├── C06E01_TheGoldenGateBridge_Scale_Shrink.java │ │ ├── C06E02_TheGoldenGateBridge_Tiles.java │ │ ├── C06E03_TheGoldenGateBridge_N_up.java │ │ ├── C06E04_88th_Oscar_Combine.java │ │ ├── C06E05_88th_Oscar_CombineXofY.java │ │ ├── C06E06_88th_Oscar_Combine_AddTOC.java │ │ ├── C06E07_Combine_Forms.java │ │ ├── C06E08_FillOutAndMergeForms.java │ │ └── C06E09_FillOutFlattenAndMergeForms.java │ │ └── chapter07 │ │ ├── C07E01_QuickBrownFox_PDFUA.java │ │ ├── C07E02_QuickBrownFox_PDFA_1a.java │ │ ├── C07E02_QuickBrownFox_PDFA_1b.java │ │ ├── C07E03_UnitedStates_PDFA_3a.java │ │ └── C07E04_MergePDFADocuments.java └── resources │ ├── color │ └── sRGB_CS_profile.icm │ ├── data │ ├── ny_times_apple.txt │ ├── ny_times_fb.txt │ ├── ny_times_inst.txt │ ├── premier_league.csv │ ├── ufo.csv │ └── united_states.csv │ ├── font │ ├── FreeSans.ttf │ └── FreeSansBold.ttf │ ├── img │ ├── car.jpg │ ├── dog.bmp │ ├── fox.bmp │ ├── ny_times_apple.jpg │ ├── ny_times_fb.jpg │ ├── ny_times_inst.jpg │ └── senator.jpg │ └── pdf │ ├── 88th_noms_announcement.pdf │ ├── 88th_reminder_list.pdf │ ├── job_application.pdf │ ├── oscars_movies_checklist_2016.pdf │ ├── quick_brown_fox_PDFA-1a.pdf │ ├── state.pdf │ ├── subscribe.pdf │ ├── the_golden_gate_bridge.pdf │ ├── ufo.pdf │ └── united_states_PDFA-1a.pdf └── test └── java └── tutorial ├── C06E09_FillOutFlattenAndMergeFormsWrapperTest.java └── JumpStartWrapperTest.java /.gitattributes: -------------------------------------------------------------------------------- 1 | # Set the default behavior, in case people don't have core.autocrlf set. 2 | * text=auto 3 | 4 | # Explicitly declare text files you want to always be normalized and converted 5 | # to LF line endings on checkout. 6 | *.afm text eol=lf 7 | *.cmap text eol=lf 8 | *.crt text eol=lf 9 | *.cs text eol=lf 10 | *.html text eol=lf 11 | *.java text eol=lf ident 12 | *.lng text eol=lf 13 | *.md text eol=lf 14 | *.pom text eol=lf 15 | *.properties text eol=lf 16 | *.txt text eol=lf 17 | *.xfdf text eol=lf 18 | *.xml text eol=lf 19 | 20 | # Declare files that will always have CRLF line endings on checkout. 21 | *.bat text eol=crlf 22 | *.csproj text eol=crlf 23 | *.sln text eol=crlf 24 | 25 | # Denote all files that are truly binary and should not be modified. 26 | *.bmp binary 27 | *.cmp binary 28 | *.dib binary 29 | *.gif binary 30 | *.j2k binary 31 | *.jb2 binary 32 | *.jp2 binary 33 | *.jpg binary 34 | *.key binary 35 | *.otf binary 36 | *.pdf binary 37 | *.pfb binary 38 | *.png binary 39 | *.tif binary 40 | *.tiff binary 41 | *.ttc binary 42 | *.ttf binary 43 | *.wmf binary 44 | *.csv binary -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Created by https://www.gitignore.io 2 | 3 | ### Java ### 4 | *.class 5 | 6 | # Mobile Tools for Java (J2ME) 7 | .mtj.tmp/ 8 | 9 | # Package Files # 10 | *.jar 11 | *.war 12 | *.ear 13 | 14 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 15 | hs_err_pid* 16 | 17 | ### Eclipse ### 18 | *.pydevproject 19 | .metadata 20 | .gradle 21 | bin/ 22 | tmp/ 23 | *.tmp 24 | *.bak 25 | *.swp 26 | *~.nib 27 | local.properties 28 | .settings/ 29 | .loadpath 30 | 31 | # Eclipse Core 32 | .project 33 | 34 | # External tool builders 35 | .externalToolBuilders/ 36 | 37 | # Locally stored "Eclipse launch configurations" 38 | *.launch 39 | 40 | # CDT-specific 41 | .cproject 42 | 43 | # JDT-specific (Eclipse Java Development Tools) 44 | .classpath 45 | 46 | # PDT-specific 47 | .buildpath 48 | 49 | # sbteclipse plugin 50 | .target 51 | 52 | # TeXlipse plugin 53 | .texlipse 54 | 55 | ### Intellij ### 56 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm 57 | 58 | *.iml 59 | 60 | ## Directory-based project format: 61 | .idea/ 62 | # if you remove the above rule, at least ignore the following: 63 | 64 | # User-specific stuff: 65 | # .idea/workspace.xml 66 | # .idea/tasks.xml 67 | # .idea/dictionaries 68 | 69 | # Sensitive or high-churn files: 70 | # .idea/dataSources.ids 71 | # .idea/dataSources.xml 72 | # .idea/sqlDataSources.xml 73 | # .idea/dynamic.xml 74 | # .idea/uiDesigner.xml 75 | 76 | # Gradle: 77 | # .idea/gradle.xml 78 | # .idea/libraries 79 | 80 | # Mongo Explorer plugin: 81 | # .idea/mongoSettings.xml 82 | 83 | ## File-based project format: 84 | *.ipr 85 | *.iws 86 | 87 | ## Plugin-specific files: 88 | 89 | # IntelliJ 90 | out/ 91 | 92 | # mpeltonen/sbt-idea plugin 93 | .idea_modules/ 94 | 95 | # JIRA plugin 96 | atlassian-ide-plugin.xml 97 | 98 | # Crashlytics plugin (for Android Studio and IntelliJ) 99 | com_crashlytics_export_strings.xml 100 | crashlytics.properties 101 | crashlytics-build.properties 102 | 103 | ### NetBeans ### 104 | nbproject/private/ 105 | build/ 106 | nbbuild/ 107 | dist/ 108 | nbdist/ 109 | nbactions.xml 110 | nb-configuration.xml 111 | .nb-gradle/ 112 | 113 | ### Linux ### 114 | *~ 115 | 116 | # KDE directory preferences 117 | .directory 118 | 119 | # Linux trash folder which might appear on any partition or disk 120 | .Trash-* 121 | 122 | ### Windows ### 123 | # Windows image file caches 124 | Thumbs.db 125 | ehthumbs.db 126 | 127 | # Folder config file 128 | Desktop.ini 129 | 130 | # Recycle Bin used on file shares 131 | $RECYCLE.BIN/ 132 | 133 | # Windows Installer files 134 | *.cab 135 | *.msi 136 | *.msm 137 | *.msp 138 | 139 | # Windows shortcuts 140 | *.lnk 141 | 142 | target/ 143 | nbactions*.xml 144 | .checkstyle 145 | .pmd 146 | .pmdruleset.xml 147 | 148 | # Ignore generated files 149 | filmfestival.log 150 | invoices.log 151 | *.lck 152 | results/ 153 | 154 | .vagrant/ 155 | -------------------------------------------------------------------------------- /.mailmap: -------------------------------------------------------------------------------- 1 | Alan Goo 2 | Alexander Chingarev 3 | Alexander Chingarev 4 | Alexander Chingarev 5 | Alexey Subach 6 | Alexey Subach 7 | Amedee Van Gasse 8 | Amedee Van Gasse 9 | Andrew Panfilov 10 | Bart De Meyer 11 | Benoît Lagae 12 | Benoît Lagae 13 | Benoît Lagae 14 | Bruno Lowagie 15 | Bruno Lowagie 16 | Bruno Lowagie 17 | Bryan 18 | Dimitry Alexandrov 19 | Dmitry Trusevich 20 | Dmitry Trusevich 21 | Dominik Helm 22 | gothinkfree 23 | Ilya Idamkin 24 | iText Software 25 | iText Software 26 | iText Software 27 | iText Software 28 | iText Software 29 | iText Software 30 | iText Software 31 | iText Software 32 | Jeff Monson 33 | Joris Schellekens 34 | Kevin Day 35 | Kevin Day 36 | Kevin Willems 37 | Kevin Willems 38 | LaughingMan 39 | Markus Wernig 40 | Marvin Wichmann 41 | Marvin Wichmann 42 | Marvin Wichmann 43 | Michaël Demey michael.demey <> 44 | Michaël Demey 45 | Michaël Demey 46 | Michaël Demey 47 | Michael Glazunoff 48 | Michael Klink 49 | Michael Klink 50 | Nadia Ivaniukovich 51 | Nadia Ivaniukovich 52 | Nadja Sych 53 | Olivier Blaise 54 | Orabi Nakhla 55 | Orabi Nakhla 56 | Paulo Soares 57 | Paulo Soares 58 | Pavel Alay pavel.alay <> 59 | Pavel Alay 60 | Pavel Alay 61 | Pavel Morozov 62 | Pavel Morozov 63 | Peter Goodman 64 | Peter Goodman 65 | Peter Goodman 66 | Peter Kjuak 67 | Richard Cohn 68 | Richard Schwark 69 | Roman Leonov 70 | Roman Nadvodny 71 | Sasha Kalykhan 72 | Sasha Kalykhan 73 | Semen Yakushev 74 | Valera <7691262@mail.ru> <7691262@mail.ru> 75 | Veronika Lisovskaya 76 | Vit Nemecky 77 | Yanina Cheremisina 78 | Yulian Gaponenko 79 | Yulian Gaponenko 80 | -------------------------------------------------------------------------------- /Jenkinsfile: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env groovy 2 | @Library('pipeline-library')_ 3 | 4 | def repoName = "JumpStart" 5 | def dependencyRegex = "itextcore" 6 | 7 | automaticJavaBuild(repoName, dependencyRegex) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [iText: Jump-Start Tutorial](https://kb.itextpdf.com/home/it7kb/ebooks/itext-jump-start-tutorial-for-java) # 2 | 3 | ## How to build ## 4 | 5 | To build the iText Jump-Start Tutorial, [Maven][1], 6 | [Ghostscript][2] and [Imagemagick][3] must be installed. 7 | 8 | ```bash 9 | $ mvn clean install \ 10 | -Dmaven.test.failure.ignore=false \ 11 | -DgsExec=$(which gs) \ 12 | -DcompareExec=$(which compare) \ 13 | -Dmaven.javadoc.failOnError=false \ 14 | > >(tee mvn.log) 2> >(tee mvn-error.log >&2) 15 | ``` 16 | 17 | [1]: http://maven.apache.org/ 18 | [2]: http://www.ghostscript.com/ 19 | [3]: http://www.imagemagick.org/ 20 | -------------------------------------------------------------------------------- /cmpfiles/chapter01/cmp_hello_world.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter01/cmp_hello_world.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter01/cmp_quick_brown_fox.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter01/cmp_quick_brown_fox.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter01/cmp_rick_astley.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter01/cmp_rick_astley.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter01/cmp_united_states.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter01/cmp_united_states.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter02/cmp_axes.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter02/cmp_axes.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter02/cmp_grid_lines.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter02/cmp_grid_lines.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter02/cmp_star_wars.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter02/cmp_star_wars.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter02/cmp_star_wars_crawl.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter02/cmp_star_wars_crawl.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter03/cmp_new_york_times.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter03/cmp_new_york_times.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter03/cmp_premier_league.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter03/cmp_premier_league.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter03/cmp_ufo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter03/cmp_ufo.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_create_and_fill.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_create_and_fill.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_fill_form.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_fill_form.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_flatten_form.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_flatten_form.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_job_application.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_job_application.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_line_annotation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_line_annotation.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_link_annotation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_link_annotation.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_text_annotation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_text_annotation.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter04/cmp_textmarkup_annotation.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter04/cmp_textmarkup_annotation.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter05/cmp_add_content.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter05/cmp_add_content.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter05/cmp_change_page.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter05/cmp_change_page.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter05/cmp_edited_job_application.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter05/cmp_edited_job_application.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter05/cmp_filled_out_job_application.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter05/cmp_filled_out_job_application.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_88th_oscar_combined_documents.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_88th_oscar_combined_documents.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_88th_oscar_combined_documents_xy_pages.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_88th_oscar_combined_documents_xy_pages.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_88th_oscar_the_revenant_nominations_TOC.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_88th_oscar_the_revenant_nominations_TOC.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_combined_forms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_combined_forms.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_fill_out_and_merge_forms.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_fill_out_and_merge_forms.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_fill_out_flatten_forms_merge.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_fill_out_flatten_forms_merge.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_fill_out_flatten_forms_smart_merge.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_fill_out_flatten_forms_smart_merge.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_the_golden_gate_bridge_nup.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_the_golden_gate_bridge_nup.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_the_golden_gate_bridge_scale_shrink.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_the_golden_gate_bridge_scale_shrink.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter06/cmp_the_golden_gate_bridge_tiles.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter06/cmp_the_golden_gate_bridge_tiles.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter07/cmp_merged_PDFA-1a_documents.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter07/cmp_merged_PDFA-1a_documents.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter07/cmp_quick_brown_fox_PDFA-1a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter07/cmp_quick_brown_fox_PDFA-1a.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter07/cmp_quick_brown_fox_PDFA-1b.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter07/cmp_quick_brown_fox_PDFA-1b.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter07/cmp_quick_brown_fox_PDFUA.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter07/cmp_quick_brown_fox_PDFUA.pdf -------------------------------------------------------------------------------- /cmpfiles/chapter07/cmp_united_states_PDFA-3a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/cmpfiles/chapter07/cmp_united_states_PDFA-3a.pdf -------------------------------------------------------------------------------- /findbugs-filter.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | com.itextpdf.publications 5 | tutorial 6 | 9.3.0-SNAPSHOT 7 | iText Publications: Jump-start Tutorial 8 | https://developers.itextpdf.com/content/itext-7-jump-start-tutorial 9 | 10 | 1.70 11 | 7.4.3 12 | 1.8.0 13 | 1.8 14 | UTF-8 15 | UTF-8 16 | 3.1.11 17 | 2.0.16 18 | 19 | 20 | 21 | com.itextpdf 22 | pdftest 23 | ${project.version} 24 | 25 | 26 | com.itextpdf 27 | kernel 28 | ${project.version} 29 | 30 | 31 | com.itextpdf 32 | layout 33 | ${project.version} 34 | 35 | 36 | com.itextpdf 37 | forms 38 | ${project.version} 39 | 40 | 41 | com.itextpdf 42 | barcodes 43 | ${project.version} 44 | 45 | 46 | com.itextpdf 47 | hyph 48 | ${project.version} 49 | 50 | 51 | com.itextpdf 52 | io 53 | ${project.version} 54 | 55 | 56 | com.itextpdf 57 | pdfa 58 | ${project.version} 59 | 60 | 61 | org.slf4j 62 | slf4j-log4j12 63 | ${slf4j-log4j12.version} 64 | 65 | 66 | 67 | 68 | 69 | false 70 | 71 | 72 | true 73 | 74 | itext-snapshot 75 | iText Repository - snapshots 76 | https://repo.itextsupport.com/snapshot 77 | 78 | 79 | 80 | true 81 | 82 | 83 | false 84 | 85 | itext-releases 86 | iText Repository - releases 87 | https://repo.itextsupport.com/releases 88 | 89 | 90 | 91 | 92 | 93 | org.apache.maven.plugins 94 | maven-compiler-plugin 95 | 3.8.1 96 | 97 | ${java.version} 98 | ${java.version} 99 | ${java.version} 100 | true 101 | 102 | 103 | 104 | org.apache.maven.plugins 105 | maven-surefire-plugin 106 | 3.0.0-M3 107 | 108 | UnitTest,BouncyCastleUnitTest 109 | ${env.SUREFIRE_OPTS} 110 | 111 | 112 | 113 | org.apache.maven.plugins 114 | maven-failsafe-plugin 115 | 3.0.0-M3 116 | 117 | 118 | 119 | integration-test 120 | verify 121 | 122 | 123 | 124 | 125 | 126 | **/*.java 127 | 128 | SampleTest,IntegrationTest,BouncyCastleIntegrationTest 129 | ${env.SUREFIRE_OPTS} 130 | 131 | 132 | 133 | com.github.ekryd.sortpom 134 | sortpom-maven-plugin 135 | 2.10.0 136 | 137 | 138 | verify 139 | 140 | sort 141 | 142 | 143 | 144 | 145 | \n 146 | ${project.build.sourceEncoding} 147 | true 148 | true 149 | false 150 | 2 151 | scope 152 | 153 | 154 | 155 | org.codehaus.mojo 156 | tidy-maven-plugin 157 | 1.1.0 158 | 159 | 160 | verify 161 | 162 | pom 163 | 164 | 165 | 166 | 167 | 168 | org.apache.maven.plugins 169 | maven-source-plugin 170 | 3.1.0 171 | 172 | 173 | attach-sources 174 | package 175 | 176 | jar-no-fork 177 | 178 | 179 | 180 | 181 | true 182 | 183 | 184 | 185 | org.apache.maven.plugins 186 | maven-javadoc-plugin 187 | 3.1.1 188 | 189 | 190 | attach-javadocs 191 | package 192 | 193 | jar 194 | 195 | 196 | 197 | 198 | 199 | com.github.spotbugs 200 | spotbugs-maven-plugin 201 | ${spotbugs.version} 202 | 203 | 204 | verify 205 | 206 | check 207 | 208 | 209 | 210 | 211 | Max 212 | High 213 | true 214 | true 215 | findbugs-filter.xml 216 | 217 | 218 | com.mebigfatguy.fb-contrib 219 | fb-contrib 220 | ${fb-contrib.version} 221 | 222 | 223 | com.h3xstream.findsecbugs 224 | findsecbugs-plugin 225 | ${findsecbugs.version} 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | org.apache.maven.plugins 236 | maven-jxr-plugin 237 | 2.5 238 | 239 | 240 | 241 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter01/C01E01_HelloWorld.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter01; 2 | 3 | import com.itextpdf.kernel.pdf.PdfDocument; 4 | import com.itextpdf.kernel.pdf.PdfWriter; 5 | import com.itextpdf.layout.Document; 6 | import com.itextpdf.layout.element.Paragraph; 7 | import java.io.File; 8 | import java.io.IOException; 9 | 10 | /** 11 | * Simple Hello World example. 12 | */ 13 | public class C01E01_HelloWorld { 14 | 15 | public static final String DEST = "results/chapter01/hello_world.pdf"; 16 | 17 | public static void main(String args[]) throws IOException { 18 | File file = new File(DEST); 19 | file.getParentFile().mkdirs(); 20 | new C01E01_HelloWorld().createPdf(DEST); 21 | } 22 | 23 | public void createPdf(String dest) throws IOException { 24 | //Initialize PDF writer 25 | PdfWriter writer = new PdfWriter(dest); 26 | 27 | //Initialize PDF document 28 | PdfDocument pdf = new PdfDocument(writer); 29 | 30 | // Initialize document 31 | Document document = new Document(pdf); 32 | 33 | //Add paragraph to the document 34 | document.add(new Paragraph("Hello World!")); 35 | 36 | //Close document 37 | document.close(); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter01/C01E02_RickAstley.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter01; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.font.PdfFont; 5 | import com.itextpdf.kernel.font.PdfFontFactory; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfWriter; 8 | import com.itextpdf.layout.Document; 9 | import com.itextpdf.layout.element.List; 10 | import com.itextpdf.layout.element.ListItem; 11 | import com.itextpdf.layout.element.Paragraph; 12 | import java.io.File; 13 | import java.io.IOException; 14 | 15 | /** 16 | * Simple List example. 17 | */ 18 | public class C01E02_RickAstley { 19 | public static final String DEST = "results/chapter01/rick_astley.pdf"; 20 | 21 | public static void main(String args[]) throws IOException { 22 | File file = new File(DEST); 23 | file.getParentFile().mkdirs(); 24 | new C01E02_RickAstley().createPdf(DEST); 25 | } 26 | 27 | public void createPdf(String dest) throws IOException { 28 | //Initialize PDF writer 29 | PdfWriter writer = new PdfWriter(dest); 30 | 31 | //Initialize PDF document 32 | PdfDocument pdf = new PdfDocument(writer); 33 | 34 | // Initialize document 35 | Document document = new Document(pdf); 36 | 37 | // Create a PdfFont 38 | PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN); 39 | // Add a Paragraph 40 | document.add(new Paragraph("iText is:").setFont(font)); 41 | // Create a List 42 | List list = new List() 43 | .setSymbolIndent(12) 44 | .setListSymbol("\u2022") 45 | .setFont(font); 46 | // Add ListItem objects 47 | list.add(new ListItem("Never gonna give you up")) 48 | .add(new ListItem("Never gonna let you down")) 49 | .add(new ListItem("Never gonna run around and desert you")) 50 | .add(new ListItem("Never gonna make you cry")) 51 | .add(new ListItem("Never gonna say goodbye")) 52 | .add(new ListItem("Never gonna tell a lie and hurt you")); 53 | // Add the list 54 | document.add(list); 55 | 56 | //Close document 57 | document.close(); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter01/C01E03_QuickBrownFox.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter01; 2 | 3 | import com.itextpdf.io.image.ImageDataFactory; 4 | import com.itextpdf.kernel.pdf.PdfDocument; 5 | import com.itextpdf.kernel.pdf.PdfWriter; 6 | import com.itextpdf.layout.Document; 7 | import com.itextpdf.layout.element.Image; 8 | import com.itextpdf.layout.element.Paragraph; 9 | import java.io.File; 10 | import java.io.IOException; 11 | 12 | /** 13 | * Simple image example. 14 | */ 15 | public class C01E03_QuickBrownFox { 16 | public static final String DOG = "src/main/resources/img/dog.bmp"; 17 | public static final String FOX = "src/main/resources/img/fox.bmp"; 18 | 19 | public static final String DEST = "results/chapter01/quick_brown_fox.pdf"; 20 | 21 | public static void main(String args[]) throws IOException { 22 | File file = new File(DEST); 23 | file.getParentFile().mkdirs(); 24 | new C01E03_QuickBrownFox().createPdf(DEST); 25 | } 26 | 27 | public void createPdf(String dest) throws IOException { 28 | //Initialize PDF writer 29 | PdfWriter writer = new PdfWriter(dest); 30 | 31 | //Initialize PDF document 32 | PdfDocument pdf = new PdfDocument(writer); 33 | 34 | // Initialize document 35 | Document document = new Document(pdf); 36 | 37 | // Compose Paragraph 38 | Image fox = new Image(ImageDataFactory.create(FOX)); 39 | Image dog = new Image(ImageDataFactory.create(DOG)); 40 | Paragraph p = new Paragraph("The quick brown ") 41 | .add(fox) 42 | .add(" jumps over the lazy ") 43 | .add(dog); 44 | // Add Paragraph to document 45 | document.add(p); 46 | 47 | //Close document 48 | document.close(); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter01/C01E04_UnitedStates.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter01; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.font.PdfFont; 5 | import com.itextpdf.kernel.font.PdfFontFactory; 6 | import com.itextpdf.kernel.geom.PageSize; 7 | import com.itextpdf.kernel.pdf.PdfDocument; 8 | import com.itextpdf.kernel.pdf.PdfWriter; 9 | import com.itextpdf.layout.Document; 10 | import com.itextpdf.layout.element.Cell; 11 | import com.itextpdf.layout.element.Paragraph; 12 | import com.itextpdf.layout.element.Table; 13 | import com.itextpdf.layout.properties.UnitValue; 14 | import java.io.BufferedReader; 15 | import java.io.File; 16 | import java.io.FileReader; 17 | import java.io.IOException; 18 | import java.util.StringTokenizer; 19 | 20 | /** 21 | * Simple table example. 22 | */ 23 | public class C01E04_UnitedStates { 24 | public static final String DATA = "src/main/resources/data/united_states.csv"; 25 | 26 | public static final String DEST = "results/chapter01/united_states.pdf"; 27 | 28 | public static void main(String args[]) throws IOException { 29 | File file = new File(DEST); 30 | file.getParentFile().mkdirs(); 31 | new C01E04_UnitedStates().createPdf(DEST); 32 | } 33 | 34 | public void createPdf(String dest) throws IOException { 35 | //Initialize PDF writer 36 | PdfWriter writer = new PdfWriter(dest); 37 | 38 | //Initialize PDF document 39 | PdfDocument pdf = new PdfDocument(writer); 40 | 41 | // Initialize document 42 | Document document = new Document(pdf, PageSize.A4.rotate()); 43 | document.setMargins(20, 20, 20, 20); 44 | 45 | PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA); 46 | PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD); 47 | Table table = new Table(UnitValue.createPercentArray(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1})) 48 | .useAllAvailableWidth(); 49 | BufferedReader br = new BufferedReader(new FileReader(DATA)); 50 | String line = br.readLine(); 51 | process(table, line, bold, true); 52 | while ((line = br.readLine()) != null) { 53 | process(table, line, font, false); 54 | } 55 | br.close(); 56 | document.add(table); 57 | 58 | //Close document 59 | document.close(); 60 | } 61 | 62 | public void process(Table table, String line, PdfFont font, boolean isHeader) { 63 | StringTokenizer tokenizer = new StringTokenizer(line, ";"); 64 | while (tokenizer.hasMoreTokens()) { 65 | if (isHeader) { 66 | table.addHeaderCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font))); 67 | } else { 68 | table.addCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font))); 69 | } 70 | } 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter02/C02E01_Axes.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter02; 2 | 3 | import com.itextpdf.kernel.geom.PageSize; 4 | import com.itextpdf.kernel.pdf.PdfDocument; 5 | import com.itextpdf.kernel.pdf.PdfPage; 6 | import com.itextpdf.kernel.pdf.PdfWriter; 7 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 8 | import com.itextpdf.kernel.pdf.canvas.PdfCanvasConstants; 9 | import java.io.File; 10 | import java.io.IOException; 11 | 12 | /** 13 | * Simple drawing lines example. 14 | */ 15 | public class C02E01_Axes { 16 | 17 | public static final String DEST = "results/chapter02/axes.pdf"; 18 | 19 | public static void main(String args[]) throws IOException { 20 | File file = new File(DEST); 21 | file.getParentFile().mkdirs(); 22 | new C02E01_Axes().createPdf(DEST); 23 | } 24 | 25 | public void createPdf(String dest) throws IOException { 26 | 27 | //Initialize PDF document 28 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 29 | 30 | PageSize ps = PageSize.A4.rotate(); 31 | PdfPage page = pdf.addNewPage(ps); 32 | 33 | PdfCanvas canvas = new PdfCanvas(page); 34 | //Replace the origin of the coordinate system to the center of the page 35 | canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2); 36 | 37 | C02E01_Axes.drawAxes(canvas, ps); 38 | 39 | //Close document 40 | pdf.close(); 41 | 42 | } 43 | 44 | public static void drawAxes(PdfCanvas canvas, PageSize ps) { 45 | //Draw X axis 46 | canvas.moveTo(-(ps.getWidth() / 2 - 15), 0) 47 | .lineTo(ps.getWidth() / 2 - 15, 0) 48 | .stroke(); 49 | 50 | //Draw X axis arrow 51 | canvas.setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND) 52 | .moveTo(ps.getWidth() / 2 - 25, -10) 53 | .lineTo(ps.getWidth() / 2 - 15, 0) 54 | .lineTo(ps.getWidth() / 2 - 25, 10).stroke() 55 | .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.MITER); 56 | 57 | //Draw Y axis 58 | canvas.moveTo(0, -(ps.getHeight() / 2 - 15)) 59 | .lineTo(0, ps.getHeight() / 2 - 15) 60 | .stroke(); 61 | 62 | //Draw Y axis arrow 63 | canvas.saveState() 64 | .setLineJoinStyle(PdfCanvasConstants.LineJoinStyle.ROUND) 65 | .moveTo(-10, ps.getHeight() / 2 - 25) 66 | .lineTo(0, ps.getHeight() / 2 - 15) 67 | .lineTo(10, ps.getHeight() / 2 - 25).stroke() 68 | .restoreState(); 69 | 70 | //Draw X serif 71 | for (int i = -((int) ps.getWidth() / 2 - 61); i < ((int) ps.getWidth() / 2 - 60); i += 40) { 72 | canvas.moveTo(i, 5).lineTo(i, -5); 73 | } 74 | //Draw Y serif 75 | for (int j = -((int) ps.getHeight() / 2 - 57); j < ((int) ps.getHeight() / 2 - 56); j += 40) { 76 | canvas.moveTo(5, j).lineTo(-5, j); 77 | } 78 | canvas.stroke(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter02/C02E02_GridLines.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter02; 2 | 3 | import com.itextpdf.kernel.colors.Color; 4 | import com.itextpdf.kernel.colors.DeviceCmyk; 5 | import com.itextpdf.kernel.geom.PageSize; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfPage; 8 | import com.itextpdf.kernel.pdf.PdfWriter; 9 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 10 | import java.io.File; 11 | import java.io.IOException; 12 | 13 | /** 14 | * Simple changing graphics state example. 15 | */ 16 | public class C02E02_GridLines { 17 | 18 | public static final String DEST = "results/chapter02/grid_lines.pdf"; 19 | 20 | public static void main(String args[]) throws IOException { 21 | File file = new File(DEST); 22 | file.getParentFile().mkdirs(); 23 | new C02E02_GridLines().createPdf(DEST); 24 | } 25 | 26 | public void createPdf(String dest) throws IOException { 27 | 28 | //Initialize PDF document 29 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 30 | 31 | PageSize ps = PageSize.A4.rotate(); 32 | PdfPage page = pdf.addNewPage(ps); 33 | 34 | PdfCanvas canvas = new PdfCanvas(page); 35 | //Replace the origin of the coordinate system to the center of the page 36 | canvas.concatMatrix(1, 0, 0, 1, ps.getWidth() / 2, ps.getHeight() / 2); 37 | 38 | Color grayColor = new DeviceCmyk(0.f, 0.f, 0.f, 0.875f); 39 | Color greenColor = new DeviceCmyk(1.f, 0.f, 1.f, 0.176f); 40 | Color blueColor = new DeviceCmyk(1.f, 0.156f, 0.f, 0.118f); 41 | 42 | canvas.setLineWidth(0.5f).setStrokeColor(blueColor); 43 | 44 | //Draw horizontal grid lines 45 | for (int i = -((int) ps.getHeight() / 2 - 57); i < ((int) ps.getHeight() / 2 - 56); i += 40) { 46 | canvas.moveTo(-(ps.getWidth() / 2 - 15), i) 47 | .lineTo(ps.getWidth() / 2 - 15, i); 48 | } 49 | //Draw vertical grid lines 50 | for (int j = -((int) ps.getWidth() / 2 - 61); j < ((int) ps.getWidth() / 2 - 60); j += 40) { 51 | canvas.moveTo(j, -(ps.getHeight() / 2 - 15)) 52 | .lineTo(j, ps.getHeight() / 2 - 15); 53 | } 54 | canvas.stroke(); 55 | 56 | //Draw axes 57 | canvas.setLineWidth(3).setStrokeColor(grayColor); 58 | C02E01_Axes.drawAxes(canvas, ps); 59 | 60 | //Draw plot 61 | canvas.setLineWidth(2).setStrokeColor(greenColor) 62 | .setLineDash(10, 10, 8) 63 | .moveTo(-(ps.getWidth() / 2 - 15), -(ps.getHeight() / 2 - 15)) 64 | .lineTo(ps.getWidth() / 2 - 15, ps.getHeight() / 2 - 15).stroke(); 65 | 66 | //Close document 67 | pdf.close(); 68 | 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter02/C02E03_StarWars.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter02; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.font.PdfFontFactory; 5 | import com.itextpdf.kernel.geom.PageSize; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfPage; 8 | import com.itextpdf.kernel.pdf.PdfWriter; 9 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 10 | import java.io.File; 11 | import java.io.IOException; 12 | import java.util.ArrayList; 13 | import java.util.List; 14 | 15 | /** 16 | * Simple drawing text example. 17 | */ 18 | public class C02E03_StarWars { 19 | 20 | public static final String DEST = "results/chapter02/star_wars.pdf"; 21 | 22 | public static void main(String args[]) throws IOException { 23 | File file = new File(DEST); 24 | file.getParentFile().mkdirs(); 25 | new C02E03_StarWars().createPdf(DEST); 26 | } 27 | 28 | public void createPdf(String dest) throws IOException { 29 | 30 | //Initialize PDF document 31 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 32 | 33 | //Add new page 34 | PageSize ps = PageSize.A4; 35 | PdfPage page = pdf.addNewPage(ps); 36 | 37 | PdfCanvas canvas = new PdfCanvas(page); 38 | 39 | List text = new ArrayList(); 40 | text.add(" Episode V "); 41 | text.add(" THE EMPIRE STRIKES BACK "); 42 | text.add("It is a dark time for the"); 43 | text.add("Rebellion. Although the Death"); 44 | text.add("Star has been destroyed,"); 45 | text.add("Imperial troops have driven the"); 46 | text.add("Rebel forces from their hidden"); 47 | text.add("base and pursued them across"); 48 | text.add("the galaxy."); 49 | text.add("Evading the dreaded Imperial"); 50 | text.add("Starfleet, a group of freedom"); 51 | text.add("fighters led by Luke Skywalker"); 52 | text.add("has established a new secret"); 53 | text.add("base on the remote ice world"); 54 | text.add("of Hoth..."); 55 | 56 | //Replace the origin of the coordinate system to the top left corner 57 | canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight()); 58 | canvas.beginText() 59 | .setFontAndSize(PdfFontFactory.createFont(StandardFonts.COURIER_BOLD), 14) 60 | .setLeading(14 * 1.2f) 61 | .moveText(70, -40); 62 | for (String s : text) { 63 | //Add text and move to the next line 64 | canvas.newlineShowText(s); 65 | } 66 | canvas.endText(); 67 | 68 | //Close document 69 | pdf.close(); 70 | 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter02/C02E04_StarWarsCrawl.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter02; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.colors.Color; 5 | import com.itextpdf.kernel.colors.ColorConstants; 6 | import com.itextpdf.kernel.colors.DeviceCmyk; 7 | import com.itextpdf.kernel.font.PdfFontFactory; 8 | import com.itextpdf.kernel.geom.PageSize; 9 | import com.itextpdf.kernel.pdf.PdfDocument; 10 | import com.itextpdf.kernel.pdf.PdfPage; 11 | import com.itextpdf.kernel.pdf.PdfWriter; 12 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 13 | import java.io.File; 14 | import java.io.IOException; 15 | import java.util.ArrayList; 16 | import java.util.List; 17 | 18 | /** 19 | * Simple changing text state example. 20 | */ 21 | 22 | public class C02E04_StarWarsCrawl { 23 | 24 | public static final String DEST = "results/chapter02/star_wars_crawl.pdf"; 25 | 26 | public static void main(String args[]) throws IOException { 27 | File file = new File(DEST); 28 | file.getParentFile().mkdirs(); 29 | new C02E04_StarWarsCrawl().createPdf(DEST); 30 | } 31 | 32 | public void createPdf(String dest) throws IOException { 33 | 34 | List text = new ArrayList(); 35 | text.add(" Episode V "); 36 | text.add(" THE EMPIRE STRIKES BACK "); 37 | text.add("It is a dark time for the"); 38 | text.add("Rebellion. Although the Death"); 39 | text.add("Star has been destroyed,"); 40 | text.add("Imperial troops have driven the"); 41 | text.add("Rebel forces from their hidden"); 42 | text.add("base and pursued them across"); 43 | text.add("the galaxy."); 44 | text.add("Evading the dreaded Imperial"); 45 | text.add("Starfleet, a group of freedom"); 46 | text.add("fighters led by Luke Skywalker"); 47 | text.add("has established a new secret"); 48 | text.add("base on the remote ice world"); 49 | text.add("of Hoth..."); 50 | 51 | int maxStringWidth = 0; 52 | for (String fragment : text) { 53 | if (fragment.length() > maxStringWidth) maxStringWidth = fragment.length(); 54 | } 55 | 56 | //Initialize PDF document 57 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 58 | 59 | //Add new page 60 | PageSize ps = PageSize.A4; 61 | PdfPage page = pdf.addNewPage(ps); 62 | 63 | PdfCanvas canvas = new PdfCanvas(page); 64 | 65 | //Set black background 66 | canvas.rectangle(0, 0, ps.getWidth(), ps.getHeight()) 67 | .setColor(ColorConstants.BLACK, true) 68 | .fill(); 69 | 70 | //Replace the origin of the coordinate system to the top left corner 71 | canvas.concatMatrix(1, 0, 0, 1, 0, ps.getHeight()); 72 | Color yellowColor = new DeviceCmyk(0.f, 0.0537f, 0.769f, 0.051f); 73 | float lineHeight = 5; 74 | float yOffset = -40; 75 | canvas.beginText() 76 | .setFontAndSize(PdfFontFactory.createFont(StandardFonts.COURIER_BOLD), 1) 77 | .setColor(yellowColor, true); 78 | for (int j = 0; j < text.size(); j++) { 79 | String line = text.get(j); 80 | float xOffset = ps.getWidth() / 2 - 45 - 8 * j; 81 | float fontSizeCoeff = 6 + j; 82 | float lineSpacing = (lineHeight + j) * j / 1.5f; 83 | int stringWidth = line.length(); 84 | for (int i = 0; i < stringWidth; i++) { 85 | float angle = (maxStringWidth / 2 - i) / 2f; 86 | float charXOffset = (4 + (float) j / 2) * i; 87 | canvas.setTextMatrix(fontSizeCoeff, 0, 88 | angle, fontSizeCoeff / 1.5f, 89 | xOffset + charXOffset, yOffset - lineSpacing) 90 | .showText(String.valueOf(line.charAt(i))); 91 | } 92 | } 93 | canvas.endText(); 94 | 95 | //Close document 96 | pdf.close(); 97 | 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter03/C03E01_NewYorkTimes.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter03; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.io.image.ImageDataFactory; 5 | import com.itextpdf.kernel.colors.ColorConstants; 6 | import com.itextpdf.kernel.font.PdfFont; 7 | import com.itextpdf.kernel.font.PdfFontFactory; 8 | import com.itextpdf.kernel.geom.PageSize; 9 | import com.itextpdf.kernel.geom.Rectangle; 10 | import com.itextpdf.kernel.pdf.*; 11 | import com.itextpdf.layout.ColumnDocumentRenderer; 12 | import com.itextpdf.layout.Document; 13 | import com.itextpdf.layout.element.*; 14 | import java.io.*; 15 | import java.nio.charset.StandardCharsets; 16 | import java.nio.file.Files; 17 | import java.nio.file.Paths; 18 | 19 | /** 20 | * Simple column renderer example. 21 | */ 22 | public class C03E01_NewYorkTimes { 23 | 24 | public static final String DEST = "results/chapter03/new_york_times.pdf"; 25 | 26 | public static final String APPLE_IMG = "src/main/resources/img/ny_times_apple.jpg"; 27 | public static final String APPLE_TXT = "src/main/resources/data/ny_times_apple.txt"; 28 | public static final String FACEBOOK_IMG = "src/main/resources/img/ny_times_fb.jpg"; 29 | public static final String FACEBOOK_TXT = "src/main/resources/data/ny_times_fb.txt"; 30 | public static final String INST_IMG = "src/main/resources/img/ny_times_inst.jpg"; 31 | public static final String INST_TXT = "src/main/resources/data/ny_times_inst.txt"; 32 | 33 | static PdfFont timesNewRoman = null; 34 | static PdfFont timesNewRomanBold = null; 35 | 36 | public static void main(String[] args) throws Exception { 37 | timesNewRoman = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN); 38 | timesNewRomanBold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD); 39 | File file = new File(DEST); 40 | file.getParentFile().mkdirs(); 41 | new C03E01_NewYorkTimes().createPdf(DEST); 42 | } 43 | 44 | protected void createPdf(String dest) throws Exception { 45 | 46 | //Initialize PDF document 47 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 48 | PageSize ps = PageSize.A5; 49 | 50 | // Initialize document 51 | Document document = new Document(pdf, ps); 52 | 53 | //Set column parameters 54 | float offSet = 36; 55 | float columnWidth = (ps.getWidth() - offSet * 2 + 10) / 3; 56 | float columnHeight = ps.getHeight() - offSet * 2; 57 | 58 | //Define column areas 59 | Rectangle[] columns = {new Rectangle(offSet - 5, offSet, columnWidth, columnHeight), 60 | new Rectangle(offSet + columnWidth, offSet, columnWidth, columnHeight), 61 | new Rectangle(offSet + columnWidth * 2 + 5, offSet, columnWidth, columnHeight)}; 62 | document.setRenderer(new ColumnDocumentRenderer(document, columns)); 63 | 64 | Image apple = new Image(ImageDataFactory.create(APPLE_IMG)).setWidth(columnWidth); 65 | String articleApple = new String(Files.readAllBytes(Paths.get(APPLE_TXT)), StandardCharsets.UTF_8); 66 | C03E01_NewYorkTimes.addArticle(document, "Apple Encryption Engineers, if Ordered to Unlock iPhone, Might Resist", "By JOHN MARKOFF MARCH 18, 2016", apple, articleApple); 67 | Image facebook = new Image(ImageDataFactory.create(FACEBOOK_IMG)).setWidth(columnWidth); 68 | String articleFB = new String(Files.readAllBytes(Paths.get(FACEBOOK_TXT)), StandardCharsets.UTF_8); 69 | C03E01_NewYorkTimes.addArticle(document, "With \"Smog Jog\" Through Beijing, Zuckerberg Stirs Debate on Air Pollution", "By PAUL MOZUR MARCH 18, 2016", facebook, articleFB); 70 | Image inst = new Image(ImageDataFactory.create(INST_IMG)).setWidth(columnWidth); 71 | String articleInstagram = new String(Files.readAllBytes(Paths.get(INST_TXT)), StandardCharsets.UTF_8); 72 | C03E01_NewYorkTimes.addArticle(document, "Instagram May Change Your Feed, Personalizing It With an Algorithm","By MIKE ISAAC MARCH 15, 2016", inst, articleInstagram); 73 | 74 | document.close(); 75 | 76 | } 77 | 78 | public static void addArticle(Document doc, String title, String author, Image img, String text) throws IOException { 79 | Paragraph p1 = new Paragraph(title) 80 | .setFont(timesNewRomanBold) 81 | .setFontSize(14); 82 | doc.add(p1); 83 | doc.add(img); 84 | Paragraph p2 = new Paragraph() 85 | .setFont(timesNewRoman) 86 | .setFontSize(7) 87 | .setFontColor(ColorConstants.GRAY) 88 | .add(author); 89 | doc.add(p2); 90 | Paragraph p3 = new Paragraph() 91 | .setFont(timesNewRoman) 92 | .setFontSize(10) 93 | .add(text); 94 | doc.add(p3); 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter03/C03E02_PremierLeague.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter03; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.colors.Color; 5 | import com.itextpdf.kernel.colors.ColorConstants; 6 | import com.itextpdf.kernel.colors.DeviceCmyk; 7 | import com.itextpdf.kernel.font.PdfFont; 8 | import com.itextpdf.kernel.font.PdfFontFactory; 9 | import com.itextpdf.kernel.geom.PageSize; 10 | import com.itextpdf.kernel.geom.Rectangle; 11 | import com.itextpdf.kernel.pdf.PdfDocument; 12 | import com.itextpdf.kernel.pdf.PdfWriter; 13 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 14 | import com.itextpdf.layout.Document; 15 | import com.itextpdf.layout.properties.HorizontalAlignment; 16 | import com.itextpdf.layout.borders.SolidBorder; 17 | import com.itextpdf.layout.element.Cell; 18 | import com.itextpdf.layout.element.Paragraph; 19 | import com.itextpdf.layout.element.Table; 20 | import com.itextpdf.layout.properties.TextAlignment; 21 | import com.itextpdf.layout.properties.UnitValue; 22 | import com.itextpdf.layout.renderer.CellRenderer; 23 | import com.itextpdf.layout.renderer.DrawContext; 24 | import java.io.*; 25 | import java.nio.charset.StandardCharsets; 26 | import java.util.StringTokenizer; 27 | 28 | /** 29 | * Simple table renderer example. 30 | */ 31 | public class C03E02_PremierLeague { 32 | 33 | public static final String DATA = "src/main/resources/data/premier_league.csv"; 34 | public static final String DEST = "results/chapter03/premier_league.pdf"; 35 | 36 | Color greenColor = new DeviceCmyk(0.78f, 0, 0.81f, 0.21f); 37 | Color yellowColor = new DeviceCmyk(0, 0, 0.76f, 0.01f); 38 | Color redColor = new DeviceCmyk(0, 0.76f, 0.86f, 0.01f); 39 | Color blueColor = new DeviceCmyk(0.28f, 0.11f, 0, 0); 40 | 41 | public static void main(String args[]) throws IOException { 42 | File file = new File(DEST); 43 | file.getParentFile().mkdirs(); 44 | new C03E02_PremierLeague().createPdf(DEST); 45 | } 46 | 47 | public void createPdf(String dest) throws IOException { 48 | 49 | //Initialize PDF document 50 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 51 | PageSize ps = new PageSize(842, 680); 52 | 53 | // Initialize document 54 | Document document = new Document(pdf, ps); 55 | 56 | PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA); 57 | PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD); 58 | Table table = new Table(UnitValue.createPercentArray(new float[]{1.5f, 7, 2, 2, 2, 2, 3, 4, 4, 2})); 59 | table 60 | .setTextAlignment(TextAlignment.CENTER) 61 | .setHorizontalAlignment(HorizontalAlignment.CENTER); 62 | 63 | BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(DATA), StandardCharsets.UTF_8)); 64 | String line = br.readLine(); 65 | process(table, line, bold, true); 66 | while ((line = br.readLine()) != null) { 67 | process(table, line, font, false); 68 | } 69 | br.close(); 70 | 71 | document.add(table); 72 | 73 | //Close document 74 | document.close(); 75 | 76 | } 77 | 78 | public void process(Table table, String line, PdfFont font, boolean isHeader) { 79 | StringTokenizer tokenizer = new StringTokenizer(line, ";"); 80 | int columnNumber = 0; 81 | while (tokenizer.hasMoreTokens()) { 82 | if (isHeader) { 83 | Cell cell = new Cell().add(new Paragraph(tokenizer.nextToken())); 84 | cell.setNextRenderer(new RoundedCornersCellRenderer(cell)); 85 | cell.setPadding(5).setBorder(null); 86 | table.addHeaderCell(cell); 87 | } else { 88 | columnNumber++; 89 | Cell cell = new Cell().add(new Paragraph(tokenizer.nextToken())); 90 | cell.setFont(font).setBorder(new SolidBorder(ColorConstants.BLACK, 0.5f)); 91 | switch (columnNumber) { 92 | case 4: 93 | cell.setBackgroundColor(greenColor); 94 | break; 95 | case 5: 96 | cell.setBackgroundColor(yellowColor); 97 | break; 98 | case 6: 99 | cell.setBackgroundColor(redColor); 100 | break; 101 | default: 102 | cell.setBackgroundColor(blueColor); 103 | break; 104 | } 105 | table.addCell(cell); 106 | } 107 | } 108 | } 109 | 110 | 111 | private class RoundedCornersCellRenderer extends CellRenderer { 112 | public RoundedCornersCellRenderer(Cell modelElement) { 113 | super(modelElement); 114 | } 115 | 116 | @Override 117 | public void drawBorder(DrawContext drawContext) { 118 | Rectangle rectangle = getOccupiedAreaBBox(); 119 | float llx = rectangle.getX() + 1; 120 | float lly = rectangle.getY() + 1; 121 | float urx = rectangle.getX() + getOccupiedAreaBBox().getWidth() - 1; 122 | float ury = rectangle.getY() + getOccupiedAreaBBox().getHeight() - 1; 123 | PdfCanvas canvas = drawContext.getCanvas(); 124 | float r = 4; 125 | float b = 0.4477f; 126 | canvas.moveTo(llx, lly).lineTo(urx, lly).lineTo(urx, ury - r) 127 | .curveTo(urx, ury - r * b, urx - r * b, ury, urx - r, ury) 128 | .lineTo(llx + r, ury) 129 | .curveTo(llx + r * b, ury, llx, ury - r * b, llx, ury - r) 130 | .lineTo(llx, lly).stroke(); 131 | super.drawBorder(drawContext); 132 | } 133 | } 134 | 135 | } 136 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter03/C03E03_UFO.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter03; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.colors.Color; 5 | import com.itextpdf.kernel.colors.ColorConstants; 6 | import com.itextpdf.kernel.colors.DeviceCmyk; 7 | import com.itextpdf.kernel.pdf.event.AbstractPdfDocumentEventHandler; 8 | import com.itextpdf.kernel.pdf.event.AbstractPdfDocumentEvent; 9 | import com.itextpdf.kernel.pdf.event.PdfDocumentEvent; 10 | import com.itextpdf.kernel.font.PdfFont; 11 | import com.itextpdf.kernel.font.PdfFontFactory; 12 | import com.itextpdf.kernel.geom.Rectangle; 13 | import com.itextpdf.kernel.pdf.PdfDocument; 14 | import com.itextpdf.kernel.pdf.PdfPage; 15 | import com.itextpdf.kernel.pdf.PdfWriter; 16 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 17 | import com.itextpdf.layout.Canvas; 18 | import com.itextpdf.layout.Document; 19 | import com.itextpdf.layout.borders.SolidBorder; 20 | import com.itextpdf.layout.element.Cell; 21 | import com.itextpdf.layout.element.Paragraph; 22 | import com.itextpdf.layout.element.Table; 23 | import com.itextpdf.layout.properties.Property; 24 | import com.itextpdf.layout.properties.TextAlignment; 25 | import com.itextpdf.layout.properties.UnitValue; 26 | import com.itextpdf.layout.properties.VerticalAlignment; 27 | import java.io.BufferedReader; 28 | import java.io.File; 29 | import java.io.FileReader; 30 | import java.util.StringTokenizer; 31 | 32 | /** 33 | * Simple event handler example. 34 | */ 35 | public class C03E03_UFO { 36 | 37 | public static final String DATA = "src/main/resources/data/ufo.csv"; 38 | public static final String DEST = "results/chapter03/ufo.pdf"; 39 | 40 | static PdfFont helvetica = null; 41 | static PdfFont helveticaBold = null; 42 | 43 | public static void main(String[] args) throws Exception { 44 | helvetica = PdfFontFactory.createFont(StandardFonts.HELVETICA); 45 | helveticaBold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD); 46 | File file = new File(DEST); 47 | file.getParentFile().mkdirs(); 48 | new C03E03_UFO().createPdf(DEST); 49 | } 50 | 51 | protected void createPdf(String dest) throws Exception { 52 | 53 | //Initialize PDF document 54 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 55 | pdf.addEventHandler(PdfDocumentEvent.END_PAGE, new MyEventHandler()); 56 | 57 | // Initialize document 58 | Document document = new Document(pdf); 59 | 60 | Paragraph p = new Paragraph("List of reported UFO sightings in 20th century") 61 | .setTextAlignment(TextAlignment.CENTER).setFont(helveticaBold).setFontSize(14); 62 | document.add(p); 63 | 64 | Table table = new Table(UnitValue.createPercentArray(new float[]{3, 5, 7, 4})); 65 | 66 | BufferedReader br = new BufferedReader(new FileReader(DATA)); 67 | String line = br.readLine(); 68 | process(table, line, helveticaBold, true); 69 | while ((line = br.readLine()) != null) { 70 | process(table, line, helvetica, false); 71 | } 72 | br.close(); 73 | 74 | document.add(table); 75 | 76 | document.close(); 77 | } 78 | 79 | public void process(Table table, String line, PdfFont font, boolean isHeader) { 80 | StringTokenizer tokenizer = new StringTokenizer(line, ";"); 81 | while (tokenizer.hasMoreTokens()) { 82 | if (isHeader) { 83 | table.addHeaderCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)).setFontSize(9).setBorder(new SolidBorder(ColorConstants.BLACK, 0.5f))); 84 | } else { 85 | table.addCell(new Cell().add(new Paragraph(tokenizer.nextToken()).setFont(font)).setFontSize(9).setBorder(new SolidBorder(ColorConstants.BLACK, 0.5f))); 86 | } 87 | } 88 | } 89 | 90 | protected static class MyEventHandler extends AbstractPdfDocumentEventHandler { 91 | @Override 92 | public void onAcceptedEvent(AbstractPdfDocumentEvent event) { 93 | PdfDocumentEvent docEvent = (PdfDocumentEvent) event; 94 | PdfDocument pdfDoc = docEvent.getDocument(); 95 | PdfPage page = docEvent.getPage(); 96 | int pageNumber = pdfDoc.getPageNumber(page); 97 | Rectangle pageSize = page.getPageSize(); 98 | PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc); 99 | 100 | //Set background 101 | Color limeColor = new DeviceCmyk(0.208f, 0, 0.584f, 0); 102 | Color blueColor = new DeviceCmyk(0.445f, 0.0546f, 0, 0.0667f); 103 | pdfCanvas.saveState() 104 | .setFillColor(pageNumber % 2 == 1 ? limeColor : blueColor) 105 | .rectangle(pageSize.getLeft(), pageSize.getBottom(), pageSize.getWidth(), pageSize.getHeight()) 106 | .fill().restoreState(); 107 | 108 | //Add header and footer 109 | pdfCanvas.beginText() 110 | .setFontAndSize(helvetica, 9) 111 | .moveText(pageSize.getWidth() / 2 - 60, pageSize.getTop() - 20) 112 | .showText("THE TRUTH IS OUT THERE") 113 | .moveText(60, -pageSize.getTop() + 30) 114 | .showText(String.valueOf(pageNumber)) 115 | .endText(); 116 | 117 | //Add watermark 118 | Canvas canvas = new Canvas(pdfCanvas, page.getPageSize()); 119 | canvas.setFontColor(ColorConstants.WHITE); 120 | canvas.setProperty(Property.FONT_SIZE, UnitValue.createPointValue(60)); 121 | canvas.setProperty(Property.FONT, helveticaBold); 122 | canvas.showTextAligned(new Paragraph("CONFIDENTIAL"), 298, 421, pdfDoc.getPageNumber(page), 123 | TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45); 124 | 125 | pdfCanvas.release(); 126 | } 127 | } 128 | } 129 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E01_01_TextAnnotation.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.kernel.colors.ColorConstants; 4 | import com.itextpdf.kernel.geom.Rectangle; 5 | import com.itextpdf.kernel.pdf.PdfDocument; 6 | import com.itextpdf.kernel.pdf.PdfString; 7 | import com.itextpdf.kernel.pdf.PdfWriter; 8 | import com.itextpdf.kernel.pdf.annot.PdfAnnotation; 9 | import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation; 10 | import com.itextpdf.layout.Document; 11 | import com.itextpdf.layout.element.Paragraph; 12 | import java.io.File; 13 | import java.io.IOException; 14 | 15 | /** 16 | * Simple text annotation example. 17 | */ 18 | public class C04E01_01_TextAnnotation { 19 | 20 | public static final String DEST = "results/chapter04/text_annotation.pdf"; 21 | 22 | public static void main(String args[]) throws IOException { 23 | File file = new File(DEST); 24 | file.getParentFile().mkdirs(); 25 | new C04E01_01_TextAnnotation().createPdf(DEST); 26 | } 27 | 28 | public void createPdf(String dest) throws IOException { 29 | 30 | //Initialize PDF document 31 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 32 | 33 | //Initialize document 34 | Document document = new Document(pdf); 35 | document.add(new Paragraph("The example of text annotation.")); 36 | 37 | //Create text annotation 38 | PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(20, 800, 0, 0)) 39 | .setOpen(true) 40 | .setColor(ColorConstants.GREEN) 41 | .setTitle(new PdfString("iText")) 42 | .setContents("With iText, you can truly take your documentation needs to the next level."); 43 | pdf.getFirstPage().addAnnotation(ann); 44 | 45 | //Close document 46 | document.close(); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E01_02_LinkAnnotation.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.kernel.geom.Rectangle; 4 | import com.itextpdf.kernel.pdf.PdfDocument; 5 | import com.itextpdf.kernel.pdf.PdfWriter; 6 | import com.itextpdf.kernel.pdf.action.PdfAction; 7 | import com.itextpdf.kernel.pdf.annot.PdfLinkAnnotation; 8 | import com.itextpdf.layout.Document; 9 | import com.itextpdf.layout.element.Link; 10 | import com.itextpdf.layout.element.Paragraph; 11 | import java.io.File; 12 | import java.io.IOException; 13 | 14 | /** 15 | * Simple link annotation example. 16 | */ 17 | public class C04E01_02_LinkAnnotation { 18 | 19 | public static final String DEST = "results/chapter04/link_annotation.pdf"; 20 | 21 | public static void main(String args[]) throws IOException { 22 | File file = new File(DEST); 23 | file.getParentFile().mkdirs(); 24 | new C04E01_02_LinkAnnotation().createPdf(DEST); 25 | } 26 | 27 | public void createPdf(String dest) throws IOException { 28 | 29 | //Initialize PDF document 30 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 31 | 32 | //Initialize document 33 | Document document = new Document(pdf); 34 | 35 | //Create link annotation 36 | PdfLinkAnnotation annotation = new PdfLinkAnnotation(new Rectangle(0, 0)) 37 | .setAction(PdfAction.createURI("http://itextpdf.com/")); 38 | Link link = new Link("here", annotation); 39 | Paragraph p = new Paragraph("The example of link annotation. Click ") 40 | .add(link.setUnderline()) 41 | .add(" to learn more..."); 42 | document.add(p); 43 | 44 | //Close document 45 | document.close(); 46 | 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E01_03_LineAnnotation.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.kernel.colors.ColorConstants; 4 | import com.itextpdf.kernel.geom.Rectangle; 5 | import com.itextpdf.kernel.pdf.*; 6 | import com.itextpdf.kernel.pdf.annot.PdfAnnotation; 7 | import com.itextpdf.kernel.pdf.annot.PdfLineAnnotation; 8 | import java.io.File; 9 | import java.io.IOException; 10 | 11 | /** 12 | * Simple line annotation example. 13 | */ 14 | public class C04E01_03_LineAnnotation { 15 | 16 | public static final String DEST = "results/chapter04/line_annotation.pdf"; 17 | 18 | public static void main(String args[]) throws IOException { 19 | File file = new File(DEST); 20 | file.getParentFile().mkdirs(); 21 | new C04E01_03_LineAnnotation().createPdf(DEST); 22 | } 23 | 24 | public void createPdf(String dest) throws IOException { 25 | 26 | //Initialize PDF document 27 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 28 | PdfPage page = pdf.addNewPage(); 29 | 30 | PdfArray lineEndings = new PdfArray(); 31 | lineEndings.add(new PdfName("Diamond")); 32 | lineEndings.add(new PdfName("Diamond")); 33 | 34 | //Create line annotation with inside caption 35 | PdfAnnotation annotation = new PdfLineAnnotation( 36 | new Rectangle(0, 0), 37 | new float[]{20, 790, page.getPageSize().getWidth() - 20, 790}) 38 | .setLineEndingStyles((lineEndings)) 39 | .setContentsAsCaption(true) 40 | .setTitle(new PdfString("iText")) 41 | .setContents("The example of line annotation") 42 | .setColor(ColorConstants.BLUE); 43 | page.addAnnotation(annotation); 44 | 45 | //Close document 46 | pdf.close(); 47 | 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E01_04_TextMarkupAnnotation.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.kernel.colors.ColorConstants; 4 | import com.itextpdf.kernel.geom.Rectangle; 5 | import com.itextpdf.kernel.pdf.PdfArray; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfString; 8 | import com.itextpdf.kernel.pdf.PdfWriter; 9 | import com.itextpdf.kernel.pdf.annot.PdfAnnotation; 10 | import com.itextpdf.kernel.pdf.annot.PdfTextMarkupAnnotation; 11 | import com.itextpdf.layout.Document; 12 | import com.itextpdf.layout.element.Paragraph; 13 | import com.itextpdf.layout.properties.TextAlignment; 14 | import com.itextpdf.layout.properties.VerticalAlignment; 15 | import java.io.File; 16 | import java.io.IOException; 17 | 18 | /** 19 | * Simple text markup annotation example. 20 | */ 21 | public class C04E01_04_TextMarkupAnnotation { 22 | 23 | public static final String DEST = "results/chapter04/textmarkup_annotation.pdf"; 24 | 25 | public static void main(String args[]) throws IOException { 26 | File file = new File(DEST); 27 | file.getParentFile().mkdirs(); 28 | new C04E01_04_TextMarkupAnnotation().createPdf(DEST); 29 | } 30 | 31 | public void createPdf(String dest) throws IOException { 32 | 33 | //Initialize PDF document 34 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 35 | 36 | //Initialize document 37 | Document document = new Document(pdf); 38 | 39 | Paragraph p = new Paragraph("The example of text markup annotation."); 40 | document.showTextAligned(p, 20, 795, 1, TextAlignment.LEFT, 41 | VerticalAlignment.MIDDLE, 0); 42 | 43 | //Create text markup annotation 44 | PdfAnnotation ann = PdfTextMarkupAnnotation.createHighLight(new Rectangle(105, 790, 64, 10), 45 | new float[]{169, 790, 105, 790, 169, 800, 105, 800}) 46 | .setColor(ColorConstants.YELLOW) 47 | .setTitle(new PdfString("Hello!")) 48 | .setContents(new PdfString("I'm a popup.")) 49 | .setTitle(new PdfString("iText")) 50 | .setRectangle(new PdfArray(new float[]{100, 600, 200, 100})); 51 | pdf.getFirstPage().addAnnotation(ann); 52 | 53 | //Close document 54 | document.close(); 55 | 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E02_JobApplication.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.forms.PdfAcroForm; 4 | import com.itextpdf.forms.fields.PdfButtonFormField; 5 | import com.itextpdf.forms.fields.PdfChoiceFormField; 6 | import com.itextpdf.forms.fields.PdfFormCreator; 7 | import com.itextpdf.forms.fields.PdfFormField; 8 | import com.itextpdf.forms.fields.PdfTextFormField; 9 | import com.itextpdf.forms.fields.TextFormFieldBuilder; 10 | import com.itextpdf.forms.fields.CheckBoxFormFieldBuilder; 11 | import com.itextpdf.forms.fields.ChoiceFormFieldBuilder; 12 | import com.itextpdf.forms.fields.PushButtonFormFieldBuilder; 13 | import com.itextpdf.forms.fields.RadioFormFieldBuilder; 14 | import com.itextpdf.forms.fields.properties.CheckBoxType; 15 | import com.itextpdf.kernel.geom.PageSize; 16 | import com.itextpdf.kernel.geom.Rectangle; 17 | import com.itextpdf.kernel.pdf.PdfDocument; 18 | import com.itextpdf.kernel.pdf.PdfWriter; 19 | import com.itextpdf.kernel.pdf.action.PdfAction; 20 | import com.itextpdf.layout.Document; 21 | import com.itextpdf.layout.element.Paragraph; 22 | import com.itextpdf.layout.properties.TextAlignment; 23 | import java.io.File; 24 | import java.io.IOException; 25 | 26 | /** 27 | * Simple widget annotation example. 28 | */ 29 | public class C04E02_JobApplication { 30 | 31 | public static final String DEST = "results/chapter04/job_application.pdf"; 32 | 33 | public static void main(String args[]) throws IOException { 34 | File file = new File(DEST); 35 | file.getParentFile().mkdirs(); 36 | new C04E02_JobApplication().createPdf(DEST); 37 | } 38 | 39 | public void createPdf(String dest) throws IOException { 40 | 41 | //Initialize PDF document 42 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 43 | PageSize ps = PageSize.A4; 44 | pdf.setDefaultPageSize(ps); 45 | 46 | // Initialize document 47 | Document document = new Document(pdf); 48 | 49 | C04E02_JobApplication.addAcroForm(document); 50 | 51 | //Close document 52 | document.close(); 53 | 54 | } 55 | 56 | public static PdfAcroForm addAcroForm(Document doc) { 57 | 58 | Paragraph title = new Paragraph("Application for employment") 59 | .setTextAlignment(TextAlignment.CENTER) 60 | .setFontSize(16); 61 | doc.add(title); 62 | doc.add(new Paragraph("Full name:").setFontSize(12)); 63 | doc.add(new Paragraph("Native language: English French German Russian Spanish").setFontSize(12)); 64 | doc.add(new Paragraph("Experience in: cooking driving software development").setFontSize(12)); 65 | doc.add(new Paragraph("Preferred working shift:").setFontSize(12)); 66 | doc.add(new Paragraph("Additional information:").setFontSize(12)); 67 | 68 | //Add acroform 69 | PdfAcroForm form = PdfFormCreator.getAcroForm(doc.getPdfDocument(), true); 70 | 71 | //Create text field 72 | PdfTextFormField nameField = new TextFormFieldBuilder(doc.getPdfDocument(), "name") 73 | .setWidgetRectangle(new Rectangle(99, 753, 425, 15)).createText(); 74 | nameField.setValue(""); 75 | form.addField(nameField); 76 | 77 | //Create radio buttons 78 | RadioFormFieldBuilder builder = new RadioFormFieldBuilder(doc.getPdfDocument(), "language"); 79 | PdfButtonFormField group = builder.createRadioGroup(); 80 | group.setValue("", true); 81 | group.addKid(builder.createRadioButton("English", new Rectangle(130, 728, 15, 15))); 82 | group.addKid(builder.createRadioButton("French", new Rectangle(200, 728, 15, 15))); 83 | group.addKid(builder.createRadioButton("German", new Rectangle(260, 728, 15, 15))); 84 | group.addKid(builder.createRadioButton("Russian", new Rectangle(330, 728, 15, 15))); 85 | group.addKid(builder.createRadioButton("Spanish", new Rectangle(400, 728, 15, 15))); 86 | form.addField(group); 87 | 88 | //Create checkboxes 89 | for (int i = 0; i < 3; i++) { 90 | PdfButtonFormField checkField = new CheckBoxFormFieldBuilder(doc.getPdfDocument(), "experience".concat(String.valueOf(i + 1))) 91 | .setWidgetRectangle(new Rectangle(119 + i * 69, 701, 15, 15)) 92 | .setCheckType(CheckBoxType.CHECK).createCheckBox(); 93 | checkField.setValue("Off", true); 94 | form.addField(checkField); 95 | } 96 | 97 | //Create combobox 98 | String[] options = {"Any", "6.30 am - 2.30 pm", "1.30 pm - 9.30 pm"}; 99 | PdfChoiceFormField choiceField = new ChoiceFormFieldBuilder(doc.getPdfDocument(), "shift") 100 | .setWidgetRectangle(new Rectangle(163, 676, 115, 15)) 101 | .setOptions(options).createComboBox(); 102 | choiceField.setValue("Any", true); 103 | form.addField(choiceField); 104 | 105 | //Create multiline text field 106 | PdfTextFormField infoField = new TextFormFieldBuilder(doc.getPdfDocument(), "info") 107 | .setWidgetRectangle(new Rectangle(158, 625, 366, 40)).createMultilineText(); 108 | infoField.setValue(""); 109 | form.addField(infoField); 110 | 111 | //Create push button field 112 | PdfButtonFormField button = new PushButtonFormFieldBuilder(doc.getPdfDocument(), "reset").setCaption("RESET") 113 | .setWidgetRectangle(new Rectangle(479, 594, 45, 15)).createPushButton(); 114 | button.getFirstFormAnnotation().setAction(PdfAction.createResetForm(new String[] {"name", "language", "experience1", "experience2", "experience3", "shift", "info"}, 0)); 115 | form.addField(button); 116 | 117 | return form; 118 | 119 | } 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E03_CreateAndFill.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.forms.PdfAcroForm; 4 | import com.itextpdf.forms.fields.PdfFormField; 5 | import com.itextpdf.kernel.pdf.PdfDocument; 6 | import com.itextpdf.kernel.pdf.PdfWriter; 7 | import com.itextpdf.layout.Document; 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.util.Map; 11 | 12 | /** 13 | * Simple filling out form example. 14 | */ 15 | public class C04E03_CreateAndFill { 16 | 17 | public static final String DEST = "results/chapter04/create_and_fill.pdf"; 18 | 19 | public static void main(String args[]) throws IOException { 20 | File file = new File(DEST); 21 | file.getParentFile().mkdirs(); 22 | new C04E03_CreateAndFill().createPdf(DEST); 23 | } 24 | 25 | public void createPdf(String dest) throws IOException { 26 | 27 | //Initialize PDF document 28 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 29 | 30 | // Initialize document 31 | Document doc = new Document(pdf); 32 | 33 | PdfAcroForm form = C04E02_JobApplication.addAcroForm(doc); 34 | Map fields = form.getAllFormFields(); 35 | fields.get("name").setValue("James Bond"); 36 | fields.get("language").setValue("English"); 37 | fields.get("experience1").setValue("Off"); 38 | fields.get("experience2").setValue("Yes"); 39 | fields.get("experience3").setValue("Yes"); 40 | fields.get("shift").setValue("Any"); 41 | fields.get("info").setValue("I was 38 years old when I became an MI6 agent."); 42 | 43 | doc.close(); 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E04_FillForm.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.forms.fields.PdfFormCreator; 4 | import com.itextpdf.kernel.pdf.PdfDocument; 5 | import com.itextpdf.kernel.pdf.PdfReader; 6 | import com.itextpdf.kernel.pdf.PdfWriter; 7 | import com.itextpdf.forms.PdfAcroForm; 8 | import com.itextpdf.forms.fields.PdfFormField; 9 | import java.io.*; 10 | import java.util.Map; 11 | 12 | /** 13 | * Simple filling out form example. 14 | */ 15 | public class C04E04_FillForm { 16 | 17 | public static final String SRC = "src/main/resources/pdf/job_application.pdf"; 18 | public static final String DEST = "results/chapter04/fill_form.pdf"; 19 | 20 | public static void main(String args[]) throws IOException { 21 | File file = new File(DEST); 22 | file.getParentFile().mkdirs(); 23 | new C04E04_FillForm().manipulatePdf(SRC, DEST); 24 | } 25 | 26 | public void manipulatePdf(String src, String dest) throws IOException { 27 | 28 | //Initialize PDF document 29 | PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 30 | 31 | 32 | PdfAcroForm form = PdfFormCreator.getAcroForm(pdf, true); 33 | Map fields = form.getAllFormFields(); 34 | fields.get("name").setValue("James Bond"); 35 | fields.get("language").setValue("English"); 36 | fields.get("experience1").setValue("Off"); 37 | fields.get("experience2").setValue("Yes"); 38 | fields.get("experience3").setValue("Yes"); 39 | fields.get("shift").setValue("Any"); 40 | fields.get("info").setValue("I was 38 years old when I became an MI6 agent."); 41 | 42 | pdf.close(); 43 | 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter04/C04E05_FlattenForm.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter04; 2 | 3 | import com.itextpdf.forms.fields.PdfFormCreator; 4 | import com.itextpdf.kernel.pdf.PdfDocument; 5 | import com.itextpdf.kernel.pdf.PdfReader; 6 | import com.itextpdf.kernel.pdf.PdfWriter; 7 | import com.itextpdf.forms.PdfAcroForm; 8 | import com.itextpdf.forms.fields.PdfFormField; 9 | import java.io.*; 10 | import java.util.Map; 11 | 12 | /** 13 | * Simple filling out form example. 14 | */ 15 | public class C04E05_FlattenForm { 16 | 17 | public static final String SRC = "src/main/resources/pdf/job_application.pdf"; 18 | public static final String DEST = "results/chapter04/flatten_form.pdf"; 19 | 20 | public static void main(String args[]) throws IOException { 21 | File file = new File(DEST); 22 | file.getParentFile().mkdirs(); 23 | new C04E05_FlattenForm().manipulatePdf(SRC, DEST); 24 | } 25 | 26 | public void manipulatePdf(String src, String dest) throws IOException { 27 | 28 | //Initialize PDF document 29 | PdfDocument pdf = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 30 | 31 | 32 | PdfAcroForm form = PdfFormCreator.getAcroForm(pdf, true); 33 | Map fields = form.getAllFormFields(); 34 | fields.get("name").setValue("James Bond"); 35 | fields.get("language").setValue("English"); 36 | fields.get("experience1").setValue("Off"); 37 | fields.get("experience2").setValue("Yes"); 38 | fields.get("experience3").setValue("Yes"); 39 | fields.get("shift").setValue("Any"); 40 | fields.get("info").setValue("I was 38 years old when I became an MI6 agent."); 41 | form.flattenFields(); 42 | 43 | pdf.close(); 44 | 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter05/C05E01_AddAnnotationsAndContent.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter05; 2 | 3 | import com.itextpdf.forms.PdfAcroForm; 4 | import com.itextpdf.forms.fields.PdfButtonFormField; 5 | import com.itextpdf.forms.fields.PdfFormCreator; 6 | import com.itextpdf.forms.fields.PdfFormField; 7 | import com.itextpdf.forms.fields.CheckBoxFormFieldBuilder; 8 | import com.itextpdf.forms.fields.properties.CheckBoxType; 9 | import com.itextpdf.io.font.constants.StandardFonts; 10 | import com.itextpdf.kernel.font.PdfFontFactory; 11 | import com.itextpdf.kernel.geom.Rectangle; 12 | import com.itextpdf.kernel.pdf.PdfDocument; 13 | import com.itextpdf.kernel.pdf.PdfReader; 14 | import com.itextpdf.kernel.pdf.PdfString; 15 | import com.itextpdf.kernel.pdf.PdfWriter; 16 | import com.itextpdf.kernel.pdf.action.PdfAction; 17 | import com.itextpdf.kernel.pdf.annot.PdfAnnotation; 18 | import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation; 19 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 20 | import java.io.File; 21 | import java.io.IOException; 22 | 23 | /** 24 | * Simple adding annotations example. 25 | */ 26 | public class C05E01_AddAnnotationsAndContent { 27 | 28 | public static final String SRC = "src/main/resources/pdf/job_application.pdf"; 29 | public static final String DEST = "results/chapter05/edited_job_application.pdf"; 30 | 31 | public static void main(String args[]) throws IOException { 32 | File file = new File(DEST); 33 | file.getParentFile().mkdirs(); 34 | new C05E01_AddAnnotationsAndContent().manipulatePdf(SRC, DEST); 35 | } 36 | 37 | public void manipulatePdf(String src, String dest) throws IOException { 38 | 39 | //Initialize PDF document 40 | PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 41 | 42 | //Add text annotation 43 | PdfAnnotation ann = new PdfTextAnnotation(new Rectangle(400, 795, 0, 0)) 44 | .setOpen(true) 45 | .setTitle(new PdfString("iText")) 46 | .setContents("Please, fill out the form."); 47 | pdfDoc.getFirstPage().addAnnotation(ann); 48 | 49 | PdfCanvas canvas = new PdfCanvas(pdfDoc.getFirstPage()); 50 | canvas.beginText().setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 12) 51 | .moveText(265, 597) 52 | .showText("I agree to the terms and conditions.") 53 | .endText(); 54 | 55 | //Add form field 56 | PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true); 57 | PdfButtonFormField checkField = new CheckBoxFormFieldBuilder(pdfDoc, "agreement") 58 | .setWidgetRectangle(new Rectangle(245, 594, 15, 15)) 59 | .setCheckType(CheckBoxType.CHECK).createCheckBox(); 60 | checkField.setValue("Off", true); 61 | checkField.setRequired(true); 62 | form.addField(checkField); 63 | 64 | //Update reset button 65 | form.getField("reset").getFirstFormAnnotation().setAction(PdfAction.createResetForm(new String[]{"name", 66 | "language", "experience1", "experience2", "experience3", "shift", "info", "agreement"}, 0)); 67 | 68 | pdfDoc.close(); 69 | 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter05/C05E02_FillAndModifyForm.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter05; 2 | 3 | import com.itextpdf.forms.PdfAcroForm; 4 | import com.itextpdf.forms.fields.PdfFormCreator; 5 | import com.itextpdf.forms.fields.PdfFormField; 6 | import com.itextpdf.io.font.constants.StandardFonts; 7 | import com.itextpdf.kernel.colors.ColorConstants; 8 | import com.itextpdf.kernel.font.PdfFont; 9 | import com.itextpdf.kernel.font.PdfFontFactory; 10 | import com.itextpdf.kernel.pdf.*; 11 | import java.io.File; 12 | import java.io.IOException; 13 | import java.util.ArrayList; 14 | import java.util.List; 15 | import java.util.Map; 16 | 17 | /** 18 | * Simple filling out form example. 19 | */ 20 | public class C05E02_FillAndModifyForm { 21 | 22 | public static final String SRC = "src/main/resources/pdf/job_application.pdf"; 23 | public static final String DEST = "results/chapter05/filled_out_job_application.pdf"; 24 | 25 | public static void main(String args[]) throws IOException { 26 | File file = new File(DEST); 27 | file.getParentFile().mkdirs(); 28 | new C05E02_FillAndModifyForm().manipulatePdf(SRC, DEST); 29 | } 30 | 31 | public void manipulatePdf(String src, String dest) throws IOException { 32 | 33 | //Initialize PDF document 34 | PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 35 | 36 | 37 | PdfAcroForm form = PdfFormCreator.getAcroForm(pdfDoc, true); 38 | Map fields = form.getAllFormFields(); 39 | 40 | fields.get("name").setValue("James Bond").getFirstFormAnnotation().setBackgroundColor(ColorConstants.ORANGE); 41 | fields.get("language").setValue("English"); 42 | 43 | fields.get("experience1").setValue("Yes"); 44 | fields.get("experience2").setValue("Yes"); 45 | fields.get("experience3").setValue("Yes"); 46 | 47 | List options = new ArrayList(); 48 | options.add(new PdfString("Any")); 49 | options.add(new PdfString("8.30 am - 12.30 pm")); 50 | options.add(new PdfString("12.30 pm - 4.30 pm")); 51 | options.add(new PdfString("4.30 pm - 8.30 pm")); 52 | options.add(new PdfString("8.30 pm - 12.30 am")); 53 | options.add(new PdfString("12.30 am - 4.30 am")); 54 | options.add(new PdfString("4.30 am - 8.30 am")); 55 | PdfArray arr = new PdfArray(options); 56 | fields.get("shift").setOptions(arr); 57 | fields.get("shift").setValue("Any"); 58 | 59 | PdfFont courier = PdfFontFactory.createFont(StandardFonts.COURIER); 60 | fields.get("info").setValue("I was 38 years old when I became an MI6 agent.", courier, 7f); 61 | 62 | pdfDoc.close(); 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter05/C05E03_AddContent.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter05; 2 | 3 | import com.itextpdf.io.font.constants.StandardFonts; 4 | import com.itextpdf.kernel.colors.ColorConstants; 5 | import com.itextpdf.kernel.font.PdfFontFactory; 6 | import com.itextpdf.kernel.geom.Rectangle; 7 | import com.itextpdf.kernel.pdf.PdfDocument; 8 | import com.itextpdf.kernel.pdf.PdfPage; 9 | import com.itextpdf.kernel.pdf.PdfReader; 10 | import com.itextpdf.kernel.pdf.PdfWriter; 11 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 12 | import com.itextpdf.kernel.pdf.extgstate.PdfExtGState; 13 | import com.itextpdf.layout.Document; 14 | import com.itextpdf.layout.element.Paragraph; 15 | import com.itextpdf.layout.properties.TextAlignment; 16 | import com.itextpdf.layout.properties.VerticalAlignment; 17 | import java.io.File; 18 | import java.io.IOException; 19 | 20 | /** 21 | * Simple adding content example. 22 | */ 23 | public class C05E03_AddContent { 24 | 25 | public static final String SRC = "src/main/resources/pdf/ufo.pdf"; 26 | 27 | public static final String DEST = "results/chapter05/add_content.pdf"; 28 | 29 | public static void main(String args[]) throws IOException { 30 | File file = new File(DEST); 31 | file.getParentFile().mkdirs(); 32 | new C05E03_AddContent().manipulatePdf(SRC, DEST); 33 | } 34 | 35 | public void manipulatePdf(String src, String dest) throws IOException { 36 | 37 | //Initialize PDF document 38 | PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 39 | 40 | Document document = new Document(pdfDoc); 41 | Rectangle pageSize; 42 | PdfCanvas canvas; 43 | int n = pdfDoc.getNumberOfPages(); 44 | for (int i = 1; i <= n; i++) { 45 | PdfPage page = pdfDoc.getPage(i); 46 | pageSize = page.getPageSize(); 47 | canvas = new PdfCanvas(page); 48 | //Draw header text 49 | canvas.beginText().setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 7) 50 | .moveText(pageSize.getWidth() / 2 - 24, pageSize.getHeight() - 10) 51 | .showText("I want to believe") 52 | .endText(); 53 | //Draw footer line 54 | canvas.setStrokeColor(ColorConstants.BLACK) 55 | .setLineWidth(.2f) 56 | .moveTo(pageSize.getWidth() / 2 - 30, 20) 57 | .lineTo(pageSize.getWidth() / 2 + 30, 20).stroke(); 58 | //Draw page number 59 | canvas.beginText().setFontAndSize(PdfFontFactory.createFont(StandardFonts.HELVETICA), 7) 60 | .moveText(pageSize.getWidth() / 2 - 7, 10) 61 | .showText(String.valueOf(i)) 62 | .showText(" of ") 63 | .showText(String.valueOf(n)) 64 | .endText(); 65 | //Draw watermark 66 | Paragraph p = new Paragraph("CONFIDENTIAL").setFontSize(60); 67 | canvas.saveState(); 68 | PdfExtGState gs1 = new PdfExtGState().setFillOpacity(0.2f); 69 | canvas.setExtGState(gs1); 70 | document.showTextAligned(p, 71 | pageSize.getWidth() / 2, pageSize.getHeight() / 2, 72 | pdfDoc.getPageNumber(page), 73 | TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45); 74 | canvas.restoreState(); 75 | } 76 | 77 | pdfDoc.close(); 78 | 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter05/C05E04_ChangePage.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter05; 2 | 3 | import com.itextpdf.kernel.colors.ColorConstants; 4 | import com.itextpdf.kernel.geom.Rectangle; 5 | import com.itextpdf.kernel.pdf.PdfDocument; 6 | import com.itextpdf.kernel.pdf.PdfPage; 7 | import com.itextpdf.kernel.pdf.PdfReader; 8 | import com.itextpdf.kernel.pdf.PdfWriter; 9 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 10 | import java.io.File; 11 | import java.io.IOException; 12 | 13 | /** 14 | * Simple changing page properties example. 15 | */ 16 | public class C05E04_ChangePage { 17 | 18 | public static final String SRC = "src/main/resources/pdf/ufo.pdf"; 19 | public static final String DEST = "results/chapter05/change_page.pdf"; 20 | 21 | public static void main(String args[]) throws IOException { 22 | File file = new File(DEST); 23 | file.getParentFile().mkdirs(); 24 | new C05E04_ChangePage().manipulatePdf(SRC, DEST); 25 | } 26 | 27 | public void manipulatePdf(String src, String dest) throws IOException { 28 | 29 | //Initialize PDF document 30 | PdfDocument pdfDoc = new PdfDocument(new PdfReader(src), new PdfWriter(dest)); 31 | 32 | 33 | float margin = 72; 34 | for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) { 35 | PdfPage page = pdfDoc.getPage(i); 36 | // change page size 37 | Rectangle mediaBox = page.getMediaBox(); 38 | Rectangle newMediaBox = new Rectangle(mediaBox.getLeft() - margin, mediaBox.getBottom() - margin, 39 | mediaBox.getWidth() + margin * 2, mediaBox.getHeight() + margin * 2); 40 | page.setMediaBox(newMediaBox); 41 | // add border 42 | PdfCanvas over = new PdfCanvas(page); 43 | over.setStrokeColor(ColorConstants.GRAY); 44 | over.rectangle(mediaBox.getLeft(), mediaBox.getBottom(), mediaBox.getWidth(), mediaBox.getHeight()); 45 | over.stroke(); 46 | // change rotation of the even pages 47 | if (i % 2 == 0) { 48 | page.setRotation(180); 49 | } 50 | } 51 | 52 | pdfDoc.close(); 53 | 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E01_TheGoldenGateBridge_Scale_Shrink.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.kernel.geom.AffineTransform; 4 | import com.itextpdf.kernel.geom.PageSize; 5 | import com.itextpdf.kernel.geom.Rectangle; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfPage; 8 | import com.itextpdf.kernel.pdf.PdfReader; 9 | import com.itextpdf.kernel.pdf.PdfWriter; 10 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 11 | import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; 12 | import java.io.File; 13 | import java.io.IOException; 14 | 15 | public class C06E01_TheGoldenGateBridge_Scale_Shrink { 16 | public static final String SRC = "src/main/resources/pdf/the_golden_gate_bridge.pdf"; 17 | public static final String DEST = "results/chapter06/the_golden_gate_bridge_scale_shrink.pdf"; 18 | 19 | public static void main(String args[]) throws IOException { 20 | File file = new File(DEST); 21 | file.getParentFile().mkdirs(); 22 | new C06E01_TheGoldenGateBridge_Scale_Shrink().createPdf(SRC, DEST); 23 | } 24 | 25 | public void createPdf(String src, String dest) throws IOException { 26 | //Initialize PDF document 27 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 28 | PdfDocument origPdf = new PdfDocument(new PdfReader(src)); 29 | 30 | //Original page size 31 | PdfPage origPage = origPdf.getPage(1); 32 | Rectangle orig = origPage.getPageSizeWithRotation(); 33 | 34 | //Add A4 page 35 | PdfPage page = pdf.addNewPage(PageSize.A4.rotate()); 36 | //Shrink original page content using transformation matrix 37 | PdfCanvas canvas = new PdfCanvas(page); 38 | AffineTransform transformationMatrix = AffineTransform.getScaleInstance(page.getPageSize().getWidth() / orig.getWidth(), page.getPageSize().getHeight() / orig.getHeight()); 39 | canvas.concatMatrix(transformationMatrix); 40 | PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf); 41 | canvas.addXObjectAt(pageCopy, 0, 0); 42 | 43 | //Add page with original size 44 | pdf.addPage(origPage.copyTo(pdf)); 45 | 46 | //Add A2 page 47 | page = pdf.addNewPage(PageSize.A2.rotate()); 48 | //Scale original page content using transformation matrix 49 | canvas = new PdfCanvas(page); 50 | transformationMatrix = AffineTransform.getScaleInstance(page.getPageSize().getWidth() / orig.getWidth(), page.getPageSize().getHeight() / orig.getHeight()); 51 | canvas.concatMatrix(transformationMatrix); 52 | canvas.addXObjectAt(pageCopy, 0, 0); 53 | 54 | pdf.close(); 55 | origPdf.close(); 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E02_TheGoldenGateBridge_Tiles.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.kernel.geom.AffineTransform; 4 | import com.itextpdf.kernel.geom.PageSize; 5 | import com.itextpdf.kernel.geom.Rectangle; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfPage; 8 | import com.itextpdf.kernel.pdf.PdfReader; 9 | import com.itextpdf.kernel.pdf.PdfWriter; 10 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 11 | import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; 12 | import java.io.File; 13 | import java.io.IOException; 14 | 15 | public class C06E02_TheGoldenGateBridge_Tiles { 16 | public static final String SRC = "src/main/resources/pdf/the_golden_gate_bridge.pdf"; 17 | public static final String DEST = "results/chapter06/the_golden_gate_bridge_tiles.pdf"; 18 | 19 | public static void main(String args[]) throws IOException { 20 | File file = new File(DEST); 21 | file.getParentFile().mkdirs(); 22 | new C06E02_TheGoldenGateBridge_Tiles().createPdf(SRC, DEST); 23 | } 24 | 25 | public void createPdf(String src, String dest) throws IOException { 26 | //Initialize PDF document 27 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 28 | PdfDocument sourcePdf = new PdfDocument(new PdfReader(src)); 29 | 30 | //Original page 31 | PdfPage origPage = sourcePdf.getPage(1); 32 | PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf); 33 | 34 | //Original page size 35 | Rectangle orig = origPage.getPageSize(); 36 | //Tile size 37 | Rectangle tileSize = PageSize.A4.rotate(); 38 | // Transformation matrix 39 | AffineTransform transformationMatrix = AffineTransform.getScaleInstance(tileSize.getWidth() / orig.getWidth() * 2f, tileSize.getHeight() / orig.getHeight() * 2f); 40 | 41 | 42 | //The first tile 43 | PdfPage page = pdf.addNewPage(PageSize.A4.rotate()); 44 | PdfCanvas canvas = new PdfCanvas(page); 45 | canvas.concatMatrix(transformationMatrix); 46 | canvas.addXObjectAt(pageCopy, 0, -orig.getHeight() / 2f); 47 | 48 | //The second tile 49 | page = pdf.addNewPage(PageSize.A4.rotate()); 50 | canvas = new PdfCanvas(page); 51 | canvas.concatMatrix(transformationMatrix); 52 | canvas.addXObjectAt(pageCopy, -orig.getWidth() / 2f, -orig.getHeight() / 2f); 53 | 54 | //The third tile 55 | page = pdf.addNewPage(PageSize.A4.rotate()); 56 | canvas = new PdfCanvas(page); 57 | canvas.concatMatrix(transformationMatrix); 58 | canvas.addXObjectAt(pageCopy, 0, 0); 59 | 60 | //The fourth tile 61 | page = pdf.addNewPage(PageSize.A4.rotate()); 62 | canvas = new PdfCanvas(page); 63 | canvas.concatMatrix(transformationMatrix); 64 | canvas.addXObjectAt(pageCopy, -orig.getWidth() / 2f, 0); 65 | 66 | pdf.close(); 67 | sourcePdf.close(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E03_TheGoldenGateBridge_N_up.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.kernel.geom.AffineTransform; 4 | import com.itextpdf.kernel.geom.PageSize; 5 | import com.itextpdf.kernel.geom.Rectangle; 6 | import com.itextpdf.kernel.pdf.PdfDocument; 7 | import com.itextpdf.kernel.pdf.PdfPage; 8 | import com.itextpdf.kernel.pdf.PdfReader; 9 | import com.itextpdf.kernel.pdf.PdfWriter; 10 | import com.itextpdf.kernel.pdf.canvas.PdfCanvas; 11 | import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; 12 | import java.io.File; 13 | import java.io.IOException; 14 | 15 | public class C06E03_TheGoldenGateBridge_N_up { 16 | public static final String SRC = "src/main/resources/pdf/the_golden_gate_bridge.pdf"; 17 | public static final String DEST = "results/chapter06/the_golden_gate_bridge_nup.pdf"; 18 | 19 | public static void main(String args[]) throws IOException { 20 | File file = new File(DEST); 21 | file.getParentFile().mkdirs(); 22 | new C06E03_TheGoldenGateBridge_N_up().createPdf(DEST); 23 | } 24 | 25 | public void createPdf(String dest) throws IOException { 26 | //Initialize PDF document 27 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 28 | PdfDocument sourcePdf = new PdfDocument(new PdfReader(SRC)); 29 | 30 | //Original page 31 | PdfPage origPage = sourcePdf.getPage(1); 32 | 33 | //Original page size 34 | Rectangle orig = origPage.getPageSize(); 35 | PdfFormXObject pageCopy = origPage.copyAsFormXObject(pdf); 36 | 37 | //N-up page 38 | PageSize nUpPageSize = PageSize.A4.rotate(); 39 | PdfPage page = pdf.addNewPage(nUpPageSize); 40 | PdfCanvas canvas = new PdfCanvas(page); 41 | 42 | //Scale page 43 | AffineTransform transformationMatrix = AffineTransform.getScaleInstance(nUpPageSize.getWidth() / orig.getWidth() / 2f, nUpPageSize.getHeight() / orig.getHeight() / 2f); 44 | canvas.concatMatrix(transformationMatrix); 45 | 46 | //Add pages to N-up page 47 | canvas.addXObjectAt(pageCopy, 0, orig.getHeight()); 48 | canvas.addXObjectAt(pageCopy, orig.getWidth(), orig.getHeight()); 49 | canvas.addXObjectAt(pageCopy, 0, 0); 50 | canvas.addXObjectAt(pageCopy, orig.getWidth(), 0); 51 | 52 | pdf.close(); 53 | sourcePdf.close(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E04_88th_Oscar_Combine.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.kernel.pdf.*; 4 | import com.itextpdf.kernel.utils.PdfMerger; 5 | import java.io.File; 6 | import java.io.IOException; 7 | 8 | public class C06E04_88th_Oscar_Combine { 9 | public static final String SRC1 = "src/main/resources/pdf/88th_reminder_list.pdf"; 10 | public static final String SRC2 = "src/main/resources/pdf/88th_noms_announcement.pdf"; 11 | public static final String DEST = "results/chapter06/88th_oscar_combined_documents.pdf"; 12 | 13 | public static void main(String args[]) throws IOException { 14 | File file = new File(DEST); 15 | file.getParentFile().mkdirs(); 16 | new C06E04_88th_Oscar_Combine().createPdf(DEST); 17 | } 18 | 19 | public void createPdf(String dest) throws IOException { 20 | //Initialize PDF document with output intent 21 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 22 | PdfMerger merger = new PdfMerger(pdf); 23 | 24 | //Add pages from the first document 25 | PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1)); 26 | merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages()); 27 | 28 | //Add pages from the second pdf document 29 | PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2)); 30 | merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages()); 31 | 32 | firstSourcePdf.close(); 33 | secondSourcePdf.close(); 34 | pdf.close(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E05_88th_Oscar_CombineXofY.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.kernel.pdf.PdfDocument; 4 | import com.itextpdf.kernel.pdf.PdfReader; 5 | import com.itextpdf.kernel.pdf.PdfWriter; 6 | import com.itextpdf.kernel.utils.PdfMerger; 7 | import java.io.File; 8 | import java.io.IOException; 9 | import java.util.Arrays; 10 | 11 | public class C06E05_88th_Oscar_CombineXofY { 12 | public static final String SRC1 = "src/main/resources/pdf/88th_reminder_list.pdf"; 13 | public static final String SRC2 = "src/main/resources/pdf/88th_noms_announcement.pdf"; 14 | public static final String DEST = "results/chapter06/88th_oscar_combined_documents_xy_pages.pdf"; 15 | 16 | public static void main(String args[]) throws IOException { 17 | File file = new File(DEST); 18 | file.getParentFile().mkdirs(); 19 | new C06E05_88th_Oscar_CombineXofY().createPdf(DEST); 20 | } 21 | 22 | public void createPdf(String dest) throws IOException { 23 | //Initialize PDF document with output intent 24 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); 25 | 26 | PdfMerger merger = new PdfMerger(pdf); 27 | 28 | //Add pages from the first document 29 | PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1)); 30 | merger.merge(firstSourcePdf, Arrays.asList(1, 5, 7, 1)); 31 | 32 | //Add pages from the second pdf document 33 | PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2)); 34 | merger.merge(secondSourcePdf, Arrays.asList(1, 15)); 35 | 36 | firstSourcePdf.close(); 37 | secondSourcePdf.close(); 38 | pdf.close(); 39 | } 40 | } 41 | 42 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E06_88th_Oscar_Combine_AddTOC.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.kernel.colors.ColorConstants; 4 | import com.itextpdf.kernel.pdf.*; 5 | import com.itextpdf.kernel.pdf.action.PdfAction; 6 | import com.itextpdf.kernel.pdf.canvas.draw.DottedLine; 7 | import com.itextpdf.layout.Document; 8 | import com.itextpdf.layout.properties.Property; 9 | import com.itextpdf.layout.element.Paragraph; 10 | import com.itextpdf.layout.element.Tab; 11 | import com.itextpdf.layout.element.TabStop; 12 | import com.itextpdf.layout.element.Text; 13 | import com.itextpdf.layout.properties.TabAlignment; 14 | import com.itextpdf.layout.properties.TextAlignment; 15 | import java.io.File; 16 | import java.io.IOException; 17 | import java.util.*; 18 | 19 | public class C06E06_88th_Oscar_Combine_AddTOC { 20 | public static final String SRC1 = "src/main/resources/pdf/88th_noms_announcement.pdf"; 21 | public static final String SRC2 = "src/main/resources/pdf/oscars_movies_checklist_2016.pdf"; 22 | public static final String DEST = "results/chapter06/88th_oscar_the_revenant_nominations_TOC.pdf"; 23 | 24 | public static final Map TheRevenantNominations = new TreeMap(); 25 | static { 26 | TheRevenantNominations.put("Performance by an actor in a leading role", 4); 27 | TheRevenantNominations.put("Performance by an actor in a supporting role", 4); 28 | TheRevenantNominations.put("Achievement in cinematography", 4); 29 | TheRevenantNominations.put("Achievement in costume design", 5); 30 | TheRevenantNominations.put("Achievement in directing", 5); 31 | TheRevenantNominations.put("Achievement in film editing", 6); 32 | TheRevenantNominations.put("Achievement in makeup and hairstyling", 7); 33 | TheRevenantNominations.put("Best motion picture of the year", 8); 34 | TheRevenantNominations.put("Achievement in production design", 8); 35 | TheRevenantNominations.put("Achievement in sound editing", 9); 36 | TheRevenantNominations.put("Achievement in sound mixing", 9); 37 | TheRevenantNominations.put("Achievement in visual effects", 10); 38 | } 39 | 40 | public static void main(String args[]) throws IOException { 41 | File file = new File(DEST); 42 | file.getParentFile().mkdirs(); 43 | new C06E06_88th_Oscar_Combine_AddTOC().createPdf(DEST); 44 | } 45 | 46 | public void createPdf(String dest) throws IOException { 47 | PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); 48 | Document document = new Document(pdfDoc); 49 | document.add(new Paragraph(new Text("The Revenant nominations list")) 50 | .setTextAlignment(TextAlignment.CENTER)); 51 | 52 | PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1)); 53 | for (Map.Entry entry : TheRevenantNominations.entrySet()) { 54 | //Copy page 55 | PdfPage page = firstSourcePdf.getPage(entry.getValue()).copyTo(pdfDoc); 56 | pdfDoc.addPage(page); 57 | 58 | //Overwrite page number 59 | Text text = new Text(String.format("Page %d", pdfDoc.getNumberOfPages() - 1)); 60 | text.setBackgroundColor(ColorConstants.WHITE); 61 | document.add(new Paragraph(text).setFixedPosition( 62 | pdfDoc.getNumberOfPages(), 549, 742, 100)); 63 | 64 | //Add destination 65 | String destinationKey = "p" + (pdfDoc.getNumberOfPages() - 1); 66 | PdfArray destinationArray = new PdfArray(); 67 | destinationArray.add(page.getPdfObject()); 68 | destinationArray.add(PdfName.XYZ); 69 | destinationArray.add(new PdfNumber(0)); 70 | destinationArray.add(new PdfNumber(page.getMediaBox().getHeight())); 71 | destinationArray.add(new PdfNumber(1)); 72 | pdfDoc.addNamedDestination(destinationKey, destinationArray); 73 | 74 | //Add TOC line with bookmark 75 | Paragraph p = new Paragraph(); 76 | p.addTabStops(new TabStop(540, TabAlignment.RIGHT, new DottedLine())); 77 | p.add(entry.getKey()); 78 | p.add(new Tab()); 79 | p.add(String.valueOf(pdfDoc.getNumberOfPages() - 1)); 80 | p.setProperty(Property.ACTION, PdfAction.createGoTo(destinationKey)); 81 | document.add(p); 82 | } 83 | firstSourcePdf.close(); 84 | 85 | //Add the last page 86 | PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2)); 87 | PdfPage page = secondSourcePdf.getPage(1).copyTo(pdfDoc); 88 | pdfDoc.addPage(page); 89 | 90 | //Add destination 91 | PdfArray destinationArray = new PdfArray(); 92 | destinationArray.add(page.getPdfObject()); 93 | destinationArray.add(PdfName.XYZ); 94 | destinationArray.add(new PdfNumber(0)); 95 | destinationArray.add(new PdfNumber(page.getMediaBox().getHeight())); 96 | destinationArray.add(new PdfNumber(1)); 97 | pdfDoc.addNamedDestination("checklist", destinationArray); 98 | 99 | //Add TOC line with bookmark 100 | Paragraph p = new Paragraph(); 101 | p.addTabStops(new TabStop(540, TabAlignment.RIGHT, new DottedLine())); 102 | p.add("Oscars\u00ae 2016 Movie Checklist"); 103 | p.add(new Tab()); 104 | p.add(String.valueOf(pdfDoc.getNumberOfPages() - 1)); 105 | p.setProperty(Property.ACTION, PdfAction.createGoTo("checklist")); 106 | document.add(p); 107 | secondSourcePdf.close(); 108 | 109 | // close the document 110 | document.close(); 111 | } 112 | } 113 | 114 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E07_Combine_Forms.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.forms.PdfPageFormCopier; 4 | import com.itextpdf.kernel.pdf.*; 5 | import java.io.File; 6 | import java.io.IOException; 7 | 8 | public class C06E07_Combine_Forms { 9 | public static final String DEST = "results/chapter06/combined_forms.pdf"; 10 | public static final String SRC1 = "src/main/resources/pdf/subscribe.pdf"; 11 | public static final String SRC2 = "src/main/resources/pdf/state.pdf"; 12 | 13 | public static void main(String args[]) throws IOException { 14 | File file = new File(DEST); 15 | file.getParentFile().mkdirs(); 16 | new C06E07_Combine_Forms().createPdf(DEST); 17 | } 18 | 19 | public void createPdf(String dest) throws IOException { 20 | PdfDocument destPdfDocument = new PdfDocument(new PdfWriter(dest)); 21 | PdfDocument[] sources = new PdfDocument[] { 22 | new PdfDocument(new PdfReader(SRC1)), 23 | new PdfDocument(new PdfReader(SRC2)) 24 | }; 25 | PdfPageFormCopier formCopier = new PdfPageFormCopier(); 26 | for (PdfDocument sourcePdfDocument : sources) { 27 | sourcePdfDocument.copyPagesTo( 28 | 1, sourcePdfDocument.getNumberOfPages(), 29 | destPdfDocument, formCopier); 30 | sourcePdfDocument.close(); 31 | } 32 | destPdfDocument.close(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E08_FillOutAndMergeForms.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.forms.PdfAcroForm; 4 | import com.itextpdf.forms.PdfPageFormCopier; 5 | import com.itextpdf.forms.fields.PdfFormCreator; 6 | import com.itextpdf.forms.fields.PdfFormField; 7 | import com.itextpdf.io.source.ByteArrayOutputStream; 8 | import com.itextpdf.kernel.pdf.PdfDocument; 9 | import com.itextpdf.kernel.pdf.PdfReader; 10 | import com.itextpdf.kernel.pdf.PdfWriter; 11 | import java.io.*; 12 | import java.util.Map; 13 | import java.util.StringTokenizer; 14 | 15 | public class C06E08_FillOutAndMergeForms { 16 | public static final String DEST = "results/chapter06/fill_out_and_merge_forms.pdf"; 17 | public static final String SRC = "src/main/resources/pdf/state.pdf"; 18 | public static final String DATA = "src/main/resources/data/united_states.csv"; 19 | 20 | public static void main(String args[]) throws IOException { 21 | File file = new File(DEST); 22 | file.getParentFile().mkdirs(); 23 | new C06E08_FillOutAndMergeForms().createPdf(DEST); 24 | } 25 | 26 | public void createPdf(String dest) throws IOException { 27 | PdfDocument pdfDocument = new PdfDocument(new PdfWriter(dest)); 28 | PdfPageFormCopier formCopier = new PdfPageFormCopier(); 29 | 30 | BufferedReader bufferedReader = new BufferedReader(new FileReader(DATA)); 31 | String line; 32 | boolean headerLine = true; 33 | int i = 1; 34 | while ((line = bufferedReader.readLine()) != null) { 35 | if (headerLine) { 36 | headerLine = false; 37 | continue; 38 | } 39 | 40 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 41 | PdfDocument sourcePdfDocument = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos)); 42 | 43 | //Rename fields 44 | i++; 45 | PdfAcroForm form = PdfFormCreator.getAcroForm(sourcePdfDocument, true); 46 | form.renameField("name", "name_" + i); 47 | form.renameField("abbr", "abbr_" + i); 48 | form.renameField("capital", "capital_" + i); 49 | form.renameField("city", "city_" + i); 50 | form.renameField("population", "population_" + i); 51 | form.renameField("surface", "surface_" + i); 52 | form.renameField("timezone1", "timezone1_" + i); 53 | form.renameField("timezone2", "timezone2_" + i); 54 | form.renameField("dst", "dst_" + i); 55 | 56 | //Fill out fields 57 | StringTokenizer tokenizer = new StringTokenizer(line, ";"); 58 | Map fields = form.getAllFormFields(); 59 | fields.get("name_" + i).setValue(tokenizer.nextToken()); 60 | fields.get("abbr_" + i).setValue(tokenizer.nextToken()); 61 | fields.get("capital_" + i).setValue(tokenizer.nextToken()); 62 | fields.get("city_" + i).setValue(tokenizer.nextToken()); 63 | fields.get("population_" + i).setValue(tokenizer.nextToken()); 64 | fields.get("surface_" + i).setValue(tokenizer.nextToken()); 65 | fields.get("timezone1_" + i).setValue(tokenizer.nextToken()); 66 | fields.get("timezone2_" + i).setValue(tokenizer.nextToken()); 67 | fields.get("dst_" + i).setValue(tokenizer.nextToken()); 68 | 69 | sourcePdfDocument.close(); 70 | sourcePdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray()))); 71 | 72 | //Copy pages 73 | sourcePdfDocument.copyPagesTo(1, sourcePdfDocument.getNumberOfPages(), pdfDocument, formCopier); 74 | sourcePdfDocument.close(); 75 | } 76 | 77 | bufferedReader.close(); 78 | pdfDocument.close(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter06/C06E09_FillOutFlattenAndMergeForms.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter06; 2 | 3 | import com.itextpdf.forms.PdfAcroForm; 4 | import com.itextpdf.forms.fields.PdfFormCreator; 5 | import com.itextpdf.forms.fields.PdfFormField; 6 | import com.itextpdf.io.source.ByteArrayOutputStream; 7 | import com.itextpdf.kernel.pdf.PdfDocument; 8 | import com.itextpdf.kernel.pdf.PdfReader; 9 | import com.itextpdf.kernel.pdf.PdfWriter; 10 | import java.io.*; 11 | import java.util.Map; 12 | import java.util.StringTokenizer; 13 | 14 | public class C06E09_FillOutFlattenAndMergeForms { 15 | public static final String DEST1 = "results/chapter06/fill_out_flatten_forms_merge.pdf"; 16 | public static final String DEST2 = "results/chapter06/fill_out_flatten_forms_smart_merge.pdf"; 17 | public static final String SRC = "src/main/resources/pdf/state.pdf"; 18 | public static final String DATA = "src/main/resources/data/united_states.csv"; 19 | 20 | public static void main(String args[]) throws IOException { 21 | File file = new File(DEST1); 22 | file.getParentFile().mkdirs(); 23 | file = new File(DEST2); 24 | file.getParentFile().mkdirs(); 25 | new C06E09_FillOutFlattenAndMergeForms().createPdf(DEST1, DEST2); 26 | } 27 | 28 | public void createPdf(String dest1, String dest2) throws IOException { 29 | PdfDocument destPdfDocument = new PdfDocument(new PdfWriter(dest1)); 30 | //Smart mode 31 | PdfDocument destPdfDocumentSmartMode = new PdfDocument(new PdfWriter(dest2).setSmartMode(true)); 32 | 33 | BufferedReader bufferedReader = new BufferedReader(new FileReader(DATA)); 34 | String line; 35 | boolean headerLine = true; 36 | while ((line = bufferedReader.readLine()) != null) { 37 | if (headerLine) { 38 | headerLine = false; 39 | continue; 40 | } 41 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 42 | PdfDocument sourcePdfDocument = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos)); 43 | 44 | //Read fields 45 | PdfAcroForm form = PdfFormCreator.getAcroForm(sourcePdfDocument, true); 46 | StringTokenizer tokenizer = new StringTokenizer(line, ";"); 47 | Map fields = form.getAllFormFields(); 48 | 49 | //Fill out fields 50 | fields.get("name").setValue(tokenizer.nextToken()); 51 | fields.get("abbr").setValue(tokenizer.nextToken()); 52 | fields.get("capital").setValue(tokenizer.nextToken()); 53 | fields.get("city").setValue(tokenizer.nextToken()); 54 | fields.get("population").setValue(tokenizer.nextToken()); 55 | fields.get("surface").setValue(tokenizer.nextToken()); 56 | fields.get("timezone1").setValue(tokenizer.nextToken()); 57 | fields.get("timezone2").setValue(tokenizer.nextToken()); 58 | fields.get("dst").setValue(tokenizer.nextToken()); 59 | 60 | //Flatten fields 61 | form.flattenFields(); 62 | 63 | sourcePdfDocument.close(); 64 | sourcePdfDocument = new PdfDocument(new PdfReader(new ByteArrayInputStream(baos.toByteArray()))); 65 | 66 | //Copy pages 67 | sourcePdfDocument.copyPagesTo(1, sourcePdfDocument.getNumberOfPages(), destPdfDocument, null); 68 | sourcePdfDocument.copyPagesTo(1, sourcePdfDocument.getNumberOfPages(), destPdfDocumentSmartMode, null); 69 | 70 | sourcePdfDocument.close(); 71 | } 72 | 73 | bufferedReader.close(); 74 | 75 | destPdfDocument.close(); 76 | destPdfDocumentSmartMode.close(); 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter07/C07E01_QuickBrownFox_PDFUA.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter07; 2 | 3 | import com.itextpdf.io.font.PdfEncodings; 4 | import com.itextpdf.io.image.ImageDataFactory; 5 | import com.itextpdf.kernel.font.PdfFont; 6 | import com.itextpdf.kernel.font.PdfFontFactory; 7 | import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 8 | import com.itextpdf.kernel.pdf.PdfDocument; 9 | import com.itextpdf.kernel.pdf.PdfDocumentInfo; 10 | import com.itextpdf.kernel.pdf.PdfString; 11 | import com.itextpdf.kernel.pdf.PdfUAConformance; 12 | import com.itextpdf.kernel.pdf.PdfViewerPreferences; 13 | import com.itextpdf.kernel.pdf.PdfWriter; 14 | import com.itextpdf.kernel.pdf.WriterProperties; 15 | import com.itextpdf.layout.Document; 16 | import com.itextpdf.layout.element.Image; 17 | import com.itextpdf.layout.element.Paragraph; 18 | import com.itextpdf.layout.element.Text; 19 | 20 | import java.io.File; 21 | import java.io.IOException; 22 | 23 | public class C07E01_QuickBrownFox_PDFUA { 24 | public static final String DOG = "src/main/resources/img/dog.bmp"; 25 | public static final String FOX = "src/main/resources/img/fox.bmp"; 26 | public static final String FONT = "src/main/resources/font/FreeSans.ttf"; 27 | 28 | public static final String DEST = "results/chapter07/quick_brown_fox_PDFUA.pdf"; 29 | 30 | public static void main(String args[]) throws IOException { 31 | File file = new File(DEST); 32 | file.getParentFile().mkdirs(); 33 | new C07E01_QuickBrownFox_PDFUA().createPdf(DEST); 34 | } 35 | 36 | public void createPdf(String dest) throws IOException { 37 | PdfDocument pdf = new PdfDocument(new PdfWriter(dest, new WriterProperties().addPdfUaXmpMetadata( 38 | PdfUAConformance.PDF_UA_1))); 39 | Document document = new Document(pdf); 40 | 41 | //Setting some required parameters 42 | pdf.setTagged(); 43 | pdf.getCatalog().setLang(new PdfString("en-US")); 44 | pdf.getCatalog().setViewerPreferences( 45 | new PdfViewerPreferences().setDisplayDocTitle(true)); 46 | PdfDocumentInfo info = pdf.getDocumentInfo(); 47 | info.setTitle("iText PDF/UA example"); 48 | 49 | //Fonts need to be embedded 50 | PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, EmbeddingStrategy.FORCE_EMBEDDED); 51 | Paragraph p = new Paragraph(); 52 | p.setFont(font); 53 | p.add(new Text("The quick brown ")); 54 | Image foxImage = new Image(ImageDataFactory.create(FOX)); 55 | //PDF/UA: Set alt text 56 | foxImage.getAccessibilityProperties().setAlternateDescription("Fox"); 57 | p.add(foxImage); 58 | p.add(" jumps over the lazy "); 59 | Image dogImage = new Image(ImageDataFactory.create(DOG)); 60 | //PDF/UA: Set alt text 61 | dogImage.getAccessibilityProperties().setAlternateDescription("Dog"); 62 | p.add(dogImage); 63 | 64 | document.add(p); 65 | document.close(); 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter07/C07E02_QuickBrownFox_PDFA_1a.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter07; 2 | 3 | import com.itextpdf.io.font.PdfEncodings; 4 | import com.itextpdf.io.image.ImageDataFactory; 5 | import com.itextpdf.kernel.font.PdfFont; 6 | import com.itextpdf.kernel.font.PdfFontFactory; 7 | import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 8 | import com.itextpdf.kernel.pdf.PdfAConformance; 9 | import com.itextpdf.kernel.pdf.PdfOutputIntent; 10 | import com.itextpdf.kernel.pdf.PdfWriter; 11 | import com.itextpdf.layout.Document; 12 | import com.itextpdf.layout.element.Image; 13 | import com.itextpdf.layout.element.Paragraph; 14 | import com.itextpdf.layout.element.Text; 15 | import com.itextpdf.pdfa.PdfADocument; 16 | import java.io.File; 17 | import java.io.FileInputStream; 18 | import java.io.IOException; 19 | 20 | public class C07E02_QuickBrownFox_PDFA_1a { 21 | public static final String DOG = "src/main/resources/img/dog.bmp"; 22 | public static final String FOX = "src/main/resources/img/fox.bmp"; 23 | public static final String FONT = "src/main/resources/font/FreeSans.ttf"; 24 | public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm"; 25 | 26 | public static final String DEST = "results/chapter07/quick_brown_fox_PDFA-1a.pdf"; 27 | 28 | public static void main(String args[]) throws IOException { 29 | File file = new File(DEST); 30 | file.getParentFile().mkdirs(); 31 | new C07E02_QuickBrownFox_PDFA_1a().createPdf(DEST); 32 | } 33 | 34 | public void createPdf(String dest) throws IOException { 35 | //Initialize PDFA document with output intent 36 | PdfADocument pdf = new PdfADocument(new PdfWriter(dest), 37 | PdfAConformance.PDF_A_1A, 38 | new PdfOutputIntent("Custom", "", "http://www.color.org", 39 | "sRGB IEC61966-2.1", new FileInputStream(INTENT))); 40 | Document document = new Document(pdf); 41 | 42 | //Setting some required parameters 43 | pdf.setTagged(); 44 | 45 | //Fonts need to be embedded 46 | PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, EmbeddingStrategy.FORCE_EMBEDDED); 47 | Paragraph p = new Paragraph(); 48 | p.setFont(font); 49 | p.add(new Text("The quick brown ")); 50 | Image foxImage = new Image(ImageDataFactory.create(FOX)); 51 | //Set alt text 52 | foxImage.getAccessibilityProperties().setAlternateDescription("Fox"); 53 | p.add(foxImage); 54 | p.add(" jumps over the lazy "); 55 | Image dogImage = new Image(ImageDataFactory.create(DOG)); 56 | //Set alt text 57 | dogImage.getAccessibilityProperties().setAlternateDescription("Dog"); 58 | p.add(dogImage); 59 | 60 | document.add(p); 61 | document.close(); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter07/C07E02_QuickBrownFox_PDFA_1b.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter07; 2 | 3 | import com.itextpdf.io.font.PdfEncodings; 4 | import com.itextpdf.io.image.ImageDataFactory; 5 | import com.itextpdf.kernel.font.PdfFont; 6 | import com.itextpdf.kernel.font.PdfFontFactory; 7 | import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 8 | import com.itextpdf.kernel.pdf.PdfAConformance; 9 | import com.itextpdf.kernel.pdf.PdfOutputIntent; 10 | import com.itextpdf.kernel.pdf.PdfWriter; 11 | import com.itextpdf.layout.Document; 12 | import com.itextpdf.layout.element.Image; 13 | import com.itextpdf.layout.element.Paragraph; 14 | import com.itextpdf.layout.element.Text; 15 | import com.itextpdf.pdfa.PdfADocument; 16 | import java.io.File; 17 | import java.io.FileInputStream; 18 | import java.io.IOException; 19 | 20 | public class C07E02_QuickBrownFox_PDFA_1b { 21 | public static final String DOG = "src/main/resources/img/dog.bmp"; 22 | public static final String FOX = "src/main/resources/img/fox.bmp"; 23 | public static final String FONT = "src/main/resources/font/FreeSans.ttf"; 24 | public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm"; 25 | 26 | public static final String DEST = "results/chapter07/quick_brown_fox_PDFA-1b.pdf"; 27 | 28 | public static void main(String args[]) throws IOException { 29 | File file = new File(DEST); 30 | file.getParentFile().mkdirs(); 31 | new C07E02_QuickBrownFox_PDFA_1b().createPdf(DEST); 32 | } 33 | 34 | public void createPdf(String dest) throws IOException { 35 | //Initialize PDFA document with output intent 36 | PdfADocument pdf = new PdfADocument(new PdfWriter(dest), 37 | PdfAConformance.PDF_A_1B, 38 | new PdfOutputIntent("Custom", "", "http://www.color.org", 39 | "sRGB IEC61966-2.1", new FileInputStream(INTENT))); 40 | Document document = new Document(pdf); 41 | 42 | //Fonts need to be embedded 43 | PdfFont font = PdfFontFactory.createFont(FONT, PdfEncodings.WINANSI, EmbeddingStrategy.FORCE_EMBEDDED); 44 | Paragraph p = new Paragraph(); 45 | p.setFont(font); 46 | p.add(new Text("The quick brown ")); 47 | Image foxImage = new Image(ImageDataFactory.create(FOX)); 48 | p.add(foxImage); 49 | p.add(" jumps over the lazy "); 50 | Image dogImage = new Image(ImageDataFactory.create(DOG)); 51 | p.add(dogImage); 52 | 53 | document.add(p); 54 | document.close(); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter07/C07E03_UnitedStates_PDFA_3a.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter07; 2 | 3 | import com.itextpdf.kernel.font.PdfFont; 4 | import com.itextpdf.kernel.font.PdfFontFactory; 5 | import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 6 | import com.itextpdf.kernel.geom.PageSize; 7 | import com.itextpdf.kernel.pdf.*; 8 | import com.itextpdf.kernel.pdf.filespec.PdfFileSpec; 9 | import com.itextpdf.layout.Document; 10 | import com.itextpdf.layout.properties.HorizontalAlignment; 11 | import com.itextpdf.layout.element.Cell; 12 | import com.itextpdf.layout.element.Paragraph; 13 | import com.itextpdf.layout.element.Table; 14 | import com.itextpdf.layout.properties.UnitValue; 15 | import com.itextpdf.pdfa.PdfADocument; 16 | import java.io.*; 17 | import java.nio.file.Files; 18 | import java.nio.file.Paths; 19 | import java.util.StringTokenizer; 20 | 21 | public class C07E03_UnitedStates_PDFA_3a { 22 | public static final String DATA = "src/main/resources/data/united_states.csv"; 23 | public static final String FONT = "src/main/resources/font/FreeSans.ttf"; 24 | public static final String BOLD_FONT = "src/main/resources/font/FreeSansBold.ttf"; 25 | public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm"; 26 | 27 | 28 | public static final String DEST = "results/chapter07/united_states_PDFA-3a.pdf"; 29 | 30 | public static void main(String args[]) throws IOException { 31 | File file = new File(DEST); 32 | file.getParentFile().mkdirs(); 33 | new C07E03_UnitedStates_PDFA_3a().createPdf(DEST); 34 | } 35 | 36 | public void createPdf(String dest) throws IOException { 37 | PdfADocument pdf = new PdfADocument(new PdfWriter(dest), 38 | PdfAConformance.PDF_A_3A, 39 | new PdfOutputIntent("Custom", "", "http://www.color.org", 40 | "sRGB IEC61966-2.1", new FileInputStream(INTENT))); 41 | Document document = new Document(pdf, PageSize.A4.rotate()); 42 | document.setMargins(20, 20, 20, 20); 43 | 44 | //Setting some required parameters 45 | pdf.setTagged(); 46 | pdf.getCatalog().setLang(new PdfString("en-US")); 47 | pdf.getCatalog().setViewerPreferences( 48 | new PdfViewerPreferences().setDisplayDocTitle(true)); 49 | PdfDocumentInfo info = pdf.getDocumentInfo(); 50 | info.setTitle("iText PDF/A-3 example"); 51 | 52 | //Add attachment 53 | PdfDictionary parameters = new PdfDictionary(); 54 | parameters.put(PdfName.ModDate, new PdfDate().getPdfObject()); 55 | PdfFileSpec fileSpec = PdfFileSpec.createEmbeddedFileSpec( 56 | pdf, Files.readAllBytes(Paths.get(DATA)), "united_states.csv", 57 | "united_states.csv", new PdfName("text/csv"), parameters, 58 | PdfName.Data); 59 | fileSpec.put(new PdfName("AFRelationship"), new PdfName("Data")); 60 | pdf.addFileAttachment("united_states.csv", fileSpec); 61 | PdfArray array = new PdfArray(); 62 | array.add(fileSpec.getPdfObject().getIndirectReference()); 63 | pdf.getCatalog().put(new PdfName("AF"), array); 64 | 65 | //Embed fonts 66 | PdfFont font = PdfFontFactory.createFont(FONT, EmbeddingStrategy.FORCE_EMBEDDED); 67 | PdfFont bold = PdfFontFactory.createFont(BOLD_FONT, EmbeddingStrategy.FORCE_EMBEDDED); 68 | 69 | // Create content 70 | Table table = new Table(UnitValue.createPercentArray(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1})) 71 | .useAllAvailableWidth(); 72 | 73 | BufferedReader br = new BufferedReader(new FileReader(DATA)); 74 | String line = br.readLine(); 75 | process(table, line, bold, true); 76 | while ((line = br.readLine()) != null) { 77 | process(table, line, font, false); 78 | } 79 | br.close(); 80 | document.add(table); 81 | 82 | //Close document 83 | document.close(); 84 | } 85 | 86 | public void process(Table table, String line, PdfFont font, boolean isHeader) { 87 | StringTokenizer tokenizer = new StringTokenizer(line, ";"); 88 | while (tokenizer.hasMoreTokens()) { 89 | if (isHeader) { 90 | table.addHeaderCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font))); 91 | } else { 92 | table.addCell(new Cell().setHorizontalAlignment(HorizontalAlignment.CENTER).add(new Paragraph(tokenizer.nextToken()).setHorizontalAlignment(HorizontalAlignment.CENTER).setFont(font))); 93 | } 94 | } 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/main/java/tutorial/chapter07/C07E04_MergePDFADocuments.java: -------------------------------------------------------------------------------- 1 | package tutorial.chapter07; 2 | 3 | import com.itextpdf.kernel.pdf.*; 4 | import com.itextpdf.kernel.utils.PdfMerger; 5 | import com.itextpdf.pdfa.PdfADocument; 6 | import java.io.File; 7 | import java.io.FileInputStream; 8 | import java.io.IOException; 9 | 10 | public class C07E04_MergePDFADocuments { 11 | public static final String INTENT = "src/main/resources/color/sRGB_CS_profile.icm"; 12 | 13 | public static final String SRC1 = "src/main/resources/pdf/quick_brown_fox_PDFA-1a.pdf"; 14 | public static final String SRC2 = "src/main/resources/pdf/united_states_PDFA-1a.pdf"; 15 | public static final String DEST = "results/chapter07/merged_PDFA-1a_documents.pdf"; 16 | 17 | public static void main(String args[]) throws IOException { 18 | File file = new File(DEST); 19 | file.getParentFile().mkdirs(); 20 | new C07E04_MergePDFADocuments().createPdf(DEST); 21 | } 22 | 23 | public void createPdf(String dest) throws IOException { 24 | //Initialize PDFA document with output intent 25 | PdfADocument pdf = new PdfADocument(new PdfWriter(dest), 26 | PdfAConformance.PDF_A_1A, 27 | new PdfOutputIntent("Custom", "", "http://www.color.org", 28 | "sRGB IEC61966-2.1", new FileInputStream(INTENT))); 29 | 30 | //Setting some required parameters 31 | pdf.setTagged(); 32 | pdf.getCatalog().setLang(new PdfString("en-US")); 33 | pdf.getCatalog().setViewerPreferences( 34 | new PdfViewerPreferences().setDisplayDocTitle(true)); 35 | PdfDocumentInfo info = pdf.getDocumentInfo(); 36 | info.setTitle("iText PDF/A-1a example"); 37 | 38 | //Create PdfMerger instance 39 | PdfMerger merger = new PdfMerger(pdf); 40 | //Add pages from the first document 41 | PdfDocument firstSourcePdf = new PdfDocument(new PdfReader(SRC1)); 42 | merger.merge(firstSourcePdf, 1, firstSourcePdf.getNumberOfPages()); 43 | //Add pages from the second pdf document 44 | PdfDocument secondSourcePdf = new PdfDocument(new PdfReader(SRC2)); 45 | merger.merge(secondSourcePdf, 1, secondSourcePdf.getNumberOfPages()); 46 | 47 | //Close the documents 48 | firstSourcePdf.close(); 49 | secondSourcePdf.close(); 50 | pdf.close(); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/main/resources/color/sRGB_CS_profile.icm: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/color/sRGB_CS_profile.icm -------------------------------------------------------------------------------- /src/main/resources/data/ny_times_apple.txt: -------------------------------------------------------------------------------- 1 | Apple employees are already discussing what they will do if ordered to help law enforcement authorities. Some say they may balk at the work, while others may even quit their high-paying jobs rather than undermine the security of the software they have already created, according to more than a half-dozen current and former Apple employees. Among those interviewed were Apple engineers who are involved in the development of mobile products and security, as well as former security engineers and executives. The potential resistance adds a wrinkle to a very public fight between Apple, the world’s most valuable company, and the authorities over access to an iPhone used by one of the attackers in the December mass killing in San Bernardino, Calif. -------------------------------------------------------------------------------- /src/main/resources/data/ny_times_fb.txt: -------------------------------------------------------------------------------- 1 | In a Friday morning post, Facebook’s co-founder and chief executive, Mark Zuckerberg, announced his arrival in Beijing with a blithe message about what must have been a dizzying jog through the center of China’s capital, which has been suffering from a weeklong bout of hazardous air pollution. -------------------------------------------------------------------------------- /src/main/resources/data/ny_times_inst.txt: -------------------------------------------------------------------------------- 1 | That could mean that if your best friend posted a photo of her new Bernese mountain dog’s puppies five hours ago while you were on a flight without Internet connectivity, Instagram might place that image at the top of your feed the next time you open the app. Based on your history of interaction with that friend, Instagram knows you probably would not want to miss that picture. -------------------------------------------------------------------------------- /src/main/resources/data/premier_league.csv: -------------------------------------------------------------------------------- 1 | POS;CLUB;Played;Won;Drawn;Lost;Goals For;Goals against;Goal Difference;Points 2 | 1;Leicester City;30;18;9;3;53;31;22;63 3 | 2;Tottenham Hotspur;30;16;10;4;53;24;29;58 4 | 3;Arsenal;29;15;7;7;46;30;16;52 5 | 4;Manchester City;29;15;6;8;52;31;21;51 6 | 5;West Ham United;29;13;10;6;45;33;12;49 7 | 6;Manchester United;29;13;8;8;37;27;10;47 8 | 7;Southampton;30;12;8;10;38;30;8;44 9 | 8;Liverpool;28;12;8;8;43;37;6;44 10 | 9;Stoke City;30;12;7;11;32;36;-4;43 11 | 10;Chelsea;29;10;10;9;43;39;4;40 12 | 11;West Bromwich Albion;29;10;9;10;30;36;-6;39 13 | 12;Everton;28;9;11;8;51;39;12;38 14 | 13;Bournemouth;30;10;8;12;38;47;-9;38 15 | 14;Watford;29;10;7;12;29;30;-1;37 16 | 15;Crystal Palace;29;9;6;14;32;39;-7;33 17 | 16;Swansea City;30;8;9;13;30;40;-10;33 18 | 17;Sunderland;29;6;7;16;35;54;-19;25 19 | 18;Norwich City;30;6;7;17;31;54;-23;25 20 | 19;Newcastle United;29;6;6;17;28;54;-26;24 21 | 20;Aston Villa;30;3;7;20;22;57;-35;16 22 | -------------------------------------------------------------------------------- /src/main/resources/data/ufo.csv: -------------------------------------------------------------------------------- 1 | Date;Name;City, State;Country 2 | 13.08.1917;Miracle of the Sun;Fatima, Santarem District;Portugal 3 | 18.05.1946;UFO-Memorial Angelholm;Angelholm, Kristianstads County;Sweden 4 | 21.06.1947;Maury Island incident;Puget Sound near Maury Island, Washington;United States 5 | 24.06.1947;Kenneth Arnold UFO sighting;north of Mount Rainier,Washington;United States 6 | 07.06.1947;1947 UFO sightings;mostly Washington;United States 7 | 23.07.1947;Bauru Close Encounter;Bauru, Sao Paulo;Brazil 8 | 07.01.1948;Thomas Mantell;Kentucky;United States 9 | 24.07.1948;Chiles-Whitted UFO encounter;Alabama;United States 10 | 01.10.1948;Gorman dogfight;North Dakota;United States 11 | 15.10.1948;Fukuoka Incident;Kyushu Island;Japan 12 | 24.04.1950;Varese Close Encounter;Abbiate Guazzone,Lombardy;Italy 13 | 11.05.1950;McMinnville UFO photographs;a farm near McMinnville,Oregon;United States 14 | 25.08.1951;Lubbock Lights;Lubbock, Texas;United States 15 | 10.09.1951;Fort Monmouth UFO Case;Monmouth County, New Jersey;United States 16 | 16.07.1952;1952 Salem, Massachusetts UFO incident;Coast Guard Air Station Salem;United States 17 | 24.07.1952;Carson Sink UFO incident;near Carson Sink,Nevada;United States 18 | 12.09.1952;The Flatwoods Monster;Flatwoods, West Virginia;United States 19 | 21.05.1953;Prescott Sightings;Prescott, Arizona;United States 20 | 12.08.1953;Ellsworth UFO sighting;Bismarck, North Dakota;United States 21 | 23.11.1953;The disappearance of Felix Moncla and Robert Wilson;near the Soo Locks overLake Superior;United States/Canada 22 | 16.12.1953;Kelly Johnson/Santa Barbara Channel Case;Seen from Agoura, California, and from aircraft flying over Pacific Ocean;United States 23 | 16.08.1954;Tananarive UFO incident;Tananarive;Madagascar 24 | 15.09.1954;Manbhum UFO sighting;Manbhum, Bihar;India 25 | 24.07.1956;Drakensberg Contactee;Drakensberg;South Africa 26 | 03.05.1957;Edwards Air Force Base flying saucer filming;Edwards Air Force Base(about 22 mi northeast ofLancaster), California;United States 27 | 20.05.1957;Milton Torres 1957 UFO Encounter;East Anglia;United Kingdom 28 | 17.06.1957;The RB-47 UFO Encounter;Forbes Air Force Base, Topeka, Kansas;United States 29 | 04.09.1957;Portugal mothership sighting;Between Granada andPortalegre;Spain /Portugal 30 | 02.11.1957;Levelland UFO Case;Levelland, Texas;United States 31 | 16.01.1958;Trindade UFO Incident;Trindade Island;Brazil 32 | 02.02.1959;Dyatlov Pass incident;Kholat Syakhl, Sverdlovsk Oblast;Soviet Union 33 | 26.06.1959;Father William Booth Gill sighting;Boianai, Territory of Papua and New Guinea;Australia 34 | 19.09.1961;Betty and Barney Hill abduction;south of Lancaster onRoute 3, New Hampshire;United States 35 | 24.04.1964;Lonnie Zamora incident;Socorro, New Mexico;United States 36 | 05.09.1964;Cisco Grove Encounter;Cisco Grove, California;United States 37 | 09.03.1965;The Incident at Exeter;Exeter, New Hampshire;United States 38 | 01.07.1965;The Valensole UFO incident;Valensole, Provence-Alpes-Cote d'Azur;France 39 | 03.07.1965;UFO sightings in Antarctica;Deception Island;Antarctica 40 | 16.09.1965;1965 Craft Landing;Pretoria-Bronkhorstspruit;South Africa 41 | 09.12.1965;Kecksburg UFO incident;Kecksburg, Pennsylvania;United States 42 | 06.04.1966;Westall UFO;Clayton South, Victoria;Australia 43 | 15.04.1966;Catalina Island UFO;Catalina Island;United States 44 | 17.04.1966;Portage County UFO chase;Ohio;United States 45 | 25.01.1967;Betty Andreasson Abduction;South Ashburnham, Massachusetts;United States 46 | 20.05.1967;Falcon Lake incident;near Falcon Lake inWhiteshell Provincial Park, Manitoba;Canada 47 | 29.08.1967;Close encounter of Cussac;outside Cussac,Auvergne;France 48 | 07.09.1967;Snippy the Horse Mutilation;near Alamosa in the San Luis Valley, Colorado;United States 49 | 04.10.1967;Shag Harbour UFO incident;Gulf of Maine near Shag Harbour, Nova Scotia;Canada 50 | 18.02.1968;Vashon Island incident;Vashon Island,Washington State;United States 51 | 07.08.1968;Buff Ledge Camp Abduction;north of Burlington atLake Champlain,Vermont;United States 52 | 28.11.1968;The Sverdlovsk Midget;Sverdlovsk (todayYekaterinburg);Soviet Union 53 | 06.01.1969;Jimmy Carter UFO incident;Leary, Georgia;United States 54 | 01.01.1970;Cowichan District Hospital UFO;Duncan, British Columbia;Canada 55 | 27.06.1972;1972 UFO sightings in the eastern Cape;Fort Beaufort;South Africa 56 | 12.11.1972;1972 UFO sightings in the eastern Cape;Rosmead;South Africa 57 | 20.09.1973;Skylab 3 UFO Encounter;space station Skylab 3 in Earth orbit;outer space 58 | 11.10.1973;Pascagoula Abduction;near Pascagoula at the Pascagoula River, Mississippi;United States 59 | 17.10.1973;Eglin Air Force Base Sighting;Florida;United States 60 | 23.01.1974;Berwyn Mountain UFO incident;Llandrillo,Merionethshire, North Wales;United Kingdom 61 | 31.05.1974;1974 Abduction Event;Beitbridge;South Africa 62 | 25.08.1974;Coyame UFO incident;Coyame, Chihuahua;Mexico 63 | 12.01.1975;Stonehenge incident;North Bergen, New Jersey;United States 64 | 30.10.1975;Wurtsmith AFB;Near Oscoda, Michigan;United States 65 | 05.11.1975;Travis Walton;near Turkey Springs inApache-Sitgreaves National Forest, Arizona;United States 66 | 22.06.1976;1976 Canary Isles sightings;Canary Islands;Spain 67 | 26.08.1976;Allagash Abductions;Eagle Lake on the Allagash waterway, Maine;United States 68 | 19.09.1976;1976 Tehran UFO incident;north of and in Tehran,Mazandaran and Tehran Provinces;Imperial State of Iran 69 | 10.05.1978;Emilcin Abduction;Emilcin, Lublin Voivodeship;Poland 70 | 21.10.1978;Valentich disappearance;Victoria;Australia 71 | 21.12.1978;Kaikoura lights;South Island;New Zealand 72 | 03.01.1979;1979 Mindalore Incident;Krugersdorp;South Africa 73 | 27.08.1979;Val Johnson incident;Marshall County, Minnesota;United States 74 | 09.11.1979;Dechmont Woods Encounter;Dechmont Law nearLivingston, Scotland;United Kingdom 75 | 11.11.1979;Manises UFO incident;Valencia, Province of Valencia;Spain 76 | 29.12.1980;Cash-Landrum incident;on state highway FM 1485 in New Caney,Texas;United States 77 | 08.01.1981;Trans-en-Provence Case;Trans-en-Provence, Provence-Alpes-Cote d'Azur;France 78 | 31.12.1981;Hudson Valley Sightings/Hudson Valley Boomerang;Hudson Valley;United States 79 | 30.06.1983;Copely Woods Encounter;a suburb of Indianapolis,Indiana;United States 80 | 27.12.1985;Communion;New York;United States 81 | 29.01.1986;Height 611 UFO incident;Dalnegorsk, Primorski Krai;Soviet Union 82 | 19.05.1986;Sao Paulo UFO sighting;Sao Paulo and Rio de Janeiro;Brazil 83 | 17.11.1986;Japan Air Lines flight 1628 incident;from eastern to south central Alaska;United States 84 | 11.11.1987;Gulf Breeze UFO incident;Florida;United States 85 | 30.03.1990;Belgian UFO wave;Ans, Wallonia;Belgium 86 | 24.08.1990;Greifswald Lights;Greifswald, Bezirk Rostock;East Germany,Central Europe 87 | 07.04.1991;Baviaanspoort Sighting;Pretoria;South Africa 88 | 15.09.1991;STS-48 incident;Space Shuttle Discoverywhile in orbit;Outer space 89 | 08.08.1993;Kelly Cahill Abduction;at a road in theDandenong Ranges nearBelgrave, Victoria;Australia 90 | 18.11.1993;Sasolburg Sighting;Sasolburg;South Africa 91 | 16.09.1994;Ariel UFO incident;Ruwa, East Mashonaland;Zimbabwe 92 | 25.05.1995;America West Airlines Flight 564;Bovina, Texas;United States 93 | 20.01.1996;Varginha UFO incident;Varginha;Brazil 94 | 28.07.1996;Erasmuskloof Sighting;Pretoria;South Africa 95 | 05.10.1996;Westendorff UFO sighting;Pelotas;Brazil 96 | 02.12.1996;STS-80 incidents;Space Shuttle Columbia while in orbit;Outer space 97 | 13.03.1997;Phoenix Lights;Phoenix, Arizona;United States 98 | 27.12.1998;Graaff-Reinet sighting;Graaff-Reinet;South Africa 99 | 08.05.2000;Warden Sighting;Warden;South Africa 100 | -------------------------------------------------------------------------------- /src/main/resources/data/united_states.csv: -------------------------------------------------------------------------------- 1 | name;abbr;capital;most populous city;population;square miles;time zone 1;time zone 2;dst 2 | ALABAMA;AL;Montgomery;Birmingham;4,708,708;52,423;CST (UTC-6);EST (UTC-5);YES 3 | ALASKA;AK;Juneau;Anchorage;698,473;656,425;AKST (UTC-09) ;HST (UTC-10) ;YES 4 | ARIZONA;AZ;Phoenix;Phoenix;6,595,778;114,006;MT (UTC-07); ;NO 5 | ARKANSAS;AR;Little Rock;Little Rock;2,889,450;53,182;CST (UTC-6); ;YES 6 | CALIFORNIA;CA;Sacramento;Los Angeles;36,961,664;163,707;PT (UTC-8); ;YES 7 | COLORADO;CO;Denver;Denver;5,024,748;104,100;MT (UTC-07); ;YES 8 | CONNECTICUT;CT;Hartford;Bridgeport;3,518,288;5,544;EST (UTC-5); ;YES 9 | DELAWARE;DE;Dover;Wilmington;885,122;1,954;EST (UTC-5); ;YES 10 | FLORIDA;FL;Tallahassee;Jacksonville;18,537,969;65,758;EST (UTC-5);CST (UTC-6);YES 11 | GEORGIA;GA;Atlanta;Atlanta;9,829,211;59,441;EST (UTC-5); ;YES 12 | HAWAII;HI;Honolulu;Honolulu;1,295,178;10,932;HST (UTC-10) ; ;NO 13 | IDAHO;ID;Boise;Boise;1,545,801;83,574;MT (UTC-07);PT (UTC-8);YES 14 | ILLINOIS;IL;Springfield;Chicago;12,910,409;57,918;CST (UTC-6); ;YES 15 | INDIANA;IN;Indianapolis;Indianapolis;6,423,113;36,420;EST (UTC-5);CST (UTC-6);YES 16 | IOWA;IA;Des Moines;Des Moines;3,007,856;56,276;CST (UTC-6); ;YES 17 | KANSAS;KS;Topeka;Wichita;2,818,747;82,282;CST (UTC-6);MT (UTC-07);YES 18 | KENTUCKY;KY;Frankfort;Louisville;4,314,113;40,411;EST (UTC-5);CST (UTC-6);YES 19 | LOUISIANA;LA;Baton Rouge;New Orleans;4,492,076;51,843;CST (UTC-6); ;YES 20 | MAINE;ME;Augusta;Portland;1,318,301;35,387;EST (UTC-5); ;YES 21 | MARYLAND;MD;Annapolis;Baltimore;5,699,478;12,407;EST (UTC-5); ;YES 22 | MASSACHUSETTS;MA;Boston;Boston;6,593,587;10,555;EST (UTC-5); ;YES 23 | MICHIGAN;MI;Lansing;Detroit;9,969,727;96,810;EST (UTC-5);CST (UTC-6);YES 24 | MINNESOTA;MN;Saint Paul;Minneapolis;5,266,214;86,943;CST (UTC-6); ;YES 25 | MISSISSIPPI;MS;Jackson;Jackson;2,951,996;48,434;CST (UTC-6); ;YES 26 | MISSOURI;MO;Jefferson City;Kansas City;5,987,580;69,709;CST (UTC-6); ;YES 27 | MONTANA;MT;Helena;Billings;974,989;147,046;MT (UTC-07); ;YES 28 | NEBRASKA;NE;Lincoln;Omaha;1,796,619;77,358;CST (UTC-6);MT (UTC-07);YES 29 | NEVADA;NV;Carson City;Las Vegas;2,643,085;110,567;PT (UTC-8);MT (UTC-07);YES 30 | NEW HAMPSHIRE;NH;Concord;Machester;1,324,575;9,351;EST (UTC-5); ;YES 31 | NEW JERSEY;NJ;Trenton;Newark;8,707,739;8,722;EST (UTC-5); ;YES 32 | NEW MEXICO;NM;Santa Fe;Albuquerque;2,009,671;121,593;MT (UTC-07); ;YES 33 | NEW YORK;NY;Albany;New York;19,541,453;54,475;EST (UTC-5); ;YES 34 | NORTH CAROLINA;NC;Raleigh;Charlotte;9,380,884;53,821;EST (UTC-5); ;YES 35 | NORTH DAKOTA;ND;Bismarck;Fargo;646,844;70,704;CST (UTC-6);MT (UTC-07);YES 36 | OHIO;OH;Columbus;Columbus;11,542,645;44,828;EST (UTC-5); ;YES 37 | OKLAHOMA;OK;Oklahoma City;Oklahoma City;3,687,050;69,903;CST (UTC-6); ;YES 38 | OREGON;OR;Salem;Portland;3,825,657;98,386;PT (UTC-8);MT (UTC-07);YES 39 | PENNSYLVANIA;PA;Harrisburg;Philadelphia;12,604,767;46,058;EST (UTC-5); ;YES 40 | RHODE ISLAND;RI;Providence;Providence;1,053,209;1,545;EST (UTC-5); ;YES 41 | SOUTH CAROLINA;SC;Columbia;Columbia;4,561,242;32,007;EST (UTC-5); ;YES 42 | SOUTH DAKOTA;SD;Pierre;Sioux Falls;812,383;77,121;CST (UTC-6);MT (UTC-07);YES 43 | TENNESSEE;TN;Nashville;Memphis;6,296,254;42,146;CST (UTC-6);EST (UTC-5);YES 44 | TEXAS;TX;Austin;Houston;24,782,302;268,601;CST (UTC-6);MT (UTC-07);YES 45 | UTAH;UT;Salt Lake City;Salt Lake City;2,784,572;84,904;MT (UTC-07); ;YES 46 | VERMONT;VT;Montpelier;Burlington;621,760;9,615;EST (UTC-5); ;YES 47 | VIRGINIA;VA;Richmond;Virginia Beach;7,882,590;42,769;EST (UTC-5); ;YES 48 | WASHINGTON;WA;Olympia;Seattle;6,664,195;71,303;PT (UTC-8); ;YES 49 | WEST VIRGINIA;WV;Charleston;Charleston;1,819,777;24,231;EST (UTC-5); ;YES 50 | WISCONSIN;WI;Madison;Milwaukee;5,654,774;65,503;CST (UTC-6); ;YES 51 | WYOMING;WY;Cheyenne;Cheyenne;544,270;97,818;MT (UTC-07); ;YES -------------------------------------------------------------------------------- /src/main/resources/font/FreeSans.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/font/FreeSans.ttf -------------------------------------------------------------------------------- /src/main/resources/font/FreeSansBold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/font/FreeSansBold.ttf -------------------------------------------------------------------------------- /src/main/resources/img/car.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/car.jpg -------------------------------------------------------------------------------- /src/main/resources/img/dog.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/dog.bmp -------------------------------------------------------------------------------- /src/main/resources/img/fox.bmp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/fox.bmp -------------------------------------------------------------------------------- /src/main/resources/img/ny_times_apple.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/ny_times_apple.jpg -------------------------------------------------------------------------------- /src/main/resources/img/ny_times_fb.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/ny_times_fb.jpg -------------------------------------------------------------------------------- /src/main/resources/img/ny_times_inst.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/ny_times_inst.jpg -------------------------------------------------------------------------------- /src/main/resources/img/senator.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/img/senator.jpg -------------------------------------------------------------------------------- /src/main/resources/pdf/88th_noms_announcement.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/88th_noms_announcement.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/88th_reminder_list.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/88th_reminder_list.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/job_application.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/job_application.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/oscars_movies_checklist_2016.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/oscars_movies_checklist_2016.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/quick_brown_fox_PDFA-1a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/quick_brown_fox_PDFA-1a.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/state.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/state.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/subscribe.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/subscribe.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/the_golden_gate_bridge.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/the_golden_gate_bridge.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/ufo.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/ufo.pdf -------------------------------------------------------------------------------- /src/main/resources/pdf/united_states_PDFA-1a.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/itext/itext-publications-jumpstart-java/74cb93b6ce648703711de188db9e1464c355e099/src/main/resources/pdf/united_states_PDFA-1a.pdf -------------------------------------------------------------------------------- /src/test/java/tutorial/C06E09_FillOutFlattenAndMergeFormsWrapperTest.java: -------------------------------------------------------------------------------- 1 | package tutorial; 2 | 3 | import com.itextpdf.kernel.utils.CompareTool; 4 | import com.itextpdf.test.RunnerSearchConfig; 5 | import com.itextpdf.test.WrappedSamplesRunner; 6 | 7 | import java.util.Collection; 8 | import java.util.concurrent.TimeUnit; 9 | import org.junit.jupiter.api.Tag; 10 | import org.junit.jupiter.api.Timeout; 11 | import org.junit.jupiter.params.ParameterizedTest; 12 | import org.junit.jupiter.params.provider.MethodSource; 13 | 14 | /** 15 | * C06E09_FillOutFlattenAndMergeForms sample has irregular DEST files: it has two of them, 16 | * so we need process it in a specific way. 17 | */ 18 | @Tag("SampleTest") 19 | public class C06E09_FillOutFlattenAndMergeFormsWrapperTest extends WrappedSamplesRunner { 20 | public static Collection data() { 21 | RunnerSearchConfig searchConfig = new RunnerSearchConfig(); 22 | searchConfig.addClassToRunnerSearchPath("tutorial.chapter06.C06E09_FillOutFlattenAndMergeForms"); 23 | return generateTestsList(searchConfig); 24 | } 25 | 26 | @Timeout(unit = TimeUnit.MILLISECONDS, value = 60000) 27 | @ParameterizedTest(name = "{index}: {0}") 28 | @MethodSource("data") 29 | public void test(RunnerParams data) throws Exception { 30 | this.sampleClassParams = data; 31 | runSamples(); 32 | } 33 | 34 | @Override 35 | protected void comparePdf(String outPath, String dest, String cmp) throws Exception { 36 | CompareTool compareTool = new CompareTool(); 37 | addError(compareTool.compareByContent(dest, cmp, outPath, "diff_")); 38 | addError(compareTool.compareDocumentInfo(dest, cmp)); 39 | 40 | // only DEST1 pdf will be checked, check additionally DEST2 41 | dest = getStringField(sampleClass, "DEST2"); 42 | cmp = getCmpPdf(dest); 43 | 44 | addError(compareTool.compareByContent(dest, cmp, outPath, "diff_")); 45 | addError(compareTool.compareDocumentInfo(dest, cmp)); 46 | } 47 | 48 | @Override 49 | protected String getDest() { 50 | return getStringField(sampleClass, "DEST1"); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/tutorial/JumpStartWrapperTest.java: -------------------------------------------------------------------------------- 1 | package tutorial; 2 | 3 | import com.itextpdf.kernel.utils.CompareTool; 4 | import com.itextpdf.test.RunnerSearchConfig; 5 | import com.itextpdf.test.WrappedSamplesRunner; 6 | 7 | import java.util.Collection; 8 | import java.util.concurrent.TimeUnit; 9 | import org.junit.jupiter.api.Tag; 10 | import org.junit.jupiter.api.Timeout; 11 | import org.junit.jupiter.params.ParameterizedTest; 12 | import org.junit.jupiter.params.provider.MethodSource; 13 | 14 | @Tag("SampleTest") 15 | public class JumpStartWrapperTest extends WrappedSamplesRunner { 16 | public static Collection data() { 17 | RunnerSearchConfig searchConfig = new RunnerSearchConfig(); 18 | searchConfig.addPackageToRunnerSearchPath("tutorial"); 19 | searchConfig.ignorePackageOrClass("tutorial.chapter06.C06E09_FillOutFlattenAndMergeForms"); 20 | searchConfig.ignorePackageOrClass("tutorial.C06E09_FillOutFlattenAndMergeFormsWrapperTest"); 21 | searchConfig.ignorePackageOrClass("tutorial.JumpStartWrapperTest"); 22 | return generateTestsList(searchConfig); 23 | } 24 | 25 | @Timeout(unit = TimeUnit.MILLISECONDS, value = 120000) 26 | @ParameterizedTest(name = "{index}: {0}") 27 | @MethodSource("data") 28 | public void test(RunnerParams data) throws Exception { 29 | this.sampleClassParams = data; 30 | runSamples(); 31 | } 32 | 33 | @Override 34 | protected void comparePdf(String outPath, String dest, String cmp) throws Exception { 35 | CompareTool compareTool = new CompareTool(); 36 | addError(compareTool.compareByContent(dest, cmp, outPath, "diff_")); 37 | addError(compareTool.compareDocumentInfo(dest, cmp)); 38 | } 39 | } 40 | --------------------------------------------------------------------------------