title: What is the best way to extract embedded flash file from a PDF using the PDFBox parser?
body: <p>I've been working around PDF parsing since late last week. Managed to find the Apache PDFBox library for Java and I already got to extract text separated by page, URLs, images and PDF Metadata that I needed for the project I'm developing. Now I'm missing a way to extract embedded flash videos from a PDF.</p>
  
  <p>I'm currently analysing how this parser extracts rich media from PDFs, using, for test purposes the pdf file available <a href="http://www.pdfpictures.com/download.php?name=Mississauga_Advantages.pdf" rel="nofollow noreferrer">here</a>. This file contains a flash video which I intended to fetch.</p>
  
  <p>I already tried using this <a href="https://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ExtractEmbeddedFiles.java?view=markup" rel="nofollow noreferrer">approach</a> which searches for embedded files inside the PDF but it is currently not working for me as it finds and saves nothing inside the folder I created to store this kind of files.</p>
  
  <p>What my code currently looks like, adapted from the approach mentioned above.</p>
  
  <pre><code>package myproject;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.util.List;
  import java.util.Map;
  import java.util.Map.Entry;
  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.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.
   *
   */
  public final class ExtractEmbeddedFiles
  {
      private ExtractEmbeddedFiles()
      {
      }
  
      /**
       * This is the main method.
       *
       * @param args The command line arguments.
       *
       * @throws IOException If there is an error parsing the document.
       */
      public static void main( String[] args ) throws IOException
      {
              PDDocument document = null;
              try
              {
                  File pdfFile = new File("/Users/henriqueferreira/Documents/PDFBoxDocuments/inOntario.pdf");
                  String filePath = pdfFile.getParent() + System.getProperty("file.separator");
                  document = PDDocument.load(new File("/Users/henriqueferreira/Documents/PDFBoxDocuments/inOntario.pdf"));
                  PDDocumentNameDictionary namesDictionary = 
                          new PDDocumentNameDictionary( document.getDocumentCatalog() );
                  PDEmbeddedFilesNameTreeNode efTree = namesDictionary.getEmbeddedFiles();
                  if (efTree != null)
                  {
                      Map&lt;String, PDComplexFileSpecification&gt; names = efTree.getNames();
                      if (names != null)
                      {
                          extractFiles(names, filePath);
                      }
                      else
                      {
                          List&lt;PDNameTreeNode&lt;PDComplexFileSpecification&gt;&gt; kids = efTree.getKids();
                          for (PDNameTreeNode&lt;PDComplexFileSpecification&gt; 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);
                              extractFile(filePath, fileSpec.getFilename(), embeddedFile);
                          }
                      }
                  }
  
              }
              finally
              {
                  if( document != null )
                  {
                      document.close();
                  }
              }
      }
  
      private static void extractFiles(Map&lt;String, PDComplexFileSpecification&gt; names, String filePath) 
              throws IOException
      {
          for (Entry&lt;String, PDComplexFileSpecification&gt; entry : names.entrySet())
          {
              String filename = entry.getKey();
              PDComplexFileSpecification fileSpec = entry.getValue();
              PDEmbeddedFile embeddedFile = getEmbeddedFile(fileSpec);
              extractFile(filePath, filename, embeddedFile);
          }
      }
  
      private static void extractFile(String filePath, String filename, PDEmbeddedFile embeddedFile)
              throws IOException
      {
          String embeddedFilename = filePath + filename;
          File file = new File("/Users/henriqueferreira/Documents/PDFBoxFiles/"+filename);
          System.out.println("Writing " + embeddedFilename);
          try (FileOutputStream fos = new FileOutputStream(file))
          {
              fos.write(embeddedFile.toByteArray());
          }
      }
  
      private static PDEmbeddedFile getEmbeddedFile(PDComplexFileSpecification fileSpec )
      {
          // search for the first available alternative of the embedded file
          PDEmbeddedFile embeddedFile = null;
          if (fileSpec != null)
          {
              embeddedFile = fileSpec.getEmbeddedFileUnicode(); 
              if (embeddedFile == null)
              {
                  embeddedFile = fileSpec.getEmbeddedFileDos();
              }
              if (embeddedFile == null)
              {
                  embeddedFile = fileSpec.getEmbeddedFileMac();
              }
              if (embeddedFile == null)
              {
                  embeddedFile = fileSpec.getEmbeddedFileUnix();
              }
              if (embeddedFile == null)
              {
                  embeddedFile = fileSpec.getEmbeddedFile();
              }
          }
          return embeddedFile;
      }
  }
  </code></pre>
  
  <p>So, my question is, what should be the most appropriate method to get this type of flash videos from a PDF file?</p>