add or remove pdf attachments in java

PDF is one of the ruling file formats in the world of digital documents. Along with other popular features, PDF format also allows you to embed files as attachments within a PDF file. These attachments are similar to ones you add to an email message. In order to automate PDF attachment manipulation, this article provides you some simple ways to add and remove attachments in PDF files using Java.

Java API for PDF Attachments – Free Download

Aspose.PDF for Java is PDF file manipulation API that lets you create, edit, and manipulate PDF documents from within your Java applications. In addition, it provides easy to use methods to add and remove attachments in the PDF files. You can either download API’s JAR or install it within your Maven-based applications using the following configurations.

<repository>
    <id>AsposeJavaAPI</id>
    <name>Aspose Java API</name>
    <url>https://repository.aspose.com/repo/</url>
</repository>
<dependency>
    <groupId>com.aspose</groupId>
    <artifactId>aspose-pdf</artifactId>
    <version>20.10</version>
    <classifier>jdk17</classifier>
</dependency>

Extract PDF Attachment Information using Java

First of all, let’s check out how to retrieve information about the attachments in a PDF file. The information contains the attachment’s name, description, MIME type, and other parameters such as the checksum, modified date, etc. The following are the steps to get information about an attachment in a PDF file.

The following code sample shows how to get information of a PDF attachment using Java.

Add an Attachment to PDF in Java

The following are the steps to add an attachment to a PDF document.

The following code sample shows how to add an attachment to a PDF using Java.

Remove Attachments from PDF in Java

You can either delete a particular attachment using its name or delete all attachments at once. The following are the steps to remove attachments from the PDF document.

The following code sample shows how to remove PDF attachments using Java.

Conclusion

In this article, you have learned how to work with attachments in PDF files using Java. The step-by-step guide and code samples have shown how to add and remove attachments from PDF programmatically using Java. You can explore more about Aspose.PDF for Java using documentation.

See Also