I added a textfield with several kids similar as described here. Did that to use the autofill functionality of PDF... Now my question is how am I able to remove the page reference from the parent element? The data field should not contain a parent reference since it is not related to any page. The widgets should contain those (which I added there however I can't remove the parent /P page reference)
I tried
PdfFormField parent = PdfFormField.createTextField(stamper.getWriter(), false, false, 0);
parent.setFieldName(fieldName);
for (int page = 1; page <= pages; page++) {
TextField textField = new TextField(stamper.getWriter(), new Rectangle(560, 600, 590, 800), null);
PdfFormField pff = textField.getTextField();
parent.addKid(pff);
// add widget to each page
pff.setPlaceInPage(page);
//iText workarounds
field.put(PdfName.P, stamper.getWriter().getPageReference(page));
field.remove(PdfName.FF);
field.remove(PdfName.FT);
}
//in addAnnotation() the page reference is written
stamper.addAnnotation(parent, 1);
//does not work
parent.remove(PdfName.P);
however it didn't work since I guess the page reference is already written. Is there a way to remove it afterwards?
Fact 1:
The
PdfFormFieldclass extends thePdfAnnotationclass, because most of the times, it is possible to merge a field dictionary with an annotation dictionary.In your case, you have a
PdfFormFieldthat is used as a hierarchical element and although that element is also an instance ofPdfAnnotation, it isn't. You can check this by using theisAnnotation()method. It will returnfalse.Fact 2:
When you add an annotation to an existing PDF, you have to use
PdfStamper'saddAnnotation()method that not only accepts an annotation object, but also a page number. If you didn't add a page number,PdfStamperwouldn't know where to add the annotation.When you add a real annotation,
PdfStamperwill add a/Pkey that refers to the page where the annotations is visualized.Fact 3:
There is no other way than using
addAnnotation()with an annotation and a page number when working withPdfStamper. When you add aPdfFormFieldobject that is not a real annotation, it won't be treated differently, hence a/Pentry will be added, although that doesn't really make sense (as you rightly point out in your question).Fact 4:
The
/Pentry is optional. For the widget annotations to be displayed correctly, it is sufficient that they appear in the/Annotsof a page.Conclusion:
iText shouldn't add a
/Pentry in case you add aPdfFormFieldthat is not a real annotation. Hence I have committed the following change: revision 6756I have tested this with the AddFieldAndKids example and it seems to work: the
/Pentry is no longer added.This solves your problem in a more robust way. You shouldn't try to remove something that shouldn't have been added in the first place.