DEV Community

E-iceblue Product Family
E-iceblue Product Family

Posted on

Add, Extract and Delete Attachments in PDF in Java

We can attach a variety types of files such as PDF, Word, Excel, TXT and Images to a PDF file. In this article, we will show you how to add, extract and delete attachments in a PDF file in Java using Free Spire.PDF for Java library.

Dependencies

First of all, you need to add needed dependencies for including Free Spire.PDF for Java into your Java project. There are two ways to do that.
If you use maven, you need to add the following code to your project’s pom.xml file.

<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.free</artifactId>  
        <version>2.6.3</version>  
    </dependency>  
</dependencies>  

For non-maven projects, download Free Spire.PDF for Java pack from this website and add Spire.Pdf.jar in the lib folder into your project as a dependency.

Add attachments

The library provides a PdfAttachmentCollection.add(PdfAttachment attachment) method which is used to add attachments to a PDF file. The following code snippet shows how to attach txt, image, PDF and Word files to a PDF file.

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

public class AddAttachments {
    public static void main(String[] args){
        //Create a PdfDocument instance
        PdfDocument pdf = new PdfDocument();

        //Attach txt file to PDF
        PdfAttachment attachment = new PdfAttachment("Input.txt");
        attachment.setDescription("Input.txt");
        attachment.setMimeType("application/txt");
        pdf.getAttachments().add(attachment);

        //Attach image file to PDF
        attachment = new PdfAttachment("panda.jpg");
        attachment.setDescription("panda.jpg");
        attachment.setMimeType("image/jpg");
        pdf.getAttachments().add(attachment);

        //Attach PDF file to PDF
        attachment = new PdfAttachment("Example.pdf");
        attachment.setDescription("Example.pdf");
        attachment.setMimeType("application/pdf");
        pdf.getAttachments().add(attachment);

        //Attach Word file to PDF
        attachment = new PdfAttachment("Template.docx");
        attachment.setDescription("Template.docx");
        attachment.setMimeType("application/msword");
        pdf.getAttachments().add(attachment);

        //Save the result file
        pdf.saveToFile("AddAttachments.pdf");

    }
}

Add attachments to pdf

Extract attachments

To extract attachments, you first need to get the attachment collection of a PDF file by using PdfDocument.getAttachments() method, second, loop through the collection, extract and save each attachment to disk.

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

import java.io.*;

public class ExtractAttachments {
    public static void main(String[] args) throws IOException {
        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("AddAttachments.pdf");

        //Get the attachment collection of the PDF file
        PdfAttachmentCollection attachments = pdf.getAttachments();

        //Loop through the collection and extract all the attachments in the PDF file
        for (int i = 0; i < attachments.getCount(); i++) {
            File file = new File("Attachments/" + attachments.get(i).getFileName());
            OutputStream output = new FileOutputStream(file);
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
            bufferedOutput.write(attachments.get(i).getData());
            bufferedOutput.close();
        }
    }
}

Extract attachments from pdf

Delete attachments

You can easily delete all of the attachments from a PDF file by using the PdfAttachmentCollection.clear() method.

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

public class DeleteAttachments {
    public static void main(String[] args){
        //Load the PDF file
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("AddAttachments.pdf");

        //Get the attachment collection of the PDF file
        PdfAttachmentCollection attachments = pdf.getAttachments();
        //Delete all the attachments from the PDF file
        attachments.clear();

        //Save the result file
        pdf.saveToFile("DeleteAttachments.pdf");
    }
}

Delete attachments from pdf

Top comments (0)