Easily Add and Remove Attachments in Your PDF Documents Using C#
Live Chat Icon For mobile
Live Chat Icon
Popular Categories.NET  (173).NET Core  (29).NET MAUI  (203)Angular  (107)ASP.NET  (51)ASP.NET Core  (82)ASP.NET MVC  (89)Azure  (40)Black Friday Deal  (1)Blazor  (211)BoldSign  (13)DocIO  (24)Essential JS 2  (106)Essential Studio  (200)File Formats  (65)Flutter  (132)JavaScript  (219)Microsoft  (118)PDF  (81)Python  (1)React  (98)Streamlit  (1)Succinctly series  (131)Syncfusion  (897)TypeScript  (33)Uno Platform  (3)UWP  (4)Vue  (45)Webinar  (50)Windows Forms  (61)WinUI  (68)WPF  (157)Xamarin  (161)XlsIO  (35)Other CategoriesBarcode  (5)BI  (29)Bold BI  (8)Bold Reports  (2)Build conference  (8)Business intelligence  (55)Button  (4)C#  (146)Chart  (127)Cloud  (15)Company  (443)Dashboard  (8)Data Science  (3)Data Validation  (8)DataGrid  (63)Development  (618)Doc  (8)DockingManager  (1)eBook  (99)Enterprise  (22)Entity Framework  (5)Essential Tools  (14)Excel  (39)Extensions  (22)File Manager  (6)Gantt  (18)Gauge  (12)Git  (5)Grid  (31)HTML  (13)Installer  (2)Knockout  (2)Language  (1)LINQPad  (1)Linux  (2)M-Commerce  (1)Metro Studio  (11)Mobile  (501)Mobile MVC  (9)OLAP server  (1)Open source  (1)Orubase  (12)Partners  (21)PDF viewer  (42)Performance  (12)PHP  (2)PivotGrid  (4)Predictive Analytics  (6)Report Server  (3)Reporting  (10)Reporting / Back Office  (11)Rich Text Editor  (12)Road Map  (12)Scheduler  (52)Security  (3)SfDataGrid  (9)Silverlight  (21)Sneak Peek  (31)Solution Services  (4)Spreadsheet  (11)SQL  (10)Stock Chart  (1)Surface  (4)Tablets  (5)Theme  (12)Tips and Tricks  (112)UI  (381)Uncategorized  (68)Unix  (2)User interface  (68)Visual State Manager  (2)Visual Studio  (31)Visual Studio Code  (17)Web  (582)What's new  (323)Windows 8  (19)Windows App  (2)Windows Phone  (15)Windows Phone 7  (9)WinRT  (26)
Easily Add and Remove Attachments in Your PDF Documents Using CSharp

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:

Transform your PDF files effortlessly in C# with just five lines of code using Syncfusion's comprehensive PDF Library!

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();

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

Explore the wide array of rich features in Syncfusion's PDF Library through step-by-step instructions and best practices.

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);

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

Embark on a virtual tour of Syncfusion's PDF Library through interactive demos.

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();

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.

Join thousands of developers who rely on Syncfusion for their PDF needs. Experience the difference today!

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

Tags:

Share this post:

Popular Now

Be the first to get updates

Subscribe RSS feed

Be the first to get updates

Subscribe RSS feed