pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From til...@apache.org
Subject svn commit: r1866085 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
Date Thu, 29 Aug 2019 16:27:21 GMT
Author: tilman
Date: Thu Aug 29 16:27:21 2019
New Revision: 1866085

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

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

Modified: pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1866085&r1=1866084&r2=1866085&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
(original)
+++ pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
Thu Aug 29 16:27:21 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)



Mime
View raw message