PDF format carries embedded attachments and that attachment can be anything. It can be a PDF itself, Excel, Word, PowerPoint document, or image files. In this article, I am going to show you how to extract attachments from a PDF document and save them to disk by using Spire.PDF for Java.
Below is a screenshot of the input document.
Installing Spire.Pdf.jar
If you create a Maven project, you can easily import the jar in your application using the following configurations. For non-Maven projects, download the jar file from this link and add it as a dependency in your application.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId> e-iceblue </groupId>
<artifactId>spire.pdf</artifactId>
<version>3.12.6</version>
</dependency>
</dependencies>
Using the code
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachmentCollection;
import java.io.*;
public class ExtractAttachmentsFromPDF {
public static void main(String[] args) throws IOException {
//Create a PdfDocument object
PdfDocument pdf = new
PdfDocument();
//Load the sample pdf document
pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");
//Get the attachment collection from pdf
PdfAttachmentCollection attachments = pdf.getAttachments();
//Loop through the attachments
for (int i = 0; i < attachments.getCount(); i++) {
//Create a new file based on the name of the specific attachment
File file = new File("attachments/" + attachments.get(i).getFileName());
//Write data to the new file
OutputStream output = new FileOutputStream(file);
BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
bufferedOutput.write(attachments.get(i).getData());
bufferedOutput.close();
}
}
}
Output
Write a comment