Search code examples
javapdfitextacrobat

Set visible property of fields in a PDF


I have a PDF template file with a button field on it. Let's say, the name of the button field in "Button1". Is it possible to hide this button from my java application using iText (v5.5)?


Solution

  • Please take a look at the HideButton example. In this example, we take a PDF named hello_button.java that has a button named "Test" (the gray area in the screen shot):

    enter image description here

    The field with name "Text" corresponds with one widget annotation. We can change the flags of this annotation like this:

    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    AcroFields form = stamper.getAcroFields();
    form.setFieldProperty("Test", "setflags", PdfAnnotation.FLAGS_HIDDEN, null);
    stamper.close();
    

    The result of this operation is a file named hello_button_hidden.pdf:

    enter image description here

    This is the iText 5 answer; the other answer was an iText 7 answer.