Class ExtractEmbeddedFiles

java.lang.Object
org.jpedal.examples.BaseExample
org.jpedal.examples.acroform.ExtractEmbeddedFiles

public class ExtractEmbeddedFiles extends org.jpedal.examples.BaseExample

File Extraction from PDF files


This class provides a simple Java API to extract embedded files and file attachments from a PDF file and also a static convenience method if you just want to dump all files from a PDF file or directory containing PDF files. All files are extracted to a folder at the given output location with a name matching the pdf filename

Example 1 - access API methods


 ExtractEmbeddedFiles extract=new ExtractEmbeddedFiles("C:/pdfs/mypdf.pdf");
 //extract.setPassword("password");
 if (extract.openPDFFile()) {
     if (extract.containsEmbeddedFiles()) {
         extract.extractEmbeddedFiles("C:/output/");
     }
     if (extract.containsFilesAttachments()) {
         extract.extractFileAttachments("C:/output");
     }
 }
 extract.closePDFfile();
 

Example 2 - convenience static method

Extract all embedded files and file attachments from a pdf


 ExtractEmbeddedFiles.extractAllFilesFromPdf("C:/pdfs/mypdf.pdf", "C:/output");
 

Example 3 - Access directly from the Jar

ExtractEmbeddedFiles can run from jar directly using the command and will extract all embedded files and file attachments from a PDF file or directory to a defined output directory:

java -cp libraries_needed org/jpedal/examples/acroform/ExtractEmbeddedFiles inputValues

Where inputValues is 3 values:
  • First value: The PDF filename (including the path if needed) or a directory containing PDF files. If it contains spaces it must be enclosed by double quotes (ie "C:/Path with spaces/").
  • Second value: The location to write out extracted files from the PDF file or files. If it contains spaces it must be enclosed by double quotes (ie "C:/Path with spaces/").

  • Constructor Details

    • ExtractEmbeddedFiles

      public ExtractEmbeddedFiles(File file)
      Create an ExtractEmbeddedFiles for a PDF file. The file is not opened until BaseExample.openPDFFile() is called.
      Parameters:
      file - the PDF file
    • ExtractEmbeddedFiles

      public ExtractEmbeddedFiles(String fileName)
      Create an ExtractEmbeddedFiles for a PDF file. The file is not opened until BaseExample.openPDFFile() is called.
      Parameters:
      fileName - the path to the PDF file
    • ExtractEmbeddedFiles

      public ExtractEmbeddedFiles(byte[] byteArray)
      Create an ExtractEmbeddedFiles for a PDF held in memory. The PDF is not opened until BaseExample.openPDFFile() is called.
      Parameters:
      byteArray - the bytes of the PDF file
  • Method Details

    • main

      public static void main(String[] args)
      Extract all embedded files and file attachments from a PDF file or directory of PDF files, as extractAllFilesFromPdf(String, String) does. If fewer than 2 arguments are given, the arguments are printed to standard out and nothing is extracted.
      Parameters:
      args - the input PDF file or directory, then the output directory
    • setPassword

      public void setPassword(String password)
      Parameters:
      password - the USER or OWNER password for the PDF file
    • containsFilesAttachments

      public boolean containsFilesAttachments()
      Method to flag if the current file contains file attachments
      Returns:
      True is file attachments are present, otherwise false.
    • extractFileAttachments

      public void extractFileAttachments(String outputDirectory)
      Extract files from file attachment annotations in the open file and place them in the output directory specified. A directory is placed in the given directory, the name is that of the pdf and it contains all extracted files. When extracting the files any existing files of the same name will be replaced. This does not extract files contained within the EmbeddedFiles dictionary (such as those found in Portfolios).
      Parameters:
      outputDirectory - Path where the extract files should be saved.
    • extractAllEmbeddedFilesAsMap

      public static Map<String,byte[]> extractAllEmbeddedFilesAsMap(String inputFilename) throws PdfException
      Open a PDF file, read every file in its EmbeddedFiles name tree and close it again. This does not include files contained within File Attachment annotations.
      Parameters:
      inputFilename - the path to the PDF file
      Returns:
      a map of embedded file name to file contents, empty if the PDF could not be opened
      Throws:
      PdfException - if the PDF file cannot be opened
    • extractEmbeddedFile

      public byte[] extractEmbeddedFile(String requestedFile)
      Read a file from the EmbeddedFiles name tree of the open PDF.
      Parameters:
      requestedFile - the file name, as returned by getEmbeddedFileNames()
      Returns:
      the decoded file contents, or an empty array if no embedded file has that name
    • getEmbeddedFileNames

      public String[] getEmbeddedFileNames()
      Get the names of the files in the EmbeddedFiles name tree of the open PDF. Each name is taken from the UF entry, or the F entry if UF is missing, with any path before the last '/' removed.
      Returns:
      the embedded file names, or an empty array if there are none
    • extractAllFileAttachmentsAsMap

      public static Map<String,byte[]> extractAllFileAttachmentsAsMap(String inputFilename) throws PdfException
      Throws:
      PdfException
    • extractAllFileAttachmentsOnPageAsMap

      public static Map<String,byte[]> extractAllFileAttachmentsOnPageAsMap(String inputFilename, int page) throws PdfException
      Throws:
      PdfException
    • extractFileAttachment

      public byte[] extractFileAttachment(String requestedFile)
      Read the file from a File Attachment annotation in the open PDF.
      Parameters:
      requestedFile - the file name, as returned by getFileAttachmentNames()
      Returns:
      the decoded file contents, or an empty array if no file attachment has that name
    • extractAllFileAttachmentFilesOnPage

      public HashMap<String,byte[]> extractAllFileAttachmentFilesOnPage(int page)
      Read the files from all File Attachment annotations on a page of the open PDF.
      Parameters:
      page - the page number (starting at 1)
      Returns:
      a map of file name to decoded file contents, empty if the page has no file attachments
    • getFileAttachmentNames

      public String[] getFileAttachmentNames()
      Get the names of the files in the File Attachment annotations of the open PDF. Each name is taken from the UF entry, or the F entry if UF is missing, with any path before the last '/' removed.
      Returns:
      the file attachment names, or an empty array if there are none
    • containsEmbeddedFiles

      public boolean containsEmbeddedFiles()
      Method to flag if the current file contains embedded files.
      Returns:
      True is embedded files are present, otherwise false.
    • extractEmbeddedFiles

      public void extractEmbeddedFiles(String outputDirectory)
      Extract embedded files and place them in the output directory specified. A directory is placed in the given directory, the name is that of the pdf and it contains all extracted files. When extracting the files any existing files of the same name will be replaced. This does not extract files contained within File Attachment annotations.
      Parameters:
      outputDirectory - Path where the extracted files should be saved.
    • extractAllFilesFromPdf

      public static void extractAllFilesFromPdf(String inputDir, String outputDir) throws PdfException
      static method to write out all pages in a PDF files or directory of PDF files as images
      Parameters:
      inputDir - directory of files to convert
      outputDir - directory of output
      Throws:
      PdfException - PdfException
    • showEmbeddedFilesDetails

      public void showEmbeddedFilesDetails()
      Print the details of each file in the EmbeddedFiles name tree of the open PDF to standard out. Only call this when containsEmbeddedFiles() returns true.
    • showFileAttachmentDetails

      public void showFileAttachmentDetails()
      Print the details of the file in each File Attachment annotation of the open PDF to standard out.