You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2019/08/29 16:27:06 UTC

svn commit: r1866082 - /pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java

Author: tilman
Date: Thu Aug 29 16:27:06 2019
New Revision: 1866082

URL: http://svn.apache.org/viewvc?rev=1866082&view=rev
Log:
PDFBOX-4636: build map of widget annotations from the pages if a widget annotation has no /P entry

Modified:
    pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java

Modified: pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1866082&r1=1866081&r2=1866082&view=diff
==============================================================================
--- pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java (original)
+++ pdfbox/branches/issue4569/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java Thu Aug 29 16:27:06 2019
@@ -812,7 +812,7 @@ public final class PDAcroForm implements
         }
     }
     
-    private Map<COSDictionary,Map<COSDictionary,PDAnnotationWidget>> buildPagesWidgetsMap(List<PDField> fields)
+    private Map<COSDictionary,Map<COSDictionary,PDAnnotationWidget>> buildPagesWidgetsMap(List<PDField> fields) throws IOException
     {
         Map<COSDictionary,Map<COSDictionary,PDAnnotationWidget>> pagesAnnotationsMap = new HashMap<>();
         boolean hasMissingPageRef = false;
@@ -822,20 +822,10 @@ public final class PDAcroForm implements
             List<PDAnnotationWidget> widgets = field.getWidgets();
             for (PDAnnotationWidget widget : widgets)
             {
-                PDPage pageForWidget = widget.getPage();
-                if (pageForWidget != null)
+                PDPage page = widget.getPage();
+                if (page != null)
                 {
-                    if (pagesAnnotationsMap.get(pageForWidget.getCOSObject()) == null)
-                    {
-                        Map<COSDictionary,PDAnnotationWidget> widgetsForPage = new HashMap<>();
-                        widgetsForPage.put(widget.getCOSObject(), widget);
-                        pagesAnnotationsMap.put(pageForWidget.getCOSObject(), widgetsForPage);
-                    }
-                    else
-                    {
-                        Map<COSDictionary,PDAnnotationWidget> widgetsForPage = pagesAnnotationsMap.get(pageForWidget.getCOSObject());
-                        widgetsForPage.put(widget.getCOSObject(), widget);
-                    }
+                    fillPagesAnnotationMap(pagesAnnotationsMap, page, widget);
                 }
                 else
                 {
@@ -843,18 +833,45 @@ public final class PDAcroForm implements
                 }
             }
         }
-        
-        // TODO: if there is a widget with a missing page reference 
-        // we'd need to build the map reverse i.e. form the annotations to the 
-        // widget. But this will be much slower so will be omitted for now.
-        if (hasMissingPageRef)
+
+        if (!hasMissingPageRef)
         {
-            LOG.warn("There has been a widget with a missing page reference. Please report to the PDFBox project");
+            return pagesAnnotationsMap;
         }
-        
+
+        // If there is a widget with a missing page reference we need to build the map reverse i.e. 
+        // from the annotations to the widget.
+        LOG.warn("There has been a widget with a missing page reference, will check all page annotations");
+        for (PDPage page : document.getPages())
+        {
+            for (PDAnnotation annotation : page.getAnnotations())
+            {
+                if (annotation instanceof PDAnnotationWidget)
+                {
+                    fillPagesAnnotationMap(pagesAnnotationsMap, page, (PDAnnotationWidget) annotation);
+                }
+            }
+        }
+
         return pagesAnnotationsMap;
     }
 
+    private void fillPagesAnnotationMap(Map<COSDictionary, Map<COSDictionary, PDAnnotationWidget>> pagesAnnotationsMap,
+            PDPage page, PDAnnotationWidget widget)
+    {
+        if (pagesAnnotationsMap.get(page.getCOSObject()) == null)
+        {
+            Map<COSDictionary,PDAnnotationWidget> widgetsForPage = new HashMap<>();
+            widgetsForPage.put(widget.getCOSObject(), widget);
+            pagesAnnotationsMap.put(page.getCOSObject(), widgetsForPage);
+        }
+        else
+        {
+            Map<COSDictionary,PDAnnotationWidget> widgetsForPage = pagesAnnotationsMap.get(page.getCOSObject());
+            widgetsForPage.put(widget.getCOSObject(), widget);
+        }
+    }
+
     private void removeFields(List<PDField> fields)
     {
         for (PDField field : fields)