How to Add and Remove PDF Attachments

Attachments in a PDF document refer to files or additional data embedded within the PDF file itself. This is distinct from the regular content of the PDF, which includes visible text, images, and formatting when you view the PDF. These attachments can take the form of various file types, including images, documents, spreadsheets, or other formats. Typically, attachments are used to provide additional reference materials or supplementary data that users can access when they open the PDF.

When it comes to working with attachments in IronPDF, the process is straightforward and user-friendly.


Add Attachment Example

To add a file as an attachment, first load it in your program as a byte[]. The easiest way to do this is to use the File.ReadAllBytes method. With the file loaded in as a byte[], you can then use the AddAttachment method to add the object into a PDF as an attachment like so:

:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-add-attachment.cs
using IronPdf;
using System.IO;

// Import attachment file
byte[] fileData = File.ReadAllBytes(@"path/to/file");

// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("sample.pdf");

// Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData);

pdf.SaveAs("addAttachment.pdf");
Imports IronPdf
Imports System.IO

' Import attachment file
Private fileData() As Byte = File.ReadAllBytes("path/to/file")

' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("sample.pdf")

' Add attachment to the PDF
pdf.Attachments.AddAttachment("Example", fileData)

pdf.SaveAs("addAttachment.pdf")
$vbLabelText   $csharpLabel

The AddAttachment function outputs a PdfAttachment object that we can keep for future reference or remove it later if needed.

After saving the PDF, you can open the attachment from the toolbar of a PDF viewer. We demonstrate where to find this feature in Google Chrome's PDF Viewer in the image below:

Attachment Preview

From there, you can click on it and save the attachment to your own storage.


Retrieve Attachment Example

The attachments in a PDF could be retrieved as binary data by accessing the Attachments property of the PdfDocument object. With the binary data, you can export the attachments from the PDF as their respective file formats.

:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-retrieve-attachment.cs
using IronPdf;
using System.IO;

// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");

// Iterate through all attachments
foreach (var attachment in pdf.Attachments)
{
    if (attachment.Name.Contains("Example"))
    {
        // Save byte to file
        File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data);
    }
}
Imports IronPdf
Imports System.IO

' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf")

' Iterate through all attachments
For Each attachment In pdf.Attachments
	If attachment.Name.Contains("Example") Then
		' Save byte to file
		File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data)
	End If
Next attachment
$vbLabelText   $csharpLabel

Remove Attachment Example

To remove an attachment, simply use the RemoveAttachment function. This method requires a reference to the attachment, which can be retrieved from the Attachments property. We demonstrate how to do this using the saved file from above.

:path=/static-assets/pdf/content-code-examples/how-to/add-remove-attachments-remove-attachment.cs
using IronPdf;
using System.Linq;

// Open existing PDF
PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf");

// Add attachment to the PDF
PdfAttachmentCollection retrieveAttachments = pdf.Attachments;

// Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First());

pdf.SaveAs("removeAttachment.pdf");
Imports IronPdf
Imports System.Linq

' Open existing PDF
Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf")

' Add attachment to the PDF
Private retrieveAttachments As PdfAttachmentCollection = pdf.Attachments

' Remove attachment from PDF
pdf.Attachments.RemoveAttachment(retrieveAttachments.First())

pdf.SaveAs("removeAttachment.pdf")
$vbLabelText   $csharpLabel

After removing the attachment and opening the resulting file in a PDF viewer, you will see that the attachment no longer appears:

Attachment Preview

Ready to see what else you can do? Check out our tutorial page here: Organize PDFs

Frequently Asked Questions

How can I add attachments to a PDF document using C#?

You can add attachments to a PDF document using IronPDF by loading the file as a byte array with the File.ReadAllBytes method and then utilizing the AddAttachment method to embed it into the PDF.

What is the process to remove attachments from a PDF?

To remove attachments from a PDF using IronPDF, employ the RemoveAttachment method. You must first obtain a reference to the attachment from the Attachments property of the PdfDocument object.

Which file formats can be added as attachments in a PDF?

A wide range of file formats can be attached to a PDF, including images, documents, spreadsheets, and other file types.

How do I retrieve and export attachments from a PDF?

To retrieve attachments from a PDF using IronPDF, access the Attachments property of the PdfDocument object to get them as binary data, which can then be exported to disk.

What steps are necessary to begin managing PDF attachments in C#?

To start managing PDF attachments, download the IronPDF C# library from NuGet, load or create a PDF document, and use the relevant methods to add or remove attachments.

Can attachments in a PDF be accessed and saved by users?

Yes, once attachments are added to a PDF using IronPDF, they can be accessed through a PDF viewer's toolbar, allowing users to save them to their storage.

How do I ensure changes to PDF attachments are saved?

After modifying attachments in a PDF using IronPDF, use the SaveAs method to save the updated PDF document to the desired location.

What is the difference between visible PDF content and attachments?

Visible PDF content includes text, images, and formatting, while attachments are additional files or data embedded within the PDF that provide supplementary information.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...Read More