Search code examples
javapdfdocumentitext7

Save as print to pdf option using Java 8


I am watermarking pdf document using itext7 library. it is preserving layers and shows one of its signature invalid. I want to flatten the created document.

When i tried saving the document manually using Adobe print option, it flattens all signature and makes the document as valid document. Same functionality i want with java program.

Is there any way using java program, we can flatten pdf document?


Solution

  • According to your tag selection you appear to be using iText 7 for Java.

    How to flatten a PDF AcroForm form using iText 7 is explained in the iText 7 knowledge base example Flattening a form. The pivotal code is:

    PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
    PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
    
    // If no fields have been explicitly included, then all fields are flattened.
    // Otherwise only the included fields are flattened.
    form.flattenFields();
    
    pdfDoc.close();
    

    (https://kb.itextpdf.com/home/it7kb/examples/flattening-a-form visited 2021-05-24)