Change the position of a field

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

6th November 2015
admin-marketing

Switch code for this example

ChangeFieldSize.java
  1. /**
  2.  * Example written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/24404413/increase-width-of-acrofieldsitextsharp
  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.AcroFields.Item;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfNumber;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class ChangeFieldSize {
  24.  
  25.     public static final String SRC = "resources/pdfs/form.pdf";
  26.     public static final String DEST = "results/acroforms/field_changed.pdf";
  27.    
  28.  
  29.     public static void main(String[] args) throws DocumentException, IOException {
  30.         File file = new File(DEST);
  31.         file.getParentFile().mkdirs();
  32.         new ChangeFieldSize().manipulatePdf(SRC, DEST);
  33.     }
  34.  
  35.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  36.         PdfReader reader = new PdfReader(src);
  37.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  38.         AcroFields form = stamper.getAcroFields();
  39.         Item item = form.getFieldItem("Name");
  40.         PdfDictionary widget = item.getWidget(0);
  41.         PdfArray rect = widget.getAsArray(PdfName.RECT);
  42.         rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() + 20f));
  43.         String value = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
  44.         form.setField("Name", value);
  45.         form.setField("Company", value);
  46.         stamper.close();
  47.     }
  48. }
ChangeFieldPosition.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/22258598/itextsharp-move-acrofield
  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.AcroFields.Item;
  10. import com.itextpdf.text.pdf.PdfArray;
  11. import com.itextpdf.text.pdf.PdfDictionary;
  12. import com.itextpdf.text.pdf.PdfName;
  13. import com.itextpdf.text.pdf.PdfNumber;
  14. import com.itextpdf.text.pdf.PdfReader;
  15. import com.itextpdf.text.pdf.PdfStamper;
  16.  
  17. import java.io.File;
  18. import java.io.FileOutputStream;
  19. import java.io.IOException;
  20. import sandbox.WrapToTest;
  21.  
  22. @WrapToTest
  23. public class ChangeFieldPosition {
  24.  
  25.     public static final String SRC = "resources/pdfs/state.pdf";
  26.     public static final String DEST = "results/acroforms/field_moved.pdf";
  27.  
  28.     public static void main(String[] args) throws DocumentException, IOException {
  29.         File file = new File(DEST);
  30.         file.getParentFile().mkdirs();
  31.         new ChangeFieldPosition().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.         AcroFields form = stamper.getAcroFields();
  38.         Item item = form.getFieldItem("timezone2");
  39.         PdfDictionary widget = item.getWidget(0);
  40.         PdfArray rect = widget.getAsArray(PdfName.RECT);
  41.         rect.set(2, new PdfNumber(rect.getAsNumber(2).floatValue() - 10f));
  42.         stamper.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