Easily Add and Remove Attachments in Your PDF Documents Using C#
Tags: fileformats, csharp, pdf
Published: 2023-06-06 15:28:51

---
title: Easily Add and Remove Attachments in Your PDF Documents Using C#
published: true
date: 2023-06-06 11:00:00 UTC
tags: fileformats, csharp, pdf
canonical_url: https://www.syncfusion.com/blogs/post/add-remove-attachment-pdf-csharp.aspx
cover_image: https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vvsigttqgrwapwea47yp.png
---
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](https://www.syncfusion.com/document-processing/pdf-framework/net/pdf-library "Syncfusion .NET 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](https://www.syncfusion.com/document-processing/pdf-framework/net/pdf-library "Syncfusion .NET PDF Library").

Agenda:

- [Add an attachment in a PDF using C#.](#Add)
- [Extract attachments from a PDF using C#.](#Extract)
- [Remove attachments from a PDF using C#.](#Remove)

## 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](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-console?view=vs-2022 "Tutorial: Create a simple C# console app in Visual Studio").  
 ![Choose the Console App option](https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Choose-the-Console-App-option.png)
2. Then, install the [Syncfusion.Pdf.Net.Core](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core "Syncfusion.Pdf.Net.Core NuGet Package") NuGet package from [NuGet.org](https://www.nuget.org/ "NuGet Gallery") as a reference to your .NET Standard application.  
 ![Install the Syncfusion.Pdf.Net.Core NuGet package](https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Install-the-Syncfusion.Pdf.Net_.Core-NuGet-package.png)

## <a name="Add">Add an attachment in a PDF using C#</a>

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

1.Use the [PdfLoadedDocument](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Parsing.PdfLoadedDocument.html "PdfLoadedDocument class of .NET PDF Library") class to load an existing PDF document where you want to add attachments.
2.Then, load an attachment from a file to the [PdfAttachment](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfAttachment.html "PdfAttachment class of .NET PDF Library") 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#.

```csharp
//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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Adding-an-attachment-to-a-PDF-document.png" alt="Adding an attachment to a PDF document" style="width:100%">

  <figcaption>Adding an attachment to a PDF document</figcaption>

</figure>

##<a name="Extract">Extract attachments from a PDF using C#</a>

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](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfAttachmentCollection.html "PdfAttachmentCollection class of .NET PDF Library") 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#.

```csharp
//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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Extracting-attachments-in-a-PDF-document.png" alt="Extracting attachments in a PDF document" style="width:100%">

  <figcaption>Extracting attachments in a PDF document</figcaption>

</figure>

##<a name="Remove">Remove attachments from a PDF using C#</a>

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:

- [Remove (PdfAttachment)](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfAttachmentCollection.html#Syncfusion_Pdf_Interactive_PdfAttachmentCollection_Remove_Syncfusion_Pdf_Interactive_PdfAttachment_ "Remove (PdfAttachment) method of .NET PDF Library")—To remove the specified attachment from the collection.
- [RemoveAt (int index)](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfAttachmentCollection.html#Syncfusion_Pdf_Interactive_PdfAttachmentCollection_RemoveAt_System_Int32_ "RemoveAt (int index) method of .NET PDF Library")—To remove attachment at the specified index.
- [Clear()](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Interactive.PdfAttachmentCollection.html#Syncfusion_Pdf_Interactive_PdfAttachmentCollection_Clear "Clear() method of .NET PDF Library")—To remove all the attachments from the collection.

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#.

```csharp
//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.
<figure>

  <img src="https://www.syncfusion.com/blogs/wp-content/uploads/2023/06/Removing-attachments-from-a-PDF-document.png" alt="Removing attachments from a PDF document" style="width:100%">

  <figcaption>Removing attachments from a PDF document</figcaption>

</figure>

## References

Check out the code example for this blog on [GitHub](https://github.com/SyncfusionExamples/create-pdf-attachments-csharp "Add and remove attachments in PDF using C# GitHub demo") and the [documentation](https://help.syncfusion.com/file-formats/pdf/working-with-attachments "Working with attachments in .NET PDF Library") 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](https://www.syncfusion.com/pdf-framework/net/pdf-library "Syncfusion .NET 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](https://help.syncfusion.com/file-formats/pdf/ "Overview of PDF Framework") 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](https://www.syncfusion.com/Account/Login "Essential Studio license and downloads page") page. If you are not a Syncfusion customer, try our 30-day [free trial](https://www.syncfusion.com/downloads "Free evaluation of the Essential Studio products") to check out our available features.

For questions, you can contact us through our [support forum](https://www.syncfusion.com/forums "Syncfusion Support Forum"), [support portal](https://support.syncfusion.com/ "Syncfusion Support Portal"), or [feedback portal](https://www.syncfusion.com/feedback/pdf "Syncfusion Feedback Portal"). We are delighted to assist you!

## Related blogs

- [How to Convert an Image to PDF in ASP.NET Core](https://www.syncfusion.com/blogs/post/how-to-convert-image-to-pdf-in-asp-net-core.aspx "Blog: How to Convert an Image to PDF in ASP.NET Core")
- [Easy Ways to Redact PDFs Using C#](https://www.syncfusion.com/blogs/post/easy-ways-to-redact-pdfs-using-c.aspx "Blog: Easy Ways to Redact PDFs Using C#")
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx "Blog: 7 Ways to Compress PDF Files in C#, VB.NET")
- [Digitally Sign and Verify Signatures in PDF Files Using C#](https://www.syncfusion.com/blogs/post/create-validate-pdf-digital-signatures-csharp.aspx "Blog: Digitally Sign and Verify Signatures in PDF Files Using C#")
- [Optical Character Recognition in PDF Using Tesseract Open-Source Engine](https://www.syncfusion.com/blogs/post/optical-character-recognition-in-pdf-using-tesseract-open-source-engine.aspx "Blog: Optical Character Recognition in PDF Using Tesseract Open-Source Engine")