Working with Attachments

PDF files allow attaching files as content and are one of the key features of PDF file format. Different file formats such as Word DOC/DOCX, Excel XLS/XLSX, EML, JPG, PNG, GLB, etc. can be embedded in a PDF file. Aspose.PDF for .NET API lets you work with attachments in a PDF file using your .NET applications. You can add/embed attachments in a PDF file, get attachments information from a PDF, access individual attachment from PDF file, and remove attachments from PDF in C# or any other .NET programming language. This article demonstrates the functionality of working with attachments in a PDF file.

Add Attachment to PDF

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.

  1. Create a new C# project.
  2. Add a reference to Aspose.PDF DLL.
  3. Create a Document object.
  4. Create a FileSpecification object with the file you are adding, and file description.
  5. 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.

C#

Get all Attachments from a PDF Document

With Aspose.PDF, it is possible to get all attachments from a PDF document. This is useful either when you want to save the documents separately from the PDF, or if you need to strip a PDF of attachments.

To get all attachments from a PDF file:

  1. Loop through the Document object’s EmbeddedFiles collection. The EmbeddedFiles collection contains all attachments. Each element of this collection represents a FileSpecification object. Each iteration of the foreach loop through the EmbeddedFiles collection returns a FileSpecification object.
  2. Once the object is available, retrieve either all the attached file’s properties or the file itself.

The following code snippets show how to get all the attachments from a PDF document.

Get Individual Attachment

In order to get an individual attachment, we can specify the index of attachment in EmbeddedFiles object of Document instance. Please try using following code snippet.

C#

Delete All Attachments from PDF Document

Aspose.PDF can remove attachments from PDF files. A PDF document’s attachments are held in the Document object’s EmbeddedFiles collection.

To delete all attachments associated with a PDF file:

  1. Call the EmbeddedFiles collection’s Delete method.
  2. Save the updated file using the Document object’s Save method.

The following code snippet shows how to remove attachments from a PDF document.

C#

Get Attachment Information

As mentioned in Get All Annotations from the Page of a PDF Document, attachment information is held in the FileSpecification object, gathered with other attachments in the Document object’s EmbeddedFiles collection. The FileSpecification object provides methods that get information about the attachment, for example:

  • Name - the file name.
  • Description - the file description.
  • MIMEType - the file’s MIME type.
  • Params - information about the file parameters, for example CheckSum, CreationDate, ModDate and Size.

To get these parameters, first make sure that the Params property is not null. Either loop through all of the attachments in the EmbeddedFiles collection using a foreach loop, or get an individual attachment by specifying its index value. The following code snippets show how to get information about a specific attachment.

C#

Disable Files Compression when Adding as Embedded Resources

The FileSpecification class allows developers to add attachments to PDF documents. By default, attachments are compressed. We have updated the API to allow developers to disable compression when adding files as embedded resource. This gives developers more control over how files are processed.

To allow developers to control file compression the Encoding property has been added to the FileSpecification class. This property determines which encoding will be used for file compression. The property accepts a value from the FileEncoding enumerator. The possible values are FileEncoding.None and FileEncoding.Zip.

The following code snippet shows how to add an attachment to a PDF document.

C#