Jacob Collins Jacob is a Java developer and the product manager of JPedal

How to attach files to a PDF in Java (Tutorial)

1 min read

embed files in pdf

In this article I will show you how you can attach PDF files in Java using our JPedal PDF SDK toolkit, using a few lines of Java code.

JPedal offers other PDF manipulation features, to aid Java developers working with the PDF format.

Introduction

PDF is a powerful format, and one such feature is the ability to embed/attach files within a PDF document. This is useful if you want to bundle resources with a document but do not want to distribute them as a ZIP or RAR file, and is great for PDF/A compliance.

Adding attachments to PDF

If you want to be able to programmatically attach files to a PDF document using Java, you may use our PDF toolkit JPedal.

  1. First, download the trial JAR
  2. Second, add JPedal to your project
  3. Finally, add the following Java code to your app:

If you want to include a FileAttachment annotation on a page to reference the attached file, you can specify a color and a bounding box like so:

Learn more about the PdfManipulator class.

Viewing attachments

If you want to view the files attachments that you have embedded in your PDF document, you may use a PDF viewer such as the JPedal Viewer.

java -jar jpedal.jar --view "inputFile.pdf"

Learn more about the JPedal Viewer.

We’ve been working with PDF files for over two decades and can help you understand the PDF format



The JPedal PDF library allows you to solve these problems in Java


Jacob Collins Jacob is a Java developer and the product manager of JPedal