Filling out a form in multiple passes

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

31st May 2016
iText PDF

Switch code for this example

FillFormFieldOrder.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/29633035/change-acrofields-order-in-existing-pdf-with-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfFormField;
  16. import com.itextpdf.io.source.ByteArrayOutputStream;
  17. import com.itextpdf.io.source.RandomAccessSourceFactory;
  18. import com.itextpdf.kernel.pdf.PdfDocument;
  19. import com.itextpdf.kernel.pdf.PdfReader;
  20. import com.itextpdf.kernel.pdf.PdfWriter;
  21. import com.itextpdf.kernel.pdf.ReaderProperties;
  22. import com.itextpdf.layout.Document;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25. import org.junit.experimental.categories.Category;
  26.  
  27. import java.io.File;
  28. import java.io.IOException;
  29. import java.util.Map;
  30.  
  31. @Category(SampleTest.class)
  32. public class FillFormFieldOrder extends GenericTest {
  33.     public static final String DEST = "./target/test/resources/sandbox/acroforms/fill_form_field_order.pdf";
  34.     public static final String SRC = "./src/test/resources/pdfs/calendar_example.pdf";
  35.  
  36.     public static void main(String[] args) throws Exception {
  37.         File file = new File(DEST);
  38.         file.getParentFile().mkdirs();
  39.         new FillFormFieldOrder().manipulatePdf(DEST);
  40.     }
  41.  
  42.     @Override
  43.     protected void manipulatePdf(String dest) throws Exception {
  44.         go2(go1());
  45.     }
  46.  
  47.     public byte[] go1() throws IOException {
  48.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  49.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(baos));
  50.         Document doc = new Document(pdfDoc);
  51.  
  52.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, false);
  53.         Map fields = form.getFormFields();
  54.  
  55.         fields.get("sunday_1").setValue("1");
  56.         fields.get("sunday_2").setValue("2");
  57.         fields.get("sunday_3").setValue("3");
  58.         fields.get("sunday_4").setValue("4");
  59.         fields.get("sunday_5").setValue("5");
  60.         fields.get("sunday_6").setValue("6");
  61.  
  62.         form.partialFormFlattening("sunday_1");
  63.         form.partialFormFlattening("sunday_2");
  64.         form.partialFormFlattening("sunday_3");
  65.         form.partialFormFlattening("sunday_4");
  66.         form.partialFormFlattening("sunday_5");
  67.         form.partialFormFlattening("sunday_6");
  68.  
  69.         form.flattenFields();
  70.         doc.close();
  71.  
  72.         return baos.toByteArray();
  73.     }
  74.  
  75.     public void go2(byte[] src) throws IOException {
  76.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(new RandomAccessSourceFactory().createSource(src),
  77.                 new ReaderProperties()), new PdfWriter(DEST));
  78.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  79.  
  80.         Map fields = form.getFormFields();
  81.         fields.get("sunday_1_notes").setValue("It's Sunday today, let's go to the sea").setBorderWidth(0);
  82.         fields.get("sunday_2_notes").setValue("It's Sunday today, let's go to the park").setBorderWidth(0);
  83.         fields.get("sunday_3_notes").setValue("It's Sunday today, let's go to the beach").setBorderWidth(0);
  84.         fields.get("sunday_4_notes").setValue("It's Sunday today, let's go to the woods").setBorderWidth(0);
  85.         fields.get("sunday_5_notes").setValue("It's Sunday today, let's go to the lake").setBorderWidth(0);
  86.         fields.get("sunday_6_notes").setValue("It's Sunday today, let's go to the river").setBorderWidth(0);
  87.  
  88.         form.flattenFields();
  89.         pdfDoc.close();
  90.     }
  91. }
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