Package org.jpedal.examples.acroform
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 inputValuesWhere 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 Summary
ConstructorsConstructorDescriptionExtractEmbeddedFiles(byte[] byteArray) Create an ExtractEmbeddedFiles for a PDF held in memory.ExtractEmbeddedFiles(File file) Create an ExtractEmbeddedFiles for a PDF file.ExtractEmbeddedFiles(String fileName) Create an ExtractEmbeddedFiles for a PDF file. -
Method Summary
Modifier and TypeMethodDescriptionbooleanMethod to flag if the current file contains embedded files.booleanMethod to flag if the current file contains file attachmentsextractAllEmbeddedFilesAsMap(String inputFilename) Open a PDF file, read every file in its EmbeddedFiles name tree and close it again.extractAllFileAttachmentFilesOnPage(int page) Read the files from all File Attachment annotations on a page of the open PDF.extractAllFileAttachmentsAsMap(String inputFilename) extractAllFileAttachmentsOnPageAsMap(String inputFilename, int page) static voidextractAllFilesFromPdf(String inputDir, String outputDir) static method to write out all pages in a PDF files or directory of PDF files as imagesbyte[]extractEmbeddedFile(String requestedFile) Read a file from the EmbeddedFiles name tree of the open PDF.voidextractEmbeddedFiles(String outputDirectory) Extract embedded files and place them in the output directory specified.byte[]extractFileAttachment(String requestedFile) Read the file from a File Attachment annotation in the open PDF.voidextractFileAttachments(String outputDirectory) Extract files from file attachment annotations in the open file and place them in the output directory specified.String[]Get the names of the files in the EmbeddedFiles name tree of the open PDF.String[]Get the names of the files in the File Attachment annotations of the open PDF.static voidExtract all embedded files and file attachments from a PDF file or directory of PDF files, asextractAllFilesFromPdf(String, String)does.voidsetPassword(String password) voidPrint the details of each file in the EmbeddedFiles name tree of the open PDF to standard out.voidPrint the details of the file in each File Attachment annotation of the open PDF to standard out.Methods inherited from class org.jpedal.examples.BaseExample
closePDFfile, openPDFFile
-
Constructor Details
-
ExtractEmbeddedFiles
Create an ExtractEmbeddedFiles for a PDF file. The file is not opened untilBaseExample.openPDFFile()is called.- Parameters:
file- the PDF file
-
ExtractEmbeddedFiles
Create an ExtractEmbeddedFiles for a PDF file. The file is not opened untilBaseExample.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 untilBaseExample.openPDFFile()is called.- Parameters:
byteArray- the bytes of the PDF file
-
-
Method Details
-
main
Extract all embedded files and file attachments from a PDF file or directory of PDF files, asextractAllFilesFromPdf(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
- 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
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
Read a file from the EmbeddedFiles name tree of the open PDF.- Parameters:
requestedFile- the file name, as returned bygetEmbeddedFileNames()- Returns:
- the decoded file contents, or an empty array if no embedded file has that name
-
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
Read the file from a File Attachment annotation in the open PDF.- Parameters:
requestedFile- the file name, as returned bygetFileAttachmentNames()- Returns:
- the decoded file contents, or an empty array if no file attachment has that name
-
extractAllFileAttachmentFilesOnPage
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
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
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
static method to write out all pages in a PDF files or directory of PDF files as images- Parameters:
inputDir- directory of files to convertoutputDir- 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 whencontainsEmbeddedFiles()returns true. -
showFileAttachmentDetails
public void showFileAttachmentDetails()Print the details of the file in each File Attachment annotation of the open PDF to standard out.
-