The PDF format supports adding attachments to a PDF file similar to the email attachments. A PDF attachment could be of TXT, DOCX, XLSX, or any other document format. In this article, you will learn how to implement some basic PDF attachment manipulation features in your .NET applications. By the end of this article, you will be able to extract, add, or remove attachments in a PDF programmatically using C#.
- PDF Attachment Manipulation API – Free Download
- Add Attachments to PDF using C#
- Extract PDF Attachment using C#
- Remove Attachments from PDF using C#
PDF Attachment Manipulation API – Free Download
Aspose.PDF for .NET is a well-known PDF manipulation API that lets you process PDF files seamlessly. You can read, create, edit, and convert the PDF files and manipulate the PDF attachments within a few steps. The API can be downloaded as DLL or MSI as well as installed using NuGet.
Install-Package Aspose.Pdf
Add Attachments to PDF using C#
Aspose.PDF for .NET also allows you to add the attachments to a PDF file. For this, you simply need to add the file to Document.EmbeddedFiles collection using the FileSpecification class. The following are the steps to add an attachment to a PDF document.
- Create a new PDF document using Document class.
- Create an instance of FileSpecification class to load the attachment file.
- Use Document.EmbeddedFiles.Add(FileSpecification) method to add the attachment.
- Save the document using Document.Save(String) method.
The following code sample shows how to add an attachment to a PDF document using C#.
Extract PDF Attachment using C#
First of all, let’s check out how to retrieve attachments from a PDF document. For this, follow the below steps:
- Create an instance of the Document class.
- Get the attachments into an EmbeddedFileCollection object using Document.EmbeddedFiles property.
- Loop through attachments in the EmbeddedFileCollection using FileSpecification object.
- Access properties of each attachment using FileSpecification object.
- Save the attachment as a file (if required).
The following code sample shows how to extract PDF attachments using C#.
Remove Attachments from PDF using C#
You can either remove all or a particular attachment from a PDF file. For this, Aspose.PDF for .NET provides the following methods:
- Delete() – Deletes all the attachments.
- Delete(String fileName) – Deletes attachment by name.
- DeleteByKey(String Key) – Deletes attachment by key in the collection.
The following are the steps to delete attachments from a PDF.
- Create an instance of the Document class to load the PDF file.
- Use Document.EmbeddedFiles.Delete() (or other delete method) to delete attachments.
- Save the file using Document.Save(String) method.
The following code sample shows how to remove attachments from a PDF file in C#.
Conclusion
In this article, you have seen how to manipulate the attachments in PDF documents programmatically. The step by step guide, API references, and code samples have shown how to retrieve, add, and remove attachments in PDF files using C#. You can learn more about Aspose.PDF for .NET using the documentation.