Adding fields to an existing form

25th October 2015
admin-marketing

Switch code for this example

AddField.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/27206327/itext-add-new-acrofields-form-feilds-in-to-a-pdf-using-itext
  4.  */
  5. package sandbox.acroforms;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.GrayColor;
  10. import com.itextpdf.text.pdf.PdfAction;
  11. import com.itextpdf.text.pdf.PdfFormField;
  12. import com.itextpdf.text.pdf.PdfReader;
  13. import com.itextpdf.text.pdf.PdfStamper;
  14. import com.itextpdf.text.pdf.PushbuttonField;
  15.  
  16. import java.io.File;
  17. import java.io.FileOutputStream;
  18. import java.io.IOException;
  19. import sandbox.WrapToTest;
  20.  
  21. @WrapToTest
  22. public class AddField {
  23.  
  24.     public static final String SRC = "resources/pdfs/form.pdf";
  25.     public static final String DEST = "results/acroforms/field_added.pdf";
  26.    
  27.  
  28.     public static void main(String[] args) throws DocumentException, IOException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new AddField().manipulatePdf(SRC, DEST);
  32.     }
  33.  
  34.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  35.         PdfReader reader = new PdfReader(src);
  36.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  37.         PushbuttonField button = new PushbuttonField(
  38.             stamper.getWriter(), new Rectangle(36, 700, 72, 730), "post");
  39.         button.setText("POST");
  40.         button.setBackgroundColor(new GrayColor(0.7f));
  41.         button.setVisibility(PushbuttonField.VISIBLE_BUT_DOES_NOT_PRINT);
  42.         PdfFormField submit = button.getField();
  43.         submit.setAction(PdfAction.createSubmitForm(
  44.             "http://itextpdf.com:8180/book/request", null,
  45.             PdfAction.SUBMIT_HTML_FORMAT | PdfAction.SUBMIT_COORDINATES));
  46.         stamper.addAnnotation(submit, 1);
  47.         stamper.close();
  48.     }
  49. }
AddFieldAndKids.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to:
  3.  * http://stackoverflow.com/questions/28418555/remove-page-reference-from-annotation0
  4.  */
  5. package sandbox.stamper;
  6.  
  7. import com.itextpdf.text.DocumentException;
  8. import com.itextpdf.text.Rectangle;
  9. import com.itextpdf.text.pdf.PdfFormField;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12. import com.itextpdf.text.pdf.PdfWriter;
  13. import com.itextpdf.text.pdf.TextField;
  14.  
  15. import java.io.File;
  16. import java.io.FileOutputStream;
  17. import java.io.IOException;
  18. import sandbox.WrapToTest;
  19.  
  20. @WrapToTest
  21. public class AddFieldAndKids {
  22.  
  23.     public static final String SRC = "resources/pdfs/hello.pdf";
  24.     public static final String DEST = "results/stamper/hello_with_field_kids.pdf";
  25.  
  26.     public static void main(String[] args) throws IOException, DocumentException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new AddFieldAndKids().manipulatePdf(SRC, DEST);
  30.     }
  31.  
  32.     public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  35.         PdfWriter writer = stamper.getWriter();
  36.         PdfFormField personal = PdfFormField.createEmpty(writer);
  37.         personal.setFieldName("personal");
  38.         TextField name = new TextField(writer, new Rectangle(36, 760, 144, 790), "name");
  39.         PdfFormField personal_name = name.getTextField();
  40.         personal.addKid(personal_name);
  41.         TextField password = new TextField(writer, new Rectangle(150, 760, 450, 790), "password");
  42.         PdfFormField personal_password = password.getTextField();
  43.         personal.addKid(personal_password);
  44.         stamper.addAnnotation(personal, 1);
  45.         stamper.close();
  46.         reader.close();
  47.     }
  48. }
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