Change the content of a button

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

6th November 2015
admin-marketing

Switch code for this example

ChangeButton.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/26804092/itext-button-resize-affects-label
  4.  * It will only work starting with iText 5.5.4
  5.  */
  6. package sandbox.acroforms;
  7.  
  8. import com.itextpdf.text.DocumentException;
  9. import com.itextpdf.text.Rectangle;
  10. import com.itextpdf.text.pdf.AcroFields;
  11. import com.itextpdf.text.pdf.PdfReader;
  12. import com.itextpdf.text.pdf.PdfStamper;
  13. import com.itextpdf.text.pdf.PushbuttonField;
  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 ChangeButton {
  22.  
  23.     public static final String SRC = "resources/pdfs/hello_button.pdf";
  24.     public static final String DEST = "results/acroforms/hello_button_changed.pdf";
  25.  
  26.     public static void main(String[] args) throws DocumentException, IOException {
  27.         File file = new File(DEST);
  28.         file.getParentFile().mkdirs();
  29.         new ChangeButton().manipulatePdf(SRC, DEST);
  30.     }
  31.  
  32.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  33.         PdfReader reader = new PdfReader(src);
  34.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  35.         AcroFields form = stamper.getAcroFields();
  36.         PushbuttonField button = form.getNewPushbuttonFromField("Test");
  37.         Rectangle box = button.getBox();
  38.         box.setRight(box.getRight() + 72);  // Increase width by 1"
  39.         button.setBox(box);
  40.         button.setText("Print Amended");
  41.         form.replacePushbuttonField("Test", button.getField());
  42.         stamper.close();
  43.     }
  44. }
HideButton.java
  1. /**
  2.  * This example was written by Bruno Lowagie in answer to the following question:
  3.  * http://stackoverflow.com/questions/38900816
  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.PdfAnnotation;
  10. import com.itextpdf.text.pdf.PdfReader;
  11. import com.itextpdf.text.pdf.PdfStamper;
  12.  
  13. import java.io.File;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import sandbox.WrapToTest;
  17.  
  18. @WrapToTest
  19. public class HideButton {
  20.  
  21.     public static final String SRC = "resources/pdfs/hello_button.pdf";
  22.     public static final String DEST = "results/acroforms/hello_button_hidden.pdf";
  23.  
  24.     public static void main(String[] args) throws DocumentException, IOException {
  25.         File file = new File(DEST);
  26.         file.getParentFile().mkdirs();
  27.         new HideButton().manipulatePdf(SRC, DEST);
  28.     }
  29.  
  30.     public void manipulatePdf(String src, String dest) throws DocumentException, IOException {
  31.         PdfReader reader = new PdfReader(src);
  32.         PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
  33.         AcroFields form = stamper.getAcroFields();
  34.         form.setFieldProperty("Test", "setflags", PdfAnnotation.FLAGS_HIDDEN, null);
  35.         stamper.close();
  36.     }
  37. }
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