Adding Attachment to PDF document
Contents
[
Hide
]
Attachments can contain a wide variety of information and can be of a variety of file types. This article explains how to add an attachment to a PDF file.
- Create a FileSpecification object that contains the file you want to attach, and file description.
- Add the FileSpecification object to a Document object’s EmbeddedFiles collection using the add(..) method. The EmbeddedFiles collection contains all the attachments added to a PDF file.
The following code snippet shows you how to add an attachment in a PDF document.
public class ExampleAttachments {
private static String _dataDir = "/home/aspose/pdf-examples/Samples/Attachments/";
public static void AddingAttachment() {
// Open a document
Document pdfDocument = new Document(_dataDir+"input.pdf");
// Set up a new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification("sample.txt", "Sample text file");
// Add an attachment to document's attachment collection
pdfDocument.getEmbeddedFiles().add(fileSpecification);
// Save the updated document
pdfDocument.save("output.pdf");
}
}