pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From msahy...@apache.org
Subject svn commit: r1785594 - /pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.java
Date Mon, 06 Mar 2017 07:34:12 GMT
Author: msahyoun
Date: Mon Mar  6 07:34:12 2017
New Revision: 1785594

URL: http://svn.apache.org/viewvc?rev=1785594&view=rev
Log:
PDFBOX-3673: ensure that existing content streams pages are wrapped in a store/restore pair
per page

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=1785594&r1=1785593&r2=1785594&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
Mon Mar  6 07:34:12 2017
@@ -229,35 +229,24 @@ 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)
+        // preserve all non widget annotations
+        for (PDPage page : document.getPages())
         {
-            for (PDAnnotationWidget widget : field.getWidgets())
+            isContentStreamWrapped = false;
+            
+            List<PDAnnotation> annotations = new ArrayList<PDAnnotation>();
+            
+            for (PDAnnotation annotation: page.getAnnotations())
             {
-                if (!widget.isInvisible() && !widget.isHidden() && widget.getNormalAppearanceStream()
!= null)
+                if (!(annotation instanceof PDAnnotationWidget))
+                {
+                    annotations.add(annotation);                 
+                }
+                else if (!annotation.isInvisible() && !annotation.isHidden() &&
annotation.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(pageRef);
-                    	}
-                    }
-
                     if (!isContentStreamWrapped)
                     {
-                        contentStream = new PDPageContentStream(document, page, AppendMode.APPEND,
true);
+                        contentStream = new PDPageContentStream(document, page, AppendMode.APPEND,
true, true);
                         isContentStreamWrapped = true;
                     }
                     else
@@ -265,7 +254,7 @@ public final class PDAcroForm implements
                         contentStream = new PDPageContentStream(document, page, AppendMode.APPEND,
true);
                     }
                     
-                    PDAppearanceStream appearanceStream = widget.getNormalAppearanceStream();
+                    PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
                     
                     PDFormXObject fieldObject = new PDFormXObject(appearanceStream.getCOSObject());
                     
@@ -284,49 +273,35 @@ public final class PDAcroForm implements
                     
                     if (needsTranslation)
                     {
-                    	transformationMatrix.translate(widget.getRectangle().getLowerLeftX(),
-                                widget.getRectangle().getLowerLeftY());
-                    	transformed = true;
+                        transformationMatrix.translate(annotation.getRectangle().getLowerLeftX(),
+                                annotation.getRectangle().getLowerLeftY());
+                        transformed = true;
                     }
 
                     if (needsScaling)
                     {                    
-	                    PDRectangle bbox = appearanceStream.getBBox();
-	                    PDRectangle fieldRect = widget.getRectangle();
-	                    
-	                    if (bbox.getWidth() - fieldRect.getWidth() != 0 && bbox.getHeight()
- fieldRect.getHeight() != 0)
-	                    {
-	                    	float xScale = fieldRect.getWidth() / bbox.getWidth();
-	                    	float yScale = fieldRect.getHeight() / bbox.getHeight();
-	                    	Matrix scalingMatrix = Matrix.getScaleInstance(xScale, yScale);
-	                    	transformationMatrix.concatenate(scalingMatrix);
-	                    	transformed = true;
-	                    }
+                        PDRectangle bbox = appearanceStream.getBBox();
+                        PDRectangle fieldRect = annotation.getRectangle();
+                        
+                        if (bbox.getWidth() - fieldRect.getWidth() != 0 && bbox.getHeight()
- fieldRect.getHeight() != 0)
+                        {
+                            float xScale = fieldRect.getWidth() / bbox.getWidth();
+                            float yScale = fieldRect.getHeight() / bbox.getHeight();
+                            Matrix scalingMatrix = Matrix.getScaleInstance(xScale, yScale);
+                            transformationMatrix.concatenate(scalingMatrix);
+                            transformed = true;
+                        }
                     }
 
                     if (transformed)
                     {
-                    	contentStream.transform(transformationMatrix);
+                        contentStream.transform(transformationMatrix);
                     }
                     
                     contentStream.drawForm(fieldObject);
                     contentStream.restoreGraphicsState();
                     contentStream.close();
-                }
-            }
-        }
-
-        // preserve all non widget annotations
-        for (PDPage page : document.getPages())
-        {
-            List<PDAnnotation> annotations = new ArrayList<PDAnnotation>();
-            
-            for (PDAnnotation annotation: page.getAnnotations())
-            {
-                if (!(annotation instanceof PDAnnotationWidget))
-                {
-                    annotations.add(annotation);                 
-                }
+                }    
             }
             page.setAnnotations(annotations);
         }



Mime
View raw message