[ https://issues.apache.org/jira/browse/PDFBOX-5225?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17888530#comment-17888530 ]
Tilman Hausherr edited comment on PDFBOX-5225 at 10/11/24 6:48 AM: ------------------------------------------------------------------- After searching for the thread that started this all, I found out that I had already posted a more general workaround there: https://lists.apache.org/thread/rvhv6wq0fvz6rw53rjcw6gk0wcs4z80y However that solution isn't fully correct because it assumes that the /P entry is mandatory, but it isn't. Here's some improved code that sets the page first: {code:java} for (PDPage page : doc.getPages()) { for (PDAnnotation ann : page.getAnnotations()) { if (ann.getPage() == null) { ann.setPage(page); } } } for (PDField field : acroForm.getFieldTree()) { if (!(field instanceof PDTerminalField)) { continue; } PDTerminalField terminalField = (PDTerminalField) field; List<PDAnnotationWidget> widgets = terminalField.getWidgets(); boolean replaceWidgets = false; List<PDAnnotationWidget> newWidgets = new ArrayList<>(); for (PDAnnotationWidget widget : widgets) { if (widget.getPage() == null) { replaceWidgets = true; System.out.println("field " + terminalField.getFullyQualifiedName() + " has widget without page"); } else { newWidgets.add(widget); } } if (replaceWidgets) { terminalField.setWidgets(newWidgets); } } {code} was (Author: tilman): After searching for the thread that started this all, I found out that I had already posted a more general workaround there: https://lists.apache.org/thread/rvhv6wq0fvz6rw53rjcw6gk0wcs4z80y However that solution isn't fully correct because it assumes that the /P entry is mandatory, but it isn't. I'll see if I can improve that one, by looking whether a potential orphan widget occurs is in one of the pages. > Flattening removes all annotations when widget annotation has no page > --------------------------------------------------------------------- > > Key: PDFBOX-5225 > URL: https://issues.apache.org/jira/browse/PDFBOX-5225 > Project: PDFBox > Issue Type: Bug > Components: AcroForm > Affects Versions: 2.0.24 > Reporter: Tilman Hausherr > Priority: Major > Attachments: SourceFailure.pdf, screenshot-1.png > > > {code} > PDAcroForm acroForm = doc.getDocumentCatalog().getAcroForm(); > List<PDField> list = new ArrayList<>(); > list.add(acroForm.getField("VN_NAME")); > acroForm.flatten(list, true); > {code} > The code from buildPagesWidgetsMap that is run when there are widgets with > missing page references does not consider the field list. So all widgets end > up in the map instead of only those we care about. > !screenshot-1.png! -- This message was sent by Atlassian Jira (v8.20.10#820010) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@pdfbox.apache.org For additional commands, e-mail: dev-h...@pdfbox.apache.org