Java - How to Add, Extract, or Delete Attachments in PDF
Tags: java, pdf, attachment
Published: 2022-12-06 09:37:57

**PDF** is a universal format. In addition to the content rendered on its pages, it can also be used as a **storage container**. You can **attach other files to it** and **extract** them later. The advantage of attaching a file to a PDF over using external links is that the **attachment will travel with the PDF** if you move it to a different location. In this article, I am going to introduce how to **add, extract and delete attachments in a PDF document in Java** using [Spire.PDF for Java](https://www.e-iceblue.com/Introduce/pdf-for-java.html).

- [Add Attachments to PDF in Java](#add-attachments-to-pdf-in-java)
- [Extract Attachments from PDF in Java](#extract-attachments-from-pdf-in-java)
- [Remove Attachments from PDF in Java](#remove-attachments-from-pdf-in-java)

## Add Spire.Pdf.jar as dependency

If you are working on a maven project, you can include the dependency in pom.xml file using this:

```java
<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>8.11.8</version>
    </dependency>
</dependencies>
```
If you are not using maven, then you can find the required jar files from the zip file available in [this location](https://www.e-iceblue.com/Download/pdf-for-java.html). Include all the jar files into the application lib folder to run the sample code given in this tutorial.

## Add Attachments to PDF in Java

There are two ways to attach files to a PDF document – attach a file in the “Attachments” panel and attach a file as an annotation. The steps to add these two types of attachments are as follows.

- Create a **PdfDocument** object, and load a PDF file using **PdfDocument.loadFromFile()** method.
- Create a **PdfAttachment** object based on a certain file.
- Attach the file in the “Attachments” panel using **PdfDocument.getAttachments().add()** method.
- Create a **PdfAttachmentAnnotation** object based on a specific file, and add it to a specific page using **PdfPageBase.getAnnotationsWidget().add()** method. This attachment will appear in the “Attachments” panel and also display as an annotation on a page.
- Save the document to another PDF file using **PdfDocument.saveToFile()** method.

```java
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachment;
import com.spire.pdf.graphics.*;
import com.spire.pdf.PdfDocument;

import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public class AttachFiles {

    public static void main(String[] args) throws IOException {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a sample PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

        //Attach a file to PDF
        PdfAttachment attachment = new PdfAttachment("C:\\Users\\Administrator\\Desktop\\Data.xlsx");
        doc.getAttachments().add(attachment);

        //Get a specific page
        PdfPageBase page = doc.getPages().get(8);

        //Draw a label on PDF
        String label = "Here is the report:";
        PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.PLAIN, 13));
        double x = 35;
        double y = doc.getPages().get(0).getActualSize().getHeight() - 280;
        page.getCanvas().drawString(label, font, PdfBrushes.getRed(), x, y);

        //Attach a file as an annotation
        String filePath = "C:\\Users\\Administrator\\Desktop\\Report.pptx";
        byte[] data = toByteArray(filePath);
        Dimension2D size = font.measureString(label);
        Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 5), (float) y, 10, 15);
        PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
        annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
        annotation.setFlags(PdfAnnotationFlags.Default);
        annotation.setIcon(PdfAttachmentIcon.Graph);
        annotation.setText("Click here to open the file");
        page.getAnnotationsWidget().add(annotation);

        //Save to file
        doc.saveToFile("Attachments.pdf");
    }

    //Convert file to byte array
    public static byte[] toByteArray(String filePath) throws IOException {

        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }

        if (offset != buffer.length) {
            throw new IOException("Could not completely read file "
                    + file.getName());
        }
        fi.close();
        return buffer;
    }
}
```

![attachFiles](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q8jg690remzlru03g3lr.png)


## Extract Attachments from PDF in Java

Since the attachments in the “Attachments” tabs and the annotation attachments are two different things, the ways to extract them are different. The following steps demonstrate how to extract these two types of attachments using Spire.PDF for Java.

- Create a **PdfDocument** object, and load a PDF file using **PdfDocument.loadFromFile()** method.
- Get the attachment collection (not containing annotation attachments) from the document using **PdfDocument.getAttachments()** method.
- Get a specific attachment using **PdfAttachmentCollection.get()** method and its data using **PdfAttachment.getData()** method. Write the data to a file and save to a specified folder.
- Get annotation collection from the document using **PdfPageBase.getAnnotationsWidget()** method, then determine if an annotation is an attachment annotation. If yes, write the annotation to a file and save to a specified folder.

```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachmentCollection;

import java.io.*;

public class ExtractAttachments {

    public static void main(String[] args) throws Exception {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a PDF file that contains attachments
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //Get the attachment collection of the PDF document
        PdfAttachmentCollection attachments = doc.getAttachments();

        //Loop through the collection
        for (int i = 0; i < attachments.getCount(); i++) {

            //Specify the output file path and name
            File file = new File("C:\\Users\\Administrator\\Desktop\\output\\" + attachments.get(i).getFileName());
            OutputStream output = new FileOutputStream(file);
            BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);

            //Get a specific attachment and write to file
            bufferedOutput.write(attachments.get(i).getData());
            bufferedOutput.close();
        }

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //Get the annotation collection
            PdfAnnotationCollection collection = doc.getPages().get(i).getAnnotationsWidget();

            //Loop through the annotations
            for (Object annotation : collection) {

                //Determine if an annotation is an instance of PdfAttachmentAnnotationWidget
                if (annotation instanceof PdfAttachmentAnnotationWidget) {

                    //Save the attchment annoation out of the document
                    File file = new File("C:\\Users\\Administrator\\Desktop\\output\\" + ((PdfAttachmentAnnotationWidget) annotation).getText());
                    OutputStream output = new FileOutputStream(file);
                    BufferedOutputStream bufferedOutput = new BufferedOutputStream(output);
                    bufferedOutput.write(((PdfAttachmentAnnotationWidget) annotation).getData());
                    bufferedOutput.close();
                }
            }
        }
    }
}
```

![Extract](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7x7y37e0ngtx0e8yq556.png)

##Remove Attachments from PDF in Java

The following are the steps to remove attachments from a PDF document using Spire.PDF for Java.

- Create a **PdfDocument** object, and load a PDF file using **PdfDocument.loadFromFile()** method.
- Get the attachment collection (not containing annotation attachments) from the document using **PdfDocument.getAttachments()** method.
- Remove all attachments using **PdfAttachmentCollection.clear()** method. Or, you can remove a specific attachment using **PdfAttachmentCollection.removeAt(index)** method.
- To remove annotation attachments from a specific page, we firstly get annotation collection from the document using **PdfPageBase.getAnnotationsWidget()** method, then determine if annotation is an attachment annotation, if yes, remove the annotation using **PdfAnnotationCollection.remove()** method.
- Save the document to another PDF file using **PdfDocument.saveToFile()** method.

```java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.PdfAnnotation;
import com.spire.pdf.annotations.PdfAnnotationCollection;
import com.spire.pdf.annotations.PdfAttachmentAnnotationWidget;
import com.spire.pdf.attachments.PdfAttachmentCollection;

public class RemoveAttachments {

    public static void main(String[] args) {

        //Create a PdfDocument object
        PdfDocument doc = new PdfDocument();

        //Load a PDF file
        doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Attachments.pdf");

        //Get attachment collection, not containing annotation attachments
        PdfAttachmentCollection attachments = doc.getAttachments();

        //Remove all attachments
        attachments.clear();

        //Remove a specific attachment
        //attachments.removeAt(0);

        //Remove annotation attachments from the document
        removeAnnotationAttachment(doc);

        //save to file
        doc.saveToFile("output/DeleteAttachments.pdf");
        doc.close();
    }

    public static void removeAnnotationAttachment(PdfDocument doc){

        //Loop through the pages
        for (int i = 0; i < doc.getPages().getCount(); i++) {

            //Get the annotation collection
            PdfAnnotationCollection annotationCollection = doc.getPages().get(i).getAnnotationsWidget();

            //Loop through the annotations
            for (Object annotation: annotationCollection) {

                //Determine if an annotation is an instance of PdfAttachmentAnnotationWidget
                if (annotation instanceof PdfAttachmentAnnotationWidget){

                    //Remove the attachment annotation
                    annotationCollection.remove((PdfAnnotation) annotation);
                }
            }
        }
    }
}
```

![RemoveAttachments](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h8j9y0ou64o7igkdrtoq.png)