Java-Extract Files from a PDF Portfolio

A PDF portfolio contains multiple documents assembled into a single unit. My previous article introduced how to create a PDF portfolio, and sometiones you may also need to extract files from the PDF portfolio. This article will share accomplish the task programmatically using Free Spire.PDF for Java.

Installation (2 methods)

Method 1: Download the free library and unzip it. Then add the Spire.Pdf.jar file to your project as dependency.

Method 2: Directly add the jar dependency to maven project by adding the following configurations to the pom.xml.


Sample Code

Free Spire.PDF for Java allows you to loop through all attachments in the PDF portfolio and then extract the files with few lines of code. The complete sample code is shown as below.

import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachment;


public class ReadPortfolio {
public static void main(String []args) throws IOException {
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load the PDF file

//Loop through the attachments in the file
for(PdfAttachment attachment : (Iterable<PdfAttachment>)pdf.getAttachments()){
//Extract files
String fileName = attachment.getFileName();
OutputStream fos = new FileOutputStream("extract/" + fileName);



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store