Extract Files from PDF Portfolio in Java

2021-07-12 06:11:23 Written by  support iceblue
Rate this item
(0 votes)

This article demonstrates how to extract files from a PDF portfolio in Java using Spire.PDF for Java.

The input PDF:

Extract Files from PDF Portfolio in Java

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

import java.io.*;

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

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

Output:

Extract Files from PDF Portfolio in Java

Additional Info

  • tutorial_title:
Last modified on Thursday, 02 September 2021 06:17