You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ms...@apache.org on 2016/04/01 14:34:09 UTC

svn commit: r1737365 - in /pdfbox/branches/2.0/pdfbox/src: main/java/org/apache/pdfbox/pdmodel/interactive/form/ test/java/org/apache/pdfbox/pdmodel/interactive/form/ test/resources/org/apache/pdfbox/pdmodel/interactive/form/

Author: msahyoun
Date: Fri Apr  1 12:34:09 2016
New Revision: 1737365

URL: http://svn.apache.org/viewvc?rev=1737365&view=rev
Log:
PDFBOX-3301: lookup widgets page reference if there is no /P entry

Added:
    pdfbox/branches/2.0/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png   (with props)
Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
    pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java

Modified: pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java?rev=1737365&r1=1737364&r2=1737365&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java Fri Apr  1 12:34:09 2016
@@ -220,6 +220,11 @@ public final class PDAcroForm implements
         // the content stream to write to
         PDPageContentStream contentStream;
         
+        // Hold a reference between the annotations and the page they are on.
+        // This will only be used in case a PDAnnotationWidget doesn't contain
+        // a /P entry specifying the page it's on as the /P entry is optional
+        Map<COSDictionary, Integer> annotationToPageRef = null;
+        
         // Iterate over all form fields and their widgets and create a
         // FormXObject at the page content level from that
         for (PDField field : fields)
@@ -229,6 +234,18 @@ public final class PDAcroForm implements
                 if (widget.getNormalAppearanceStream() != null)
                 {
                     PDPage page = widget.getPage();
+
+                    // resolve the page from looking at the annotations
+                    if (widget.getPage() == null) {
+                    	if (annotationToPageRef == null) {
+                    		annotationToPageRef = buildAnnotationToPageRef();
+                    	}
+                    	Integer pageRef = annotationToPageRef.get(widget.getCOSObject());
+                    	if (pageRef != null) {
+                    		page = document.getPage((int) pageRef);
+                    	}
+                    }
+
                     if (!isContentStreamWrapped)
                     {
                         contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true);
@@ -616,4 +633,23 @@ public final class PDAcroForm implements
     {
         dictionary.setFlag(COSName.SIG_FLAGS, FLAG_APPEND_ONLY, appendOnly);
     }
+    
+    private Map<COSDictionary, Integer> buildAnnotationToPageRef() {
+    	Map<COSDictionary, Integer> annotationToPageRef = new HashMap<COSDictionary, Integer>();
+    	
+    	int idx = 0;
+    	for (PDPage page : document.getPages()) {
+    		try {
+				for (PDAnnotation annotation : page.getAnnotations()) {
+					if (annotation instanceof PDAnnotationWidget) {
+						annotationToPageRef.put(annotation.getCOSObject(), idx);
+					}
+				}
+			} catch (IOException e) {
+				LOG.warn("Can't retriev annotations for page " + idx);
+			}
+    		idx++;
+    	}    	
+    	return annotationToPageRef;
+    }
 }

Modified: pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java?rev=1737365&r1=1737364&r2=1737365&view=diff
==============================================================================
--- pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java (original)
+++ pdfbox/branches/2.0/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java Fri Apr  1 12:34:09 2016
@@ -26,6 +26,7 @@ import java.io.IOException;
 
 import org.apache.pdfbox.cos.COSName;
 import org.apache.pdfbox.pdmodel.PDDocument;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationWidget;
 import org.apache.pdfbox.rendering.TestPDFToImage;
 import org.junit.After;
 import org.junit.Before;
@@ -101,6 +102,34 @@ public class PDAcroFormTest
         
     }
 
+    /*
+     * Same as above but remove the page reference from the widget annotation
+     * before doing the flatten() to ensure that the widgets page reference is properly looked up
+     * (PDFBOX-3301)
+     */
+    @Test
+    public void testFlattenWidgetNoRef() throws IOException
+    {
+        PDDocument testPdf = PDDocument.load(new File(IN_DIR, "AlignmentTests.pdf"));
+        PDAcroForm acroForm = testPdf.getDocumentCatalog().getAcroForm();
+        for (PDField field : acroForm.getFieldTree()) {
+        	for (PDAnnotationWidget widget : field.getWidgets()) {
+        		widget.getCOSObject().removeItem(COSName.P);
+        	}
+        }
+        testPdf.getDocumentCatalog().getAcroForm().flatten();
+        assertTrue(testPdf.getDocumentCatalog().getAcroForm().getFields().isEmpty());
+        File file = new File(OUT_DIR, "AlignmentTests-flattened-noRef.pdf");
+        testPdf.save(file);
+        // compare rendering
+        TestPDFToImage testPDFToImage = new TestPDFToImage(TestPDFToImage.class.getName());
+        if (!testPDFToImage.doTestFile(file, IN_DIR.getAbsolutePath(), OUT_DIR.getAbsolutePath()))
+        {
+            // don't fail, rendering is different on different systems, result must be viewed manually
+            System.out.println("Rendering of " + file + " failed or is not identical to expected rendering in " + IN_DIR + " directory");
+        }
+    }
+    
     @After
     public void tearDown() throws IOException
     {

Added: pdfbox/branches/2.0/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png
URL: http://svn.apache.org/viewvc/pdfbox/branches/2.0/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png?rev=1737365&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/branches/2.0/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png
------------------------------------------------------------------------------
    svn:mime-type = image/png