pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1737364 - in /pdfbox/trunk/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/
Date Fri, 01 Apr 2016 12:32:05 GMT
Author: msahyoun
Date: Fri Apr  1 12:32:05 2016
New Revision: 1737364

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

Added:
    pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png
  (with props)
Modified:
    pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
    pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.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=1737364&r1=1737363&r2=1737364&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
Fri Apr  1 12:32:05 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/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java?rev=1737364&r1=1737363&r2=1737364&view=diff
==============================================================================
--- pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
(original)
+++ pdfbox/trunk/pdfbox/src/test/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroFormTest.java
Fri Apr  1 12:32:05 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,35 @@ 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/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png
URL: http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/test/resources/org/apache/pdfbox/pdmodel/interactive/form/AlignmentTests-flattened-noRef.pdf-1.png?rev=1737364&view=auto
==============================================================================
Binary file - no diff available.

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



Mime
View raw message