Change the position of a field

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

31st May 2016
iText PDF

Switch code for this example

ChangeFieldSize.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/24404413/increase-width-of-acrofieldsitextsharp
  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.kernel.pdf.*;
  17. import com.itextpdf.kernel.pdf.annot.PdfWidgetAnnotation;
  18. import com.itextpdf.samples.GenericTest;
  19. import com.itextpdf.test.annotations.type.SampleTest;
  20.  
  21. import org.junit.experimental.categories.Category;
  22.  
  23. import java.io.File;
  24.  
  25. @Category(SampleTest.class)
  26. public class ChangeFieldSize extends GenericTest {
  27.     public static final String DEST = "./target/test/resources/sandbox/acroforms/change_field_size.pdf";
  28.     public static final String SRC = "./src/test/resources/pdfs/form.pdf";
  29.  
  30.     public static void main(String[] args) throws Exception {
  31.         File file = new File(DEST);
  32.         file.getParentFile().mkdirs();
  33.         new ChangeFieldSize().manipulatePdf(DEST);
  34.     }
  35.  
  36.     @Override
  37.     protected void manipulatePdf(String dest) throws Exception {
  38.         PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  39.         PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true);
  40.  
  41.         String value = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" +
  42.                 "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  43.         PdfFormField field = form.getField("Name");
  44.         PdfWidgetAnnotation widgetAnnotation = field.getWidgets().get(0);
  45.         PdfArray annotationRect = widgetAnnotation.getRectangle();
  46.         annotationRect.set(2, new PdfNumber(annotationRect.getAsNumber(2).floatValue() + 20f));
  47.         field.setValue(value);
  48.         form.getField("Company").setValue(value);
  49.  
  50.         pdfDoc.close();
  51.     }
  52. }
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