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/26 20:53:57 UTC

svn commit: r1870479 - /pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java

Author: tilman
Date: Tue Nov 26 20:53:56 2019
New Revision: 1870479

URL: http://svn.apache.org/viewvc?rev=1870479&view=rev
Log:
PDFBOX-4071: refactor

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

Modified: pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java?rev=1870479&r1=1870478&r2=1870479&view=diff
==============================================================================
--- pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java (original)
+++ pdfbox/branches/issue45/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java Tue Nov 26 20:53:56 2019
@@ -57,63 +57,70 @@ public final class ExtractEmbeddedFiles
             usage();
             System.exit(1);
         }
-        else
+
+        PDDocument document = null;
+        try
         {
-            PDDocument document = null;
-            try
+            File pdfFile = new File(args[0]);
+            String filePath = pdfFile.getParent() + System.getProperty("file.separator");
+            document = PDDocument.load(pdfFile );
+            PDDocumentNameDictionary namesDictionary = 
+                    new PDDocumentNameDictionary( document.getDocumentCatalog() );
+            PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
+            if (efTree != null)
             {
-                File pdfFile = new File(args[0]);
-                String filePath = pdfFile.getParent() + System.getProperty("file.separator");
-                document = PDDocument.load(pdfFile );
-                PDDocumentNameDictionary namesDictionary = 
-                        new PDDocumentNameDictionary( document.getDocumentCatalog() );
-                PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
-                if (efTree != null)
-                {
-                    Map<String, PDComplexFileSpecification> names = efTree.getNames();
-                    if (names != null)
-                    {
-                        extractFiles(names, filePath);
-                    }
-                    else
-                    {
-                        List<PDNameTreeNode<PDComplexFileSpecification>> kids = efTree.getKids();
-                        for (PDNameTreeNode<PDComplexFileSpecification> node : kids)
-                        {
-                            names = node.getNames();
-                            extractFiles(names, filePath);
-                        }
-                    }
-                }
-                
-                // extract files from annotations
-                for (PDPage page : document.getPages())
-                {
-                    for (PDAnnotation annotation : page.getAnnotations())
-                    {
-                        if (annotation instanceof PDAnnotationFileAttachment)
-                        {
-                            PDAnnotationFileAttachment annotationFileAttachment = (PDAnnotationFileAttachment) annotation;
-                            PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) annotationFileAttachment.getFile();
-                            PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
-                            if (embeddedFile != null)
-                            {
-                                extractFile(filePath, fileSpec.getFilename(), embeddedFile);
-                            }
-                        }
-                    }
-                }
-                
+                extractFilesFromEFTree(efTree, filePath);
             }
-            finally
+
+            // extract files from page annotations
+            for (PDPage page : document.getPages())
             {
-                if( document != null )
+                extractFilesFromPage(page, filePath);
+            }                
+        }
+        finally
+        {
+            if( document != null )
+            {
+                document.close();
+            }
+        }
+    }
+
+    private static void extractFilesFromPage(PDPage page, String filePath) throws IOException
+    {
+        for (PDAnnotation annotation : page.getAnnotations())
+        {
+            if (annotation instanceof PDAnnotationFileAttachment)
+            {
+                PDAnnotationFileAttachment annotationFileAttachment = (PDAnnotationFileAttachment) annotation;
+                PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) annotationFileAttachment.getFile();
+                PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
+                if (embeddedFile != null)
                 {
-                    document.close();
+                    extractFile(filePath, fileSpec.getFilename(), embeddedFile);
                 }
             }
         }
     }
+
+    private static void extractFilesFromEFTree(PDEmbeddedFilesNameTreeNode efTree, String filePath) throws IOException
+    {
+        Map<String, PDComplexFileSpecification> names = efTree.getNames();
+        if (names != null)
+        {
+            extractFiles(names, filePath);
+        }
+        else
+        {
+            List<PDNameTreeNode<PDComplexFileSpecification>> kids = efTree.getKids();
+            for (PDNameTreeNode<PDComplexFileSpecification> node : kids)
+            {
+                names = node.getNames();
+                extractFiles(names, filePath);
+            }
+        }
+    }
 
     private static void extractFiles(Map<String, PDComplexFileSpecification> names, String filePath) 
             throws IOException