IronPDF How-Tos Add & Remove Attachments How to Add and Remove PDF Attachments ByJordi Bardia September 11, 2023 Updated June 22, 2025 Share: 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. View the IronPDF YouTube Playlist How to Add and Remove PDF Attachments Download the IronPDF C# library from NuGet Load an existing PDF or render a new one Import the file to be attached as byte[] using the File.ReadAllBytes method Use the AddAttachment method to attach it to the PDF Remove the attachment from the PDF with the RemoveAttachment method 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 // Import the necessary namespaces for working with PDFs and file I/O using IronPdf; using System.IO; // Ensure the path to the file you want to attach is correct. Replace "path/to/file" with an actual file path. string filePath = @"path/to/file"; // Check if the file exists to prevent errors during file reading. if (File.Exists(filePath)) { // Load all bytes from the specified file into a byte array. byte[] fileData = File.ReadAllBytes(filePath); // Load an existing PDF document from a file named "sample.pdf". // Ensure the file "sample.pdf" exists in the same directory or provide a full path. if (File.Exists("sample.pdf")) { PdfDocument pdf = PdfDocument.FromFile("sample.pdf"); // Add an attachment to the PDF document using the previously loaded file data. // The attachment is given a name "Example" which will appear in the PDF's attachment pane. pdf.Attachments.AddAttachment("Example", fileData); // Save the modified PDF with the attachment to a new file called "addAttachment.pdf". // Ensure you have write permissions to the directory to save the file. pdf.SaveAs("addAttachment.pdf"); } else { // Handle the case where "sample.pdf" does not exist. System.Console.WriteLine("The file sample.pdf does not exist."); } } else { // Handle the case where the file to be attached does not exist. System.Console.WriteLine($"The file {filePath} does not exist."); } ' Import the necessary namespaces for working with PDFs and file I/O Imports IronPdf Imports System.IO ' Ensure the path to the file you want to attach is correct. Replace "path/to/file" with an actual file path. Private filePath As String = "path/to/file" ' Check if the file exists to prevent errors during file reading. If File.Exists(filePath) Then ' Load all bytes from the specified file into a byte array. Dim fileData() As Byte = File.ReadAllBytes(filePath) ' Load an existing PDF document from a file named "sample.pdf". ' Ensure the file "sample.pdf" exists in the same directory or provide a full path. If File.Exists("sample.pdf") Then Dim pdf As PdfDocument = PdfDocument.FromFile("sample.pdf") ' Add an attachment to the PDF document using the previously loaded file data. ' The attachment is given a name "Example" which will appear in the PDF's attachment pane. pdf.Attachments.AddAttachment("Example", fileData) ' Save the modified PDF with the attachment to a new file called "addAttachment.pdf". ' Ensure you have write permissions to the directory to save the file. pdf.SaveAs("addAttachment.pdf") Else ' Handle the case where "sample.pdf" does not exist. System.Console.WriteLine("The file sample.pdf does not exist.") End If Else ' Handle the case where the file to be attached does not exist. System.Console.WriteLine($"The file {filePath} does not exist.") End If $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: 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; // Load an existing PDF document using IronPdf // The PDF document "addAttachment.pdf" should be present in the execution folder or its full path should be specified. PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf"); // Iterate through all attachments in the PDF document foreach (var attachment in pdf.Attachments) { // Check if the attachment's name contains the string "Example" if (attachment.Name.Contains("Example")) { // Save the attachment's data (byte array) as a file // The file is saved with the same name as the attachment, but with a .doc extension File.WriteAllBytes($"{attachment.Name}.doc", attachment.Data); } } Imports IronPdf Imports System.IO ' Load an existing PDF document using IronPdf ' The PDF document "addAttachment.pdf" should be present in the execution folder or its full path should be specified. Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf") ' Iterate through all attachments in the PDF document For Each attachment In pdf.Attachments ' Check if the attachment's name contains the string "Example" If attachment.Name.Contains("Example") Then ' Save the attachment's data (byte array) as a file ' The file is saved with the same name as the attachment, but with a .doc extension 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; // Load an existing PDF document from a file PdfDocument pdf = PdfDocument.FromFile("addAttachment.pdf"); // Retrieve the collection of attachments from the PDF document PdfAttachmentCollection attachments = pdf.Attachments; // Check if there are any attachments present in the document if (attachments.Any()) { // Remove the first attachment in the collection attachments.Remove(attachments.First()); // Note: Using 'Remove' method of PdfAttachmentCollection to remove the attachment } // Save the modified PDF to a new file with the specified name pdf.SaveAs("removeAttachment.pdf"); Imports IronPdf Imports System.Linq ' Load an existing PDF document from a file Private pdf As PdfDocument = PdfDocument.FromFile("addAttachment.pdf") ' Retrieve the collection of attachments from the PDF document Private attachments As PdfAttachmentCollection = pdf.Attachments ' Check if there are any attachments present in the document If attachments.Any() Then ' Remove the first attachment in the collection attachments.Remove(attachments.First()) ' Note: Using 'Remove' method of PdfAttachmentCollection to remove the attachment End If ' Save the modified PDF to a new file with the specified name 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: 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 Chat with engineering team now 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. Ready to Get Started? Free NuGet Download Total downloads: 14,403,271 View Licenses