Filling out a form in multiple passes

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

6th November 2015
admin-marketing

Switch code for this example

FillFormFieldOrder.java
  1. /*
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/29633035/change-acrofields-order-in-existing-pdf-with-itext
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.pdf.AcroFields;
  9. import com.itextpdf.text.pdf.PdfReader;
  10. import com.itextpdf.text.pdf.PdfStamper;
  11.  
  12. import java.io.ByteArrayOutputStream;
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class FillFormFieldOrder {
  20.    
  21.     public static final String SRC = "resources/pdfs/calendar_example.pdf";
  22.     public static final String DEST = "results/acroforms/calendar_filled.pdf";
  23.    
  24.     public static void main(String[] args) throws DocumentException, IOException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new FillFormFieldOrder().manipulatePdf(SRC, DEST);
  28.     }
  29.    
  30.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  31.         go2(go1(src), dest);
  32.     }
  33.    
  34.     public byte[] go1(String src) throws IOException, DocumentException {
  35.         PdfReader reader = new PdfReader(src);
  36.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  37.         PdfStamper stamper = new PdfStamper(reader, baos);
  38.         AcroFields form = stamper.getAcroFields();
  39.         form.setField("sunday_1", "1");
  40.         form.setField("sunday_2", "2");
  41.         form.setField("sunday_3", "3");
  42.         form.setField("sunday_4", "4");
  43.         form.setField("sunday_5", "5");
  44.         form.setField("sunday_6", "6");
  45.         stamper.setFormFlattening(true);
  46.         stamper.partialFormFlattening("sunday_1");
  47.         stamper.partialFormFlattening("sunday_2");
  48.         stamper.partialFormFlattening("sunday_3");
  49.         stamper.partialFormFlattening("sunday_4");
  50.         stamper.partialFormFlattening("sunday_5");
  51.         stamper.partialFormFlattening("sunday_6");
  52.         stamper.close();
  53.         reader.close();
  54.         return baos.toByteArray();
  55.     }
  56.    
  57.     public void go2(byte[] src, String dest) throws IOException, DocumentException {
  58.         PdfReader reader = new PdfReader(src);
  59.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  60.         AcroFields form = stamper.getAcroFields();
  61.         form.setField("sunday_1_notes", "It's Sunday today, let's go to the sea");
  62.         form.setField("sunday_2_notes", "It's Sunday today, let's go to the park");
  63.         form.setField("sunday_3_notes", "It's Sunday today, let's go to the beach");
  64.         form.setField("sunday_4_notes", "It's Sunday today, let's go to the woods");
  65.         form.setField("sunday_5_notes", "It's Sunday today, let's go to the lake");
  66.         form.setField("sunday_6_notes", "It's Sunday today, let's go to the river");
  67.         stamper.setFormFlattening(true);
  68.         stamper.close();
  69.         reader.close();
  70.     }
  71. }
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