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 2021/06/06 12:15:46 UTC

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

Author: tilman
Date: Sun Jun  6 12:15:45 2021
New Revision: 1890529

URL: http://svn.apache.org/viewvc?rev=1890529&view=rev
Log:
PDFBOX-4892: avoid memory leak, as suggested by valerybokov

Modified:
    pdfbox/branches/2.0/pdfbox/src/main/java/org/apache/pdfbox/pdmodel/interactive/form/PDAcroForm.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=1890529&r1=1890528&r2=1890529&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 Sun Jun  6 12:15:45 2021
@@ -242,9 +242,6 @@ public final class PDAcroForm implements
             refreshAppearances(fields);
         }
 
-        // the content stream to write to
-        PDPageContentStream contentStream;
-
         // get the widgets per page
         Map<COSDictionary,Set<COSDictionary>> pagesWidgetsMap = buildPagesWidgetsMap(fields);
         
@@ -267,24 +264,31 @@ public final class PDAcroForm implements
                 }
                 else if (isVisibleAnnotation(annotation))
                 {
-                    contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, !isContentStreamWrapped);
-                    isContentStreamWrapped = true;
-                    
-                    PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
-                    
-                    PDFormXObject fieldObject = new PDFormXObject(appearanceStream.getCOSObject());
-                    
-                    contentStream.saveGraphicsState();
-                    
-                    // see https://stackoverflow.com/a/54091766/1729265 for an explanation
-                    // of the steps required
-                    // this will transform the appearance stream form object into the rectangle of the
-                    // annotation bbox and map the coordinate systems
-                    Matrix transformationMatrix = resolveTransformationMatrix(annotation, appearanceStream);
-                    contentStream.transform(transformationMatrix);                    
-                    contentStream.drawForm(fieldObject);
-                    contentStream.restoreGraphicsState();
-                    contentStream.close();
+                    PDPageContentStream contentStream = new PDPageContentStream(
+                            document, page, AppendMode.APPEND, true, !isContentStreamWrapped);
+                    try
+                    {
+                        isContentStreamWrapped = true;
+
+                        PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
+
+                        PDFormXObject fieldObject = new PDFormXObject(appearanceStream.getCOSObject());
+
+                        contentStream.saveGraphicsState();
+
+                        // see https://stackoverflow.com/a/54091766/1729265 for an explanation
+                        // of the steps required
+                        // this will transform the appearance stream form object into the rectangle of the
+                        // annotation bbox and map the coordinate systems
+                        Matrix transformationMatrix = resolveTransformationMatrix(annotation, appearanceStream);
+                        contentStream.transform(transformationMatrix);
+                        contentStream.drawForm(fieldObject);
+                        contentStream.restoreGraphicsState();
+                    }
+                    finally
+                    {
+                        contentStream.close();
+                    }
                 }
             }
             page.setAnnotations(annotations);