Adding fields to an existing form

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

31st May 2016
admin-marketing

Switch code for this example

AddField.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 the following question:
  10.  * http://stackoverflow.com/questions/27206327/itext-add-new-acrofields-form-feilds-in-to-a-pdf-using-itext
  11.  */
  12. package com.itextpdf.samples.sandbox.acroforms;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfButtonFormField;
  16. import com.itextpdf.forms.fields.PdfFormField;
  17. import com.itextpdf.kernel.color.Color;
  18. import com.itextpdf.kernel.geom.Rectangle;
  19. import com.itextpdf.kernel.pdf.PdfDocument;
  20. import com.itextpdf.kernel.pdf.PdfReader;
  21. import com.itextpdf.kernel.pdf.PdfWriter;
  22. import com.itextpdf.kernel.pdf.action.PdfAction;
  23. import com.itextpdf.samples.GenericTest;
  24. import com.itextpdf.test.annotations.type.SampleTest;
  25.  
  26. import org.junit.experimental.categories.Category;
  27.  
  28. import java.io.File;
  29.  
  30. @Category(SampleTest.class)
  31. public class AddField extends GenericTest {
  32.     public static final String DEST = "./target/test/resources/sandbox/acroforms/add_field.pdf";
  33.     public static final String SRC = "./src/test/resources/pdfs/form.pdf";
  34.  
  35.     public static void main(String[] args) throws Exception {
  36.         File file = new File(DEST);
  37.         file.getParentFile().mkdirs();
  38.         new AddField().manipulatePdf(DEST);
  39.     }
  40.  
  41.     @Override
  42.     protected void manipulatePdf(String dest) throws Exception {
  43.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  44.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  45.  
  46.         PdfButtonFormField button = PdfFormField.createPushButton(pdfDoc, new Rectangle(36, 700, 36, 30), "post", "POST");
  47.         button.setBackgroundColor(Color.GRAY);
  48.         button.setValue("POST");
  49.         button.setAction(PdfAction.createSubmitForm("http://itextpdf.com:8180/book/request", null,
  50.                 PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
  51.         button.setVisibility(PdfFormField.VISIBLE_BUT_DOES_NOT_PRINT);
  52.  
  53.         form.addField(button);
  54.  
  55.         pdfDoc.close();
  56.     }
  57. }
AddFieldAndKids.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/28418555/remove-page-reference-from-annotation0
  11.  */
  12. package com.itextpdf.samples.sandbox.stamper;
  13.  
  14. import com.itextpdf.forms.PdfAcroForm;
  15. import com.itextpdf.forms.fields.PdfFormField;
  16. import com.itextpdf.forms.fields.PdfTextFormField;
  17. import com.itextpdf.kernel.geom.Rectangle;
  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.samples.GenericTest;
  22. import com.itextpdf.test.annotations.type.SampleTest;
  23.  
  24. import org.junit.experimental.categories.Category;
  25.  
  26. import java.io.File;
  27.  
  28. @Category(SampleTest.class)
  29. public class AddFieldAndKids extends GenericTest {
  30.     public static final String DEST = "./target/test/resources/sandbox/stamper/add_field_and_kids.pdf";
  31.     public static final String SRC = "./src/test/resources/pdfs/hello.pdf";
  32.  
  33.     public static void main(String[] args) throws Exception {
  34.         File file = new File(DEST);
  35.         file.getParentFile().mkdirs();
  36.         new AddExtraMargin().manipulatePdf(DEST);
  37.     }
  38.  
  39.     @Override
  40.     protected void manipulatePdf(String dest) throws Exception {
  41.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  42.  
  43.         PdfFormField personal = PdfFormField.createEmptyField(pdfDoc);
  44.         personal.setFieldName("personal");
  45.         PdfTextFormField name =
  46.                 PdfFormField.createText(pdfDoc, new Rectangle(36, 760, 108, 30), "name", "");
  47.         personal.addKid(name);
  48.         PdfTextFormField password =
  49.                 PdfFormField.createText(pdfDoc, new Rectangle(150, 760, 300, 30), "password", "");
  50.         personal.addKid(password);
  51.  
  52.         PdfAcroForm.getAcroForm(pdfDoc, true).addField(personal, pdfDoc.getFirstPage());
  53.         pdfDoc.close();
  54.     }
  55. }
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