Add, Extract and Delete Attachments in PDF in Java
Tags: java, pdf, attachments
Published: 2020-06-22 07:00:24

---
title: Add, Extract and Delete Attachments in PDF in Java
published: true
description: This article explains how to add, extract and delete attachments in a PDF file in Java.
tags: java, pdf, attachments
//cover_image: https://direct_url_to_image.jpg
---
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](https://www.e-iceblue.cn/Introduce/Free-Spire-PDF-JAVA.html) 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.
```java
<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](https://www.e-iceblue.com/Download/pdf-for-java-free.html) 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.
```java
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](https://dev-to-uploads.s3.amazonaws.com/i/5z8vvrlwkqarak30430t.png)
##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.
```java
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](https://dev-to-uploads.s3.amazonaws.com/i/qs7knrunrfklmp9cioa5.png)
##Delete attachments##
You can easily delete all of the attachments from a PDF file by using the **PdfAttachmentCollection.clear()** method.
```java
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](https://dev-to-uploads.s3.amazonaws.com/i/di588h6on9b6um5m27p3.png)