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 2014/09/29 17:46:34 UTC

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

Author: tilman
Date: Mon Sep 29 15:46:34 2014
New Revision: 1628210

URL: http://svn.apache.org/r1628210
Log:
PDFBOX-2394: Add example code to extract embedded files in annotations

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

Modified: pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java
URL: http://svn.apache.org/viewvc/pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java?rev=1628210&r1=1628209&r2=1628210&view=diff
==============================================================================
--- pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java (original)
+++ pdfbox/branches/1.8/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java Mon Sep 29 15:46:34 2014
@@ -17,6 +17,7 @@
 package org.apache.pdfbox.examples.pdmodel;
 
 import java.io.File;
+import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.util.List;
@@ -25,10 +26,13 @@ import java.util.Map;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.pdmodel.PDDocumentNameDictionary;
 import org.apache.pdfbox.pdmodel.PDEmbeddedFilesNameTreeNode;
+import org.apache.pdfbox.pdmodel.PDPage;
 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;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;
+import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotationFileAttachment;
 
 /**
  * This is an example on how to extract all embedded files from a PDF document.
@@ -96,6 +100,22 @@ public class ExtractEmbeddedFiles
                         }
                     }
                 }
+                
+                // extract files from annotations
+                List<PDPage> allPages = document.getDocumentCatalog().getAllPages();
+                for (PDPage pdPage : allPages)
+                {
+                    for (PDAnnotation annotation : pdPage.getAnnotations())
+                    {
+                        if (annotation instanceof PDAnnotationFileAttachment)
+                        {
+                            PDAnnotationFileAttachment annotationFileAttachment = (PDAnnotationFileAttachment) annotation;
+                            PDComplexFileSpecification fileSpec = (PDComplexFileSpecification) annotationFileAttachment.getFile();
+                            PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
+                            extractFile(filePath, fileSpec.getFilename(), embeddedFile);
+                        }
+                    }
+                }
             }
             finally
             {
@@ -107,7 +127,6 @@ public class ExtractEmbeddedFiles
         }
     }
 
-
     private static void extractFiles(Map<String,COSObjectable> names, String filePath) 
             throws IOException
     {
@@ -115,14 +134,20 @@ public class ExtractEmbeddedFiles
         {
             PDComplexFileSpecification fileSpec = (PDComplexFileSpecification)names.get(filename);
             PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
-            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();
+            extractFile(filePath, filename, embeddedFile);
         }
     }
+
+    private static void extractFile(String filePath, String filename, PDEmbeddedFile embeddedFile)
+            throws IOException, FileNotFoundException
+    {
+        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();
+    }    
     
     private static PDEmbeddedFile getEmbeddedFile(PDComplexFileSpecification fileSpec)
     {