Easily Add and Remove Attachments in Your PDF Documents Using C#

jollenmoyani

Jollen Moyani

Posted on June 6, 2023

Easily Add and Remove Attachments in Your PDF Documents Using C#

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:

Getting started with app creation

First, create an application with the Syncfusion .NET PDF Library by following these steps:

  1. Create a console application using Visual Studio. Choose the Console App option
  2. Then, install the Syncfusion.Pdf.Net.Core NuGet package from NuGet.org as a reference to your .NET Standard application. Install the Syncfusion.Pdf.Net.Core NuGet package

Add an attachment in a PDF using C#

Follow these steps to add file attachments to your PDF document using the Syncfusion .NET PDF Library:

1.Use the PdfLoadedDocument class to load an existing PDF document where you want to add attachments.
2.Then, load an attachment from a file to the PdfAttachment class.
3.You can modify the properties of the attachment using the PdfAttachment object.
4.Finally, save the PDF file using the Save method.

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();
Enter fullscreen mode Exit fullscreen mode

Execute this code example to get a PDF document with an attachment like the following screenshot.

Adding an attachment to a PDF document

Adding an attachment to a PDF document

Extract attachments from a PDF using C#

Let’s see how to extract all the attachments from a PDF file:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Then, iterate the required attachments from the PDF using the PdfAttachment class.
3.Extract all the attachments using the PdfAttachmentCollection class.
4.Finally, save the extracted attachments on the disk.

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);
Enter fullscreen mode Exit fullscreen mode

Execute this code example to get an extracted text file like the following screenshot.

Extracting attachments in a PDF document

Extracting attachments in a PDF document

Remove attachments from a PDF using C#

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:

1.Use the PdfLoadedDocument class to load an existing PDF document.
2.Then, get the attachment that you want to remove from the PDF using the PdfAttachment class.
3.Remove the required attachment file in the PdfAttachmentCollection class using the Remove method or by passing the index value of the attachment to the RemoveAt method. Or remove all the attachments using the Clear method.
4.Finally, save the PDF file using the Save method.

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();
Enter fullscreen mode Exit fullscreen mode

Execute this code example to get a PDF document like the following screenshot.

Removing attachments from a PDF document

Removing attachments from a PDF document

References

Check out the code example for this blog on GitHub and the documentation about PDF attachments.

Conclusion

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!

Related blogs

💖 💪 🙅 🚩
jollenmoyani
Jollen Moyani

Posted on June 6, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related