pdfbox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From le...@apache.org
Subject svn commit: r1454887 - /pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
Date Sun, 10 Mar 2013 17:58:55 GMT
Author: lehmi
Date: Sun Mar 10 17:58:54 2013
New Revision: 1454887

URL: http://svn.apache.org/r1454887
Log:
PDFBOX-1538: handle both kinds of embedded files tree configurations

Modified:
    pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java

Modified: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java?rev=1454887&r1=1454886&r2=1454887&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
(original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
Sun Mar 10 17:58:54 2013
@@ -18,13 +18,16 @@ package org.apache.pdfbox.examples.pdmod
 
 import java.io.File;
 import java.io.FileOutputStream;
-import java.util.Iterator;
+import java.io.IOException;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.pdfbox.exceptions.InvalidPasswordException;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
 import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
+import org.apache.pdfbox.pdmodel.common.COSObjectable;
+import org.apache.pdfbox.pdmodel.common.PDNameTreeNode;
 import org.apache.pdfbox.pdmodel.common.filespecification.PDComplexFileSpecification;
 import org.apache.pdfbox.pdmodel.common.filespecification.PDEmbeddedFile;
 
@@ -58,7 +61,6 @@ public class ExtractEmbeddedFiles
         else
         {
             PDDocument document = null;
-
             try
             {
                 File pdfFile = new File(args[0]);
@@ -80,23 +82,23 @@ public class ExtractEmbeddedFiles
                     }
                 }
                 PDDocumentNameDictionary namesDictionary = 
-                    new PDDocumentNameDictionary( document.getDocumentCatalog() );
+                        new PDDocumentNameDictionary( document.getDocumentCatalog() );
                 PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
                 if (efTree != null)
                 {
-                    Map<String,Object> names = efTree.getNames();
-                    Iterator<String> namesKeys = names.keySet().iterator();
-                    while (namesKeys.hasNext())
+                    Map<String,COSObjectable> names = efTree.getNames();
+                    if (names != null)
+                    {
+                        extractFiles(names, filePath);
+                    }
+                    else
                     {
-                        String filename = namesKeys.next();
-                        PDComplexFileSpecification fileSpec = (PDComplexFileSpecification)names.get(filename);
-                        PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
-                        String embeddedFilename = filePath+filename;
-                        File file = new File(filePath+filename);
-                        System.out.println("Writing "+ embeddedFilename);
-                        FileOutputStream fos = new FileOutputStream(file);
-                        fos.write(embeddedFile.getByteArray());
-                        fos.close();
+                        List<PDNameTreeNode> kids = efTree.getKids();
+                        for (PDNameTreeNode node : kids)
+                        {
+                            names = node.getNames();
+                            extractFiles(names, filePath);
+                        }
                     }
                 }
             }
@@ -111,6 +113,21 @@ public class ExtractEmbeddedFiles
     }
 
 
+    private static void extractFiles(Map<String,COSObjectable> names, String filePath)

+            throws IOException
+    {
+        for (String filename : names.keySet())
+        {
+            PDComplexFileSpecification fileSpec = (PDComplexFileSpecification)names.get(filename);
+            PDEmbeddedFile embeddedFile = fileSpec.getEmbeddedFile();
+            String embeddedFilename = filePath+filename;
+            File file = new File(filePath+filename);
+            System.out.println("Writing "+ embeddedFilename);
+            FileOutputStream fos = new FileOutputStream(file);
+            fos.write(embeddedFile.getByteArray());
+            fos.close();
+        }
+    }
     /**
      * This will print the usage for this program.
      */



Mime
View raw message