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.
How to Add and Remove PDF Attachments
Add Attachment Example
To add a file as an attachment, first load it in your program as byte []. The easiest way to do this is to use the File.ReadAllBytes
method. With the file loaded in as 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")
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:
From there, you can click on it and save the attachment to your own storage.
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")
After removing the attachment and opening the resulting file in a PDF viewer, it can be seen that the attachment no longer appears: