Skip to content Skip to sidebar Skip to footer

Itext - Java Android - Adding Fields To Existing Pdf

I'm trying to add interactive fields to an existing PDF, i use a PdfReader and a PdfStamper to do this, and when i open it on my tablet, everything is ok. However, when I want to o

Solution 1:

There actually are two issues in your code,

  1. you create the fields with empty names:

    cellFillFieldPage1.setCellEvent(newMyCellField("", 1));
    ...
    cellCheckBoxPage2.setCellEvent(newCheckboxCellEvent("", false, 2));
    

    First of all fields need to have a name. And furthermore, even if the empty name was an allowed choice for a name, two fields with the same name are considered to be two representations of the same abstract field which hardly works out if one is a text field and the other a checkbox field.

    As soon as you name those fields with non-empty, different names, you'll see that you have fields on Adobe Reader DC/Win: You can tab through them with the TAB key and change their values. Unfortunately, though, they are borderless and, therefore, hard to spot if empty. This is because

  2. you don't set border colors for your fields. iText, consequently, creates the form field appearance streams without borders.

    You can set the border color like this:

    ...
    finalTextFieldtextField=newTextField(writer, rectangle, fieldname);
    textField.setBorderColor(BaseColor.BLACK);
    ...
    RadioCheckFieldcheckbox=newRadioCheckField(writer, rect, name, "Yes");
    checkbox.setBorderColor(BaseColor.BLACK);
    ...
    

    Having done so, you get the fields with black borders...


If you wonder why different viewers (even by the same company with the same name) behave different here...

  1. Some PDF viewers recognize early that they couldn't properly handle fields with empty names and, therefore, don't even show them while other viewers recognize that later when trying to save or post the form, or don't recognize it at all, producing broken outputs.

  2. PDF viewers are allowed to ignore appearance streams of form fields (actually of annotations in general) and create their own appearances; thus, in the case at hand they may ignore the borderless appearance generated by iText and draw one with a visible border.

Post a Comment for "Itext - Java Android - Adding Fields To Existing Pdf"