Adding Attachment to a 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.
The next code snippet also works with Aspose.Drawing library.
- Create a new C# project.
- Add a reference to Aspose.PDF DLL.
- Create a Document object.
- Create a FileSpecification object with the file you are adding, and file description.
- Add the FileSpecification object to the Document object’s EmbeddedFiles collection, with the collection’s Add method.
The EmbeddedFiles collection contains all the attachments in the PDF file. The following code snippet shows you how to add an attachment in a PDF document.
// For complete examples and data files, please go to https://github.com/aspose-pdf/Aspose.PDF-for-.NET
// The path to the documents directory.
string dataDir = RunExamples.GetDataDir_AsposePdf_Attachments();
// Open document
Document document = new Document(dataDir + "AddAttachment.pdf");
// Setup new file to be added as attachment
FileSpecification fileSpecification = new FileSpecification(dataDir + "test.txt", "Sample text file");
// Add attachment to document's attachment collection
document.EmbeddedFiles.Add(fileSpecification);
// Save updated document
document.Save(dataDir + "AddllAnnotations_out.pdf");