File attachments in PDF documents refer to the feature that enables the inclusion of supplementary files or documents within a PDF document. The PDF attachment files can be beneficial when sending related documents, such as a report with supporting documents or a portfolio of design work.
The Syncfusion PDF Library is a feature-rich and high-performance .NET library that supports adding attachments to a PDF document from within the application code. The library also provides APIs for extracting attachments from PDF documents and modifying and deleting existing attachments.
This article will cover adding, extracting, and removing file attachments from a PDF document using the Syncfusion .NET PDF Library.
Agenda:
First, create an application with the Syncfusion .NET PDF Library by following these steps:
Follow these steps to add file attachments to your PDF document using the Syncfusion .NET PDF Library:
Refer to the following code example to add an attachment to a PDF file using C#.
//Load the PDF document. FileStream docStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read); PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Creates an attachment. Stream fileStream = new FileStream("Input.txt", FileMode.Open, FileAccess.Read); PdfAttachment attachment = new PdfAttachment("Input.txt", fileStream); attachment.ModificationDate = DateTime.Now; attachment.Description = "Input.txt"; attachment.MimeType = "application/txt"; if (loadedDocument.Attachments == null) loadedDocument.CreateAttachment(); //Add the attachment to the document. loadedDocument.Attachments.Add(attachment); FileStream stream = new FileStream("output.pdf", FileMode.Create); //Save the modified document to a file. loadedDocument.Save(stream); //Close the PDF document. loadedDocument.Close(true); stream.Close();
Execute this code example to get a PDF document with an attachment like the following screenshot.
Let’s see how to extract all the attachments from a PDF file:
Refer to the following code example to extract attachments from an existing PDF document using C#.
//Load an existing PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Iterate the attachments. foreach (PdfAttachment attachment in loadedDocument.Attachments) { //Extracts the attachment and saves it to the disk. FileStream s = new FileStream(attachment.FileName, FileMode.Create); s.Write(attachment.Data, 0, attachment.Data.Length); s.Dispose(); } //Close the PDF document. loadedDocument.Close(true);
Execute this code example to get an extracted text file like the following screenshot.
Removing a file attachment from a PDF document can be useful in cases where the attachment is no longer needed or when the PDF file needs to be shared without the attached file.
You can either remove all or specific attachments from PDF documents using the following methods:
Let’s see the steps to remove attachments from a PDF document:
Refer to the following code example to remove an attachment from a PDF document using C#.
//Load the PDF document. PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream); //Removes an attachment. PdfAttachmentCollection attachments = loadedDocument.Attachments; attachments.RemoveAt(0); attachments.Remove(attachments[0]); attachments.Clear(); FileStream stream = new FileStream("output.pdf", FileMode.Create); //Save the modified document to file. loadedDocument.Save(stream); //Close the PDF document. loadedDocument.Close(true); stream.Close();
Execute this code example to get a PDF document like the following screenshot.
Check out the code example for this blog on GitHub and the documentation about PDF attachments.
Thanks for reading! In this blog, we learned to add, extract, and remove file attachments in your .NET console application using our Syncfusion PDF Library. Try out these methods and leave your feedback in the comments section of this blog post!
Take a moment to peruse the PDF Library documentation to check out the other advanced features, such as form authentication, tables of contents, and automatic bookmark hierarchy based on heading styles.
For existing customers, the new version of Essential Studio® is available for download from the License and Downloads page. If you are not a Syncfusion customer, try our 30-day free trial to check out our available features.
For questions, you can contact us through our support forum, support portal, or feedback portal. We are delighted to assist you!