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

What are attachments in a PDF document?

Attachments in a PDF document are files or additional data embedded within the PDF, distinct from the visible text and images. They can include various file types and are used to provide supplementary materials.

How can I add an attachment to a PDF?

To add an attachment using IronPDF, load the file as a byte array using File.ReadAllBytes, then use the AddAttachment method to attach it to the PDF.

How do I remove an attachment from a PDF?

Use the RemoveAttachment function within IronPDF, providing a reference to the attachment obtained from the Attachments property of the PdfDocument object.

What is the purpose of the AddAttachment method?

The AddAttachment method in IronPDF is used to add files as attachments into a PDF document, allowing them to be accessed and saved by users.

How can I retrieve attachments from a PDF?

Attachments can be retrieved as binary data by accessing the Attachments property of a PdfDocument object in IronPDF, which can then be exported to disk.

What file types can be attached to a PDF?

Various file types can be attached to a PDF, including images, documents, spreadsheets, and other formats.

What is required to start adding PDF attachments?

You need to download the IronPDF C# library from NuGet, then load or create a PDF document to start adding or removing attachments.

Can attachments be removed after being added to a PDF?

Yes, attachments can be removed using the RemoveAttachment method in IronPDF, provided you have a reference to the attachment.

How do I save a PDF after modifying attachments?

After adding or removing attachments using IronPDF, use the SaveAs method to save the updated PDF to your desired location.

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 says it’s one of his favorite aspects of working with Iron Software. Jordi grew up in Miami, Florida and studied Computer Science and Statistics at University of Florida.