### Make PDF Searchable with ONNX Models Source: https://github.com/itext/itext-java/blob/develop/README.md This example demonstrates how to make an existing PDF searchable by applying OCR using ONNX models. ```Java com.itextpdf.samples.sandbox.pdfocr.onnx.PdfOcrOnnxPdfAsInputExample ``` -------------------------------- ### Create a Simple PDF Document with iText Source: https://github.com/itext/itext-java/blob/develop/README.md This Java example demonstrates the basic usage of iText to create a PDF file named 'hello-pdf.pdf' and add a 'Hello PDF!' paragraph to it. Ensure you have the iText dependencies configured. ```java package com.itextpdf.hellopdf; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import java.io.FileNotFoundException; public class HelloPdfApp { public static void main(String[] args) throws FileNotFoundException { try (Document document = new Document(new PdfDocument(new PdfWriter("./hello-pdf.pdf")))) { document.add(new Paragraph("Hello PDF!")); } } } ``` -------------------------------- ### OCR Images and Create PDF with Tesseract 4 Source: https://github.com/itext/itext-java/blob/develop/README.md Utilize Tesseract 4 OCR engine to process images and create a PDF. This example is part of the iText publications examples. ```Java com.itextpdf.samples.sandbox.pdfocr.tesseract4.PdfOcrTesseractExample ``` -------------------------------- ### GPL Notice for Interactive Programs Source: https://github.com/itext/itext-java/blob/develop/io/src/test/resources/com/itextpdf/io/font/GNU General Public License.txt Display this short notice when a program starts in interactive mode to inform users about its free software status, warranty, and redistribution conditions. The commands 'show w' and 'show c' should display relevant license sections. ```text Copyright (C) This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. ``` -------------------------------- ### Run iText Tests with Ghostscript and ImageMagick Source: https://github.com/itext/itext-java/blob/develop/BUILDING.md Build and run tests, specifying Ghostscript and ImageMagick commands via environment variables. Ensure these tools are installed and accessible. ```bash $ mvn clean install \ -P test \ -Dmaven.test.failure.ignore=false \ -DITEXT_GS_EXEC="gs command" \ -DITEXT_MAGICK_COMPARE_EXEC="magick compare command" \ > >(tee mvn.log) 2> >(tee mvn-error.log >&2) ``` -------------------------------- ### Build iText with GraalVM Native Image Source: https://github.com/itext/itext-java/blob/develop/BUILDING.md Compile iText as a native image using GraalVM. Ensure GraalVM (JDK 22+) is installed and use the 'native' profile. Tests are enabled. ```bash $ mvn clean install -Pnative -DskipTests=false \ -Dmaven.test.failure.ignore=false \ -DITEXT_GS_EXEC="gs command" \ -DITEXT_MAGICK_COMPARE_EXEC="magick compare command" \ > >(tee mvn.log) 2> >(tee mvn-error.log >&2) ``` -------------------------------- ### Convert HTML with Custom Signature Tag to PDF and Sign Source: https://github.com/itext/itext-java/blob/develop/README.md This example shows how to convert an HTML file containing a custom signature tag into a PDF and then sign it. It requires an HTML file with the signature tag and a Java class to perform the conversion and signing. ```Java /* * This class is part of the iText samples and is licensed under the AGPL/Commercial license. * Notice that the AGPL version of iText(1.4.x) differs from the commercial version. * In order to use this form, you will need to have a commercial license * or be in compliance with the AGPL license. */ package com.itextpdf.samples.sandbox.signatures.signaturetag; import com.itextpdf.html2pdf.ConverterProperties; import com.itextpdf.html2pdf.HtmlConverter; import com.itextpdf.kernel.geom.PageSize; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.signatures.BouncyCastleDigest; import com.itextpdf.signatures.CrlClientOffline; import com.itextpdf.signatures.IExternalSignature; import com.itextpdf.signatures.PdfSigner; import com.itextpdf.signatures.SignatureUtil; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.Security; import java.security.cert.Certificate; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.bouncycastle.jce.provider.BouncyCastleProvider; /** * Example that converts an HTML file with a custom signature tag into a PDF and signs it. */ public class ConvertToPdfAndSign { public static final String SRC = "src/main/resources/signatures/signaturetag/ConvertToPdfAndSign/htmlWithSignatureTag.html"; public static final String DEST = "target/signatures/signaturetag/ConvertToPdfAndSign/signedPdfAfterConvert.pdf"; public static final String KEYSTORE = "src/main/resources/cert/certificate.p12"; public static final String PASSWORD = "password"; public static void main(String[] args) throws Exception { // Add BouncyCastleProvider Security.addProvider(new BouncyCastleProvider()); // Create the destination directory if it does not exist File file = new File(DEST); file.getParentFile().mkdirs(); // Convert HTML to PDF and sign convertToPdfAndSign(SRC, DEST); // Verify the signature verifySignatures(DEST); } public static void convertToPdfAndSign(String src, String dest) throws Exception { // Load the keystore KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(new FileInputStream(KEYSTORE), PASSWORD.toCharArray()); // Get the private key and certificate chain String alias = ks.aliases().nextElement(); PrivateKey pk = (PrivateKey) ks.getKey(alias, PASSWORD.toCharArray()); Certificate[] chain = ks.getCertificateChain(alias); // Configure the converter properties ConverterProperties converterProperties = new ConverterProperties(); converterProperties.setBaseUri(SRC.substring(0, SRC.lastIndexOf('/') + 1)); // Set base URI for relative paths // Create a PDF writer and document PdfWriter writer = new PdfWriter(dest); PdfDocument pdfDoc = new PdfDocument(writer); Document document = new Document(pdfDoc, PageSize.A4); // Add a paragraph to the document (optional, for content) document.add(new Paragraph("This is a sample document.")); document.close(); // Close the document to finalize the PDF structure before signing // Sign the PDF PdfSigner pdfSigner = new PdfSigner(pdfDoc, writer, new BouncyCastleDigest()); pdfSigner.setSignerProperties(alias, chain, null, null, null, null, null, PdfSigner.CryptoStandard.CMS); // Set the signature properties IExternalSignature pqs = new BouncyCastleSigner(BouncyCastlePqcV118Constants.KYBER512); // Example using PQC pdfSigner.signDetached(pqs, null); // Convert HTML to PDF and sign HtmlConverter.convertToPdf(new File(src), pdfDoc, converterProperties); // Close the PDF document pdfDoc.close(); } public static void verifySignatures(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfReader(dest)); SignatureUtil util = new SignatureUtil(pdfDoc); List names = util.getSignatureNames(); if (names.isEmpty()) { System.out.println("No signatures found in the document."); } else { for (String name : names) { System.out.println("Signature name: " + name); System.out.println("Signature verified: " + util.signatureCoversWholeDocument(name)); System.out.println("Signature valid: " + util.isSignatureValid(name)); } } pdfDoc.close(); } } ``` ```HTML HTML with Signature Tag

Document Content

This is a sample document that will be converted to PDF and signed.

Signature Placeholder

End of document.

``` -------------------------------- ### Check for Un documented Public API with javadoc Source: https://github.com/itext/itext-java/blob/develop/BUILDING.md Run javadoc to check for any undocumented public methods or classes. This command performs a clean install first, then generates javadoc and filters for warnings or errors. ```bash $ mvn clean install $ mvn javadoc:javadoc | grep -E "(: warning:)|(: error:)" ``` -------------------------------- ### Create PDF/A Compliant Documents with iText Source: https://context7.com/itext/itext-java/llms.txt This Java example shows how to create PDF/A-3B compliant documents using iText. It requires specifying an output intent (color profile) and ensuring all fonts are embedded. ```java import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; import com.itextpdf.kernel.pdf.PdfAConformance; import com.itextpdf.kernel.pdf.PdfOutputIntent; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.pdfa.PdfADocument; import java.io.FileInputStream; import java.io.InputStream; public class PdfAExample { public static void main(String[] args) throws Exception { // Output intent (color profile) required for PDF/A InputStream colorProfile = new FileInputStream("sRGB_CS_profile.icm"); PdfOutputIntent intent = new PdfOutputIntent( "Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", colorProfile ); // Create PDF/A-3B document PdfWriter writer = new PdfWriter("pdfa-document.pdf"); PdfADocument pdf = new PdfADocument(writer, PdfAConformance.PDF_A_3B, intent); Document document = new Document(pdf); // Use embedded font (required for PDF/A) PdfFont font = PdfFontFactory.createFont( "fonts/FreeSans.ttf", PdfFontFactory.EmbeddingStrategy.FORCE_EMBEDDED ); document.setFont(font); document.add(new Paragraph("This is a PDF/A-3B compliant document.")); document.add(new Paragraph("All fonts must be embedded for PDF/A compliance.")); document.close(); } } ``` -------------------------------- ### Set Document Metadata and Properties Source: https://context7.com/itext/itext-java/llms.txt This Java example demonstrates how to configure PDF document metadata, including title, author, keywords, and creation date, as well as custom properties and XMP metadata using iText. ```java import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfDocumentInfo; import com.itextpdf.kernel.pdf.PdfVersion; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.WriterProperties; import com.itextpdf.kernel.xmp.XMPMeta; import com.itextpdf.kernel.xmp.XMPMetaFactory; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; public class MetadataExample { public static void main(String[] args) throws Exception { // Configure writer properties WriterProperties writerProperties = new WriterProperties() .setPdfVersion(PdfVersion.PDF_2_0) .setFullCompressionMode(true); PdfWriter writer = new PdfWriter("metadata-example.pdf", writerProperties); PdfDocument pdf = new PdfDocument(writer); // Set document information PdfDocumentInfo info = pdf.getDocumentInfo(); info.setTitle("Sample Document with Metadata"); info.setAuthor("John Doe"); info.setSubject("Demonstrating PDF metadata"); info.setKeywords("iText, PDF, metadata, example"); info.setCreator("iText 7"); // Add custom metadata property info.setMoreInfo("Department", "Engineering"); info.setMoreInfo("Project", "Documentation"); // Set XMP metadata (required for PDF/A, recommended for all) pdf.setXmpMetadata(); // Create document content Document document = new Document(pdf); document.add(new Paragraph("This document has custom metadata.")); document.add(new Paragraph("Check the document properties in your PDF viewer.")); document.close(); } } ``` -------------------------------- ### OCR Images and Create PDF with ONNX Source: https://github.com/itext/itext-java/blob/develop/README.md Use ONNX models to perform OCR on images and generate a PDF. This example is part of the iText publications examples. ```Java com.itextpdf.samples.sandbox.pdfocr.onnx.PdfOcrOnnxExample ``` -------------------------------- ### Sign PDF with PQC Algorithms (Experimental) Source: https://github.com/itext/itext-java/blob/develop/README.md Demonstrates how to sign PDF documents using Post-Quantum Cryptography (PQC) algorithms. This feature is experimental and may require specific configurations. ```Java /* * This class is part of the iText samples and is licensed under the AGPL/Commercial license. * Notice that the AGPL version of iText(1.4.x) differs from the commercial version. * In order to use this form, you will need to have a commercial license * or be in compliance with the AGPL license. */ package com.itextpdf.samples.sandbox.signatures.pqc; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.samples.sandbox.BouncyCastlePqcV118Constants; import com.itextpdf.signatures.BouncyCastleDigest; import com.itextpdf.signatures.BouncyCastleSigner; import com.itextpdf.signatures.IExternalSignature; import com.itextpdf.signatures.PdfSigner; import com.itextpdf.signatures.SignatureUtil; import java.io.FileInputStream; import java.io.FileOutputStream; import java.security.KeyStore; import java.security.PrivateKey; import java.security.cert.Certificate; /** * Example demonstrating the usage of PQC algorithms for signing. */ public class PqcSign { public static final String SRC = "src/main/resources/pqc/empty.pdf"; public static final String DEST = "target/pqc-signed.pdf"; public static void main(String[] args) throws Exception { // Example using Kyber512 sign(BouncyCastlePqcV118Constants.KYBER512, "kyber512", "password"); // Example using Dilithium2 sign(BouncyCastlePqcV118Constants.DILITHIUM2, "dilithium2", "password"); } public static void sign(String pqcAlgorithm, String alias, String password) throws Exception { KeyStore ks = KeyStore.getInstance("PKCS12"); ks.load(new FileInputStream(BouncyCastlePqcV118Constants.KEYSTORE), password.toCharArray()); PrivateKey pk = (PrivateKey) ks.getKey(alias, password.toCharArray()); Certificate[] chain = ks.getCertificateChain(alias); // Signing PdfReader reader = new PdfReader(SRC); PdfWriter writer = new PdfWriter(DEST.replace(".pdf", "-" + pqcAlgorithm.toLowerCase() + ".pdf")); PdfDocument pdfDoc = new PdfDocument(reader, writer); PdfSigner pdfSigner = new PdfSigner(pdfDoc, writer, new BouncyCastleDigest()); // Set the signature properties pdfSigner.setSignerProperties(alias, chain, null, null, null, null, null, PdfSigner.CryptoStandard.CMS); // Set the PQC signature IExternalSignature pqs = new BouncyCastleSigner(pqcAlgorithm); pdfSigner.signDetached(pqs, null); pdfDoc.close(); // Verification SignatureUtil util = new SignatureUtil(DEST.replace(".pdf", "-" + pqcAlgorithm.toLowerCase() + ".pdf")); System.out.println("Signature verified: " + util.signatureCoversWholeDocument(0)); System.out.println("Signature valid: " + util.isSignatureValid(0)); } } ``` -------------------------------- ### Standard GPL Notice for Source Files Source: https://github.com/itext/itext-java/blob/develop/io/src/test/resources/com/itextpdf/io/font/GNU General Public License.txt Include this notice at the beginning of each source file to clearly state the program's name, copyright, and licensing terms under the GPL. Ensure it points to the full license text. ```text Copyright (C) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . ``` -------------------------------- ### Add Multiple Images to PDF in Java Source: https://github.com/itext/itext-java/blob/develop/README.md This example shows how to embed multiple images into a PDF document using iText. ```java package com.itextpdf.samples.sandbox.images; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Image; import java.io.IOException; public class MultipleImages { public static final String DEST = "./out/multiple_images.pdf"; public static final String IMG1 = "./src/main/resources/img/itext.png"; public static final String IMG2 = "./src/main/resources/img/itext.png"; public static void main(String[] args) throws IOException { // Create a PdfWriter object PdfWriter writer = new PdfWriter(DEST); // Create a PdfDocument object PdfDocument pdf = new PdfDocument(writer); // Create a Document object Document document = new Document(pdf); // Create Image objects Image image1 = new Image(new com.itextpdf.io.image.ImageDataFactory().create(IMG1)); Image image2 = new Image(new com.itextpdf.io.image.ImageDataFactory().create(IMG2)); // Add the images to the document document.add(image1); document.add(image2); // Close the document document.close(); } } ``` -------------------------------- ### Redacting Content with iText PDFSweep Source: https://github.com/itext/itext-java/blob/develop/README.md This example demonstrates how to redact content from PDF documents using the iText PDFSweep library in Java. ```Java https://kb.itextpdf.com/home/it7kb/examples/removing-content-with-pdfsweep ``` -------------------------------- ### Create a Simple Table in Java Source: https://github.com/itext/itext-java/blob/develop/README.md Demonstrates how to create and populate a basic table within a PDF document using iText. ```java package com.itextpdf.samples.sandbox.tables; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Cell; import com.itextpdf.layout.element.Table; import com.itextpdf.layout.property.TextAlignment; import java.io.IOException; public class SimpleTable9 { public static final String DEST = "./out/simple_table9.pdf"; public static void main(String[] args) throws IOException { // Create a PdfWriter object PdfWriter writer = new PdfWriter(DEST); // Create a PdfDocument object PdfDocument pdf = new PdfDocument(writer); // Create a Document object Document document = new Document(pdf); // Create a Table object Table table = new Table(3); // Add cells to the table table.addCell(new Cell().add("Header 1").setTextAlignment(TextAlignment.CENTER)); table.addCell(new Cell().add("Header 2").setTextAlignment(TextAlignment.CENTER)); table.addCell(new Cell().add("Header 3").setTextAlignment(TextAlignment.CENTER)); table.addCell("Row 1, Col 1"); table.addCell("Row 1, Col 2"); table.addCell("Row 1, Col 3"); table.addCell("Row 2, Col 1"); table.addCell("Row 2, Col 2"); table.addCell("Row 2, Col 3"); // Add the table to the document document.add(table); // Close the document document.close(); } } ``` -------------------------------- ### Build iText Community with Maven Source: https://github.com/itext/itext-java/blob/develop/BUILDING.md Run this command to build the iText Community jars. Tests are skipped by default. Output is redirected to mvn.log and mvn-error.log. ```bash $ mvn clean install \ -Dmaven.test.skip=true \ > >(tee mvn.log) 2> >(tee mvn-error.log >&2) ``` -------------------------------- ### Create a Popup Annotation in Java Source: https://github.com/itext/itext-java/blob/develop/README.md This Java example shows how to create and move a popup annotation within a PDF document using iText. ```Java package com.itextpdf.samples.sandbox.annotations; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfName; import com.itextpdf.kernel.pdf.PdfObject; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.annot.PdfAnnotation; import com.itextpdf.kernel.pdf.annot.PdfTextMarkupAnnotation; import com.itextpdf.kernel.pdf.annot.PdfTextAnnotation; import com.itextpdf.kernel.pdf.annot.PdfPopupAnnotation; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import com.itextpdf.layout.properties.TextAlignment; import java.io.File; public class MovePopup { public static final String DEST = "./sandbox/annotations/move_popup.pdf"; public static void main(String[] args) throws Exception { File file = new File(DEST); file.getParentFile().mkdirs(); new MovePopup().manipulatePdf(DEST); } protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); Document document = new Document(pdfDoc); Paragraph paragraph = new Paragraph("This is a paragraph with a popup annotation."); paragraph.setTextAlignment(TextAlignment.CENTER); document.add(paragraph); // Create a popup annotation PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(new com.itextpdf.kernel.geom.Rectangle(100, 100, 200, 200)); popupAnnotation.setOpen(true); popupAnnotation.setParent(new PdfAnnotation(new com.itextpdf.kernel.geom.Rectangle(100, 100, 200, 200))); // Placeholder for parent annotation popupAnnotation.setContents("This is the popup content."); // Add the popup annotation to the document document.addAnnotation(popupAnnotation); document.close(); } } ``` -------------------------------- ### Create and Fill PDF Forms Source: https://context7.com/itext/itext-java/llms.txt Demonstrates creating a new PDF with form fields and then filling an existing form. Ensure the input PDF exists when filling. ```java import com.itextpdf.forms.PdfAcroForm; import com.itextpdf.forms.fields.PdfFormCreator; import com.itextpdf.forms.fields.PdfFormField; import com.itextpdf.forms.fields.PdfTextFormField; import com.itextpdf.forms.fields.TextFormFieldBuilder; import com.itextpdf.kernel.geom.Rectangle; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import java.util.Map; public class FormsExample { public static void main(String[] args) throws Exception { // Create a new PDF with form fields createFormPdf(); // Fill an existing form fillFormPdf(); } static void createFormPdf() throws Exception { PdfDocument pdf = new PdfDocument(new PdfWriter("form-template.pdf")); pdf.addNewPage(); // Get or create the AcroForm PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true); // Create text field for name PdfTextFormField nameField = new TextFormFieldBuilder(pdf, "name") .setWidgetRectangle(new Rectangle(100, 700, 200, 20)) .createText(); nameField.setValue("Enter your name"); form.addField(nameField); // Create text field for email PdfTextFormField emailField = new TextFormFieldBuilder(pdf, "email") .setWidgetRectangle(new Rectangle(100, 650, 200, 20)) .createText(); emailField.setValue(""); form.addField(emailField); pdf.close(); } static void fillFormPdf() throws Exception { // Open existing form PDF PdfDocument pdf = new PdfDocument( new PdfReader("form-template.pdf"), new PdfWriter("form-filled.pdf") ); PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, false); // Get all fields Map fields = form.getAllFormFields(); // Fill fields by name PdfFormField nameField = fields.get("name"); if (nameField != null) { nameField.setValue("John Doe"); } PdfFormField emailField = fields.get("email"); if (emailField != null) { emailField.setValue("john.doe@example.com"); } // Optional: Flatten form (convert fields to static content) form.flattenFields(); pdf.close(); } } ``` -------------------------------- ### Add Watermark to PDF in Java Source: https://github.com/itext/itext-java/blob/develop/README.md Learn how to add a watermark to a PDF document using iText in Java. This example demonstrates the Watermarking class. ```Java package com.itextpdf.samples.sandbox.events; import com.itextpdf.kernel.events.Event; import com.itextpdf.kernel.events.IEventHandler; import com.itextpdf.kernel.events.PdfDocumentEvent; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.layout.Document; import com.itextpdf.layout.element.Paragraph; import java.io.File; public class Watermarking { public static final String DEST = "./sandbox/events/watermarking.pdf"; public static void main(String[] args) throws Exception { File file = new File(DEST); file.getParentFile().mkdirs(); new Watermarking().manipulatePdf(DEST); } protected void manipulatePdf(String dest) throws Exception { PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest)); pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE, new WatermarkEventHandler()); Document document = new Document(pdfDoc); document.add(new Paragraph("This is a sample PDF document with a watermark.")); document.close(); } static class WatermarkEventHandler implements IEventHandler { @Override public void handleEvent(Event event) { PdfDocumentEvent docEvent = (PdfDocumentEvent) event; PdfDocument pdfDoc = docEvent.getDocument(); PdfCanvas underContent = pdfDoc.getUnderContent(); underContent.saveState(); underContent.setColor(java.awt.Color.GRAY, true); underContent.beginText(); underContent.setFontAndSize(pdfDoc.getDefaultFont(), 30); underContent.setTextMatrix(150, 300); underContent.showText("DRAFT"); underContent.endText(); underContent.restoreState(); } } } ``` -------------------------------- ### Flatten Annotations in Java Source: https://github.com/itext/itext-java/blob/develop/README.md Provides an example of how to flatten annotations in a PDF document using iText. Flattening makes annotations a permanent part of the document content. ```Java package com.itextpdf.samples.sandbox.annotations; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.annot.PdfAnnotation; import com.itextpdf.kernel.pdf.canvas.PdfCanvas; import com.itextpdf.kernel.pdf.xobject.PdfFormXObject; import java.io.IOException; public class FlattenAnnotations { public static final String SRC = "./src/main/resources/pdfs/annotation_example.pdf"; public static final String DEST = "./cmpfiles/sandbox/annotations/cmp_flatten_annotations.pdf"; public static void main(String[] args) throws IOException { PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST)); for (int i = 1; i <= pdfDoc.getNumberOfPages(); i++) { com.itextpdf.kernel.pdf.PdfPage page = pdfDoc.getPage(i); for (PdfAnnotation annot : page.getAnnotations()) { // Flatten the annotation annot.flatten(); } } pdfDoc.close(); } } ``` -------------------------------- ### GraalVM Native Image Configuration for Brotli Source: https://github.com/itext/itext-java/blob/develop/brotli-compressor/README.md Include necessary resources and initialize the Brotli4jLoader at runtime when building a GraalVM native image to ensure the native Brotli library is available. ```bash #add all the runtimes you are deploying to, iText test env does these so we have added these as a example -H:IncludeResources=lib/windows-x86_64/brotli.dll -H:IncludeResources=lib/linux-aarch64/libbrotli.so --initialize-at-run-time=com.aayushatharva.brotli4j.Brotli4jLoader ```