Flattening a form

This is a code example of iText PDF, discover more.

31st May 2016
admin-marketing

Switch code for this example

CheckBoxFlatten.java
  1. /*
  2.  
  3.     This file is part of the iText (R) project.
  4.     Copyright (c) 1998-2016 iText Group NV
  5.  
  6. */
  7.  
  8. /**
  9.  * Example written by Bruno Lowagie in answer to:
  10.  * http://stackoverflow.com/questions/34439850
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.kernel.pdf.PdfDocument;
  15. import com.itextpdf.kernel.pdf.PdfReader;
  16. import com.itextpdf.kernel.pdf.PdfWriter;
  17. import com.itextpdf.forms.PdfAcroForm;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import java.io.File;
  22.  
  23. import org.junit.experimental.categories.Category;
  24.  
  25. @Category(SampleTest.class)
  26. public class CheckBoxFlatten extends GenericTest {
  27.     public static final String DEST
  28.             = "./target/test/resources/sandbox/acroforms/checkbox_flatten.pdf";
  29.     public static final String SRC
  30.             = "./src/test/resources/pdfs/checkboxes.pdf";
  31.  
  32.     public static void main(String[] args) throws Exception {
  33.         File file = new File(DEST);
  34.         file.getParentFile().mkdirs();
  35.         new CheckBoxFlatten().manipulatePdf(DEST);
  36.     }
  37.  
  38.     @Override
  39.     protected void manipulatePdf(String dest) throws Exception {
  40.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  41.         PdfAcroForm.getAcroForm(pdfDoc, true).flattenFields();
  42.         pdfDoc.close();
  43.     }
  44. }
Contact

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now