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 2019/11/24 16:02:28 UTC

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

Author: tilman
Date: Sun Nov 24 16:02:27 2019
New Revision: 1870307

URL: http://svn.apache.org/viewvc?rev=1870307&view=rev
Log:
PDFBOX-4697: reduce memory footprint by having one content stream

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=1870307&r1=1870306&r2=1870307&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 Nov 24 16:02:27 2019
@@ -295,23 +295,15 @@ 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);
-        
+
         // preserve all non widget annotations
         for (PDPage page : document.getPages())
         {
             Set<COSDictionary> widgetsForPageMap = pagesWidgetsMap.get(page.getCOSObject());
-
-            // indicates if the original content stream
-            // has been wrapped in a q...Q pair.
-            boolean isContentStreamWrapped = false;
-            
             List<PDAnnotation> annotations = new ArrayList<PDAnnotation>();
-            
+            PDPageContentStream contentStream = null;
             for (PDAnnotation annotation: page.getAnnotations())
             {
                 if (widgetsForPageMap != null && !widgetsForPageMap.contains(annotation.getCOSObject()))
@@ -322,9 +314,12 @@ public final class PDAcroForm implements
                          annotation.getNormalAppearanceStream() != null && 
                          annotation.getNormalAppearanceStream().getBBox() != null)
                 {
-                    contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, !isContentStreamWrapped);
-                    isContentStreamWrapped = true;
-                    
+                    if (contentStream == null)
+                    {
+                        // have one content stream for all (reduces the memory footprint)
+                        contentStream = new PDPageContentStream(document, page, AppendMode.APPEND, true, true);
+                    }
+
                     PDAppearanceStream appearanceStream = annotation.getNormalAppearanceStream();
                     
                     PDFormXObject fieldObject = new PDFormXObject(appearanceStream.getCOSObject());
@@ -383,8 +378,11 @@ public final class PDAcroForm implements
                     
                     contentStream.drawForm(fieldObject);
                     contentStream.restoreGraphicsState();
-                    contentStream.close();
-                }
+                }    
+            }
+            if (contentStream != null)
+            {
+                contentStream.close();
             }
             page.setAnnotations(annotations);
         }