Author: lehmi
Date: Sun Mar 28 15:57:41 2010
New Revision: 928439

URL: http://svn.apache.org/viewvc?rev=928439&view=rev
Log:
PDFBOX-515: close the source files not before all files are merged. Patch by 
Ernst Eibensteiner (ernst dot eibensteiner at fabasoft dot com)

Modified:
    
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java

Modified: 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java?rev=928439&r1=928438&r2=928439&view=diff
==============================================================================
--- 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java 
(original)
+++ 
pdfbox/trunk/pdfbox/src/main/java/org/apache/pdfbox/util/PDFMergerUtility.java 
Sun Mar 28 15:57:41 2010
@@ -57,7 +57,7 @@ import org.apache.pdfbox.pdmodel.interac
 public class PDFMergerUtility
 {
 
-    private List sources;
+    private List<File> sources;
     private String destinationFileName;
 
     /**
@@ -65,7 +65,7 @@ public class PDFMergerUtility
      */
     public PDFMergerUtility()
     {
-        sources = new ArrayList();
+        sources = new ArrayList<File>();
     }
 
     /**
@@ -120,26 +120,20 @@ public class PDFMergerUtility
         PDDocument source;
         if (sources != null && sources.size() > 0)
         {
+               java.util.Vector<PDDocument> tobeclosed = new 
java.util.Vector<PDDocument>();
+               
             try
             {
-                Iterator sit = sources.iterator();
-                sourceFile = (File) sit.next();
+                Iterator<File> sit = sources.iterator();
+                sourceFile = sit.next();
                 destination = PDDocument.load(sourceFile);
+
                 while (sit.hasNext())
                 {
                     sourceFile = (File) sit.next();
                     source = PDDocument.load(sourceFile);
-                    try
-                    {
-                        appendDocument(destination, source);
-                    }
-                    finally
-                    {
-                        if (source != null)
-                        {
-                            source.close();
-                        }
-                    }
+                    tobeclosed.add(source);
+                    appendDocument(destination, source);
                 }
                 destination.save(destinationFileName);
             }
@@ -149,6 +143,9 @@ public class PDFMergerUtility
                 {
                     destination.close();
                 }
+               for(PDDocument doc : tobeclosed){
+                       doc.close();
+               } 
             }
         }
     }
@@ -298,11 +295,11 @@ public class PDFMergerUtility
         }
 
         //finally append the pages
-        List pages = source.getDocumentCatalog().getAllPages();
-        Iterator pageIter = pages.iterator();
+        List<PDPage> pages = source.getDocumentCatalog().getAllPages();
+        Iterator<PDPage> pageIter = pages.iterator();
         while( pageIter.hasNext() )
         {
-            PDPage page = (PDPage)pageIter.next();
+            PDPage page = pageIter.next();
             PDPage newPage =
                 new PDPage( (COSDictionary)cloneForNewDocument( destination, 
page.getCOSDictionary() ) );
             newPage.setCropBox( page.findCropBox() );
@@ -311,7 +308,8 @@ public class PDFMergerUtility
             destination.addPage( newPage );
         }
     }
-    Map clonedVersion = new HashMap();
+
+    Map<Object,COSBase> clonedVersion = new HashMap<Object,COSBase>();
 
 
   /**


Reply via email to