How to Add and Remove PDF Attachments in C# with IronPDF
IronPDF enables you to programmatically add, retrieve, and remove file attachments in PDF documents using simple C# methods like AddAttachment() and RemoveAttachment(), allowing you to embed supplementary files directly into your PDFs.
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. This capability is particularly useful when creating comprehensive PDF reports or when you need to merge multiple PDFs with supporting documentation.
Quickstart: Adding Attachments to PDFAdd attachments to your PDF documents using IronPDF. This quick example demonstrates how to embed a file as an attachment into a PDF. Load your existing PDF, use the AddAttachment method, and save the updated document. This process ensures your supplementary materials are included with your PDF, making them accessible directly from any PDF viewer.
-
1Install IronPDF with NuGet Package Manager
-
2Copy and run this code snippet.
var pdf = IronPdf.PdfDocument.FromFile("example.pdf"); pdf.Attachments.AddAttachment("file.txt", System.IO.File.ReadAllBytes("file.txt")); pdf.SaveAs("updated.pdf");C# -
3Deploy to test on your live environment
Start using IronPDF in your project today with a free trial
Minimal Workflow (5 steps)
- Download the IronPDF C# library from NuGet
- Load an existing PDF or render a new one
- Import the file as
byte[]usingFile.ReadAllBytes - Use
AddAttachmentto attach it to the PDF - Remove attachments with
RemoveAttachment
How Do I Add a File as an Attachment to a PDF?
To add a file as an attachment, first load it as a byte[]. The easiest way to do this is to use the File.ReadAllBytes method. With the file loaded as a byte[], use the AddAttachment method to add the object into a PDF as an attachment:
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 you can keep for future reference or remove later if needed. This approach is similar to how you would add images to PDFs or manage other PDF assets.
After saving the PDF, you can open the attachment from the toolbar of a PDF viewer. The image below demonstrates where to find this feature in Google Chrome's PDF Viewer:

From there, you can click on it and save the attachment to your storage.
What File Types Can I Attach to a PDF?
IronPDF supports attaching virtually any file type to PDF documents. Common attachment types include:
- Office documents (DOCX, XLSX, PPTX)
- Images (JPG, PNG, GIF, SVG)
- Text files (TXT, CSV, XML)
- Archives (ZIP, RAR)
- Other PDFs
The attachment system works with binary data, so any file that can be read as bytes can be attached. When working with specific document types, you might also consider IronPDF's built-in conversion features, such as converting DOCX to PDF or converting images to PDF.
Where Do Attachments Appear in PDF Viewers?
Different PDF viewers display attachments in various locations:
- Adobe Acrobat: Shows a paperclip icon in the navigation pane
- Chrome PDF Viewer: Displays attachments in the left sidebar when clicked
- Firefox PDF Viewer: Shows attachments in a dedicated panel
- Microsoft Edge: Similar to Chrome, with a sidebar attachment view
Most modern PDF viewers support attachments, though the interface may vary slightly between applications.
What Happens to the PdfAttachment Object After Adding?
When you call AddAttachment(), IronPDF creates a PdfAttachment object that contains:
- Name: The display name of the attachment
- Data: The binary content of the attached file
- Index: The position of the attachment within the collection
This object is added to the PDF's internal attachment collection and remains accessible through the Attachments property until explicitly removed.
Attachments
How Can I Retrieve Attachments from an Existing PDF?
The attachments in a PDF can 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.
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 attachmentThis process is particularly useful when you need to extract content from PDFs or process attached documents programmatically.
How Do I Access Multiple Attachments in a PDF?
The Attachments property returns a collection that you can iterate through or query using LINQ:
// Get all attachments as a list
var allAttachments = pdf.Attachments.ToList();
// Filter attachments by size (e.g., files larger than 1MB)
var largeAttachments = pdf.Attachments
.Where(a => a.Data.Length > 1024 * 1024)
.ToList();
// Find specific attachment by exact name
var specificAttachment = pdf.Attachments
.FirstOrDefault(a => a.Name == "report.xlsx");' Get all attachments as a list
Dim allAttachments = pdf.Attachments.ToList()
' Filter attachments by size (e.g., files larger than 1MB)
Dim largeAttachments = pdf.Attachments _
.Where(Function(a) a.Data.Length > 1024 * 1024) _
.ToList()
' Find specific attachment by exact name
Dim specificAttachment = pdf.Attachments _
.FirstOrDefault(Function(a) a.Name = "report.xlsx")What Properties Are Available on Retrieved Attachments?
Each PdfAttachment object provides:
Name: The display name of the attachmentData: Binary content as byte arrayIndex: The position of the attachment within the collection
You can use these properties to identify, filter, and process attachments based on your requirements.
How Can I Filter Attachments by Name or Type?
Since attachments are stored with their display names, you can filter them using string operations:
// Filter by file extension (assuming names include extensions)
var imageAttachments = pdf.Attachments
.Where(a => a.Name.EndsWith(".jpg") ||
a.Name.EndsWith(".png") ||
a.Name.EndsWith(".gif"))
.ToList();
// Filter by name pattern
var reportsOnly = pdf.Attachments
.Where(a => a.Name.StartsWith("Report_"))
.ToList();' Filter by file extension (assuming names include extensions)
Dim imageAttachments = pdf.Attachments _
.Where(Function(a) a.Name.EndsWith(".jpg") OrElse _
a.Name.EndsWith(".png") OrElse _
a.Name.EndsWith(".gif")) _
.ToList()
' Filter by name pattern
Dim reportsOnly = pdf.Attachments _
.Where(Function(a) a.Name.StartsWith("Report_")) _
.ToList()The IronSuite play a crucial role in our operations. These are tools that increase efficiencies across the business including creating floor plans and improving inventory management.
How Do I Remove Attachments from a PDF?
To remove an attachment, use the RemoveAttachment function. This method requires a reference to the attachment, which can be retrieved from the Attachments property. Here's how to do this using the saved file from above:
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, you will see that the attachment no longer appears:

What Happens When I Remove an Attachment?
When you remove an attachment:
- The attachment data is completely removed from the PDF file
- The file size decreases by approximately the size of the removed attachment
- Any references to that attachment in the PDF structure are cleaned up
- The change is permanent once you save the PDF
This is similar to other PDF modification operations like removing pages or redacting content.
Can I Remove Multiple Attachments at Once?
Yes, you can remove multiple attachments in a single operation. Here's an example:
// Remove all attachments that match a pattern
var attachmentsToRemove = pdf.Attachments
.Where(a => a.Name.StartsWith("temp_"))
.ToList();
foreach (var attachment in attachmentsToRemove)
{
pdf.Attachments.RemoveAttachment(attachment);
}
// Or remove all attachments at once
while (pdf.Attachments.Count > 0)
{
pdf.Attachments.RemoveAttachment(pdf.Attachments.First());
}Imports System.Linq
' Remove all attachments that match a pattern
Dim attachmentsToRemove = pdf.Attachments _
.Where(Function(a) a.Name.StartsWith("temp_")) _
.ToList()
For Each attachment In attachmentsToRemove
pdf.Attachments.RemoveAttachment(attachment)
Next
' Or remove all attachments at once
While pdf.Attachments.Count > 0
pdf.Attachments.RemoveAttachment(pdf.Attachments.First())
End WhileHow Do I Verify an Attachment Was Successfully Removed?
You can verify attachment removal in several ways:
// Check the attachment count
int attachmentCountBefore = pdf.Attachments.Count;
pdf.Attachments.RemoveAttachment(targetAttachment);
int attachmentCountAfter = pdf.Attachments.Count;
// Verify the count decreased
if (attachmentCountAfter < attachmentCountBefore)
{
Console.WriteLine("Attachment successfully removed");
}
// Check if specific attachment exists
bool attachmentExists = pdf.Attachments
.Any(a => a.Name == "specificFile.txt");' Check the attachment count
Dim attachmentCountBefore As Integer = pdf.Attachments.Count
pdf.Attachments.RemoveAttachment(targetAttachment)
Dim attachmentCountAfter As Integer = pdf.Attachments.Count
' Verify the count decreased
If attachmentCountAfter < attachmentCountBefore Then
Console.WriteLine("Attachment successfully removed")
End If
' Check if specific attachment exists
Dim attachmentExists As Boolean = pdf.Attachments _
.Any(Function(a) a.Name = "specificFile.txt")Best Practices for PDF Attachments
When working with PDF attachments in IronPDF, consider these best practices:
- File Size Management: Be mindful of attachment sizes, as they directly increase PDF file size
- Naming Conventions: Use clear, descriptive names for attachments to help users identify them
- Security Considerations: When handling sensitive attachments, consider applying PDF passwords and permissions
- Performance: For large attachments or many files, consider using async operations to maintain application responsiveness
Ready to see what else you can do? Check out our tutorial page here: Organize PDFs
Frequently Asked Questions
How can I add a file as an attachment to a PDF using IronPDF?
To add a file as an attachment using IronPDF, first load the file as a byte array using `File.ReadAllBytes()`. Then, open your PDF with `PdfDocument.FromFile()` and use the `AddAttachment()` method to embed the file. Finally, save the document to preserve changes.
What types of files can be attached to a PDF with IronPDF?
IronPDF allows attaching a wide array of file types, including Office documents (DOCX, XLSX, PPTX), images (JPG, PNG, GIF), text files (TXT, CSV), and even other PDFs.
Where do attachments typically appear in PDF viewers?
Attachments are displayed differently depending on the PDF viewer. In Adobe Acrobat, they appear with a paperclip icon, while in Chrome's PDF Viewer, they are listed in the left sidebar.
How can I remove attachments from a PDF using IronPDF?
To remove an attachment, load your PDF using `PdfDocument.FromFile()`, access the attachment via the `Attachments` property, and employ the `RemoveAttachment()` method. Save the updated document afterward.
Can IronPDF handle multiple attachments simultaneously?
Yes, IronPDF can manage multiple attachments simultaneously. You can iterate through the `Attachments` collection for batch processing, like removing multiple attachments at once.
What does the `AddAttachment()` method return in IronPDF?
The `AddAttachment()` method returns a `PdfAttachment` object, which includes properties like Name, Data, and Index to manage the attachment within the PDF.
Is it possible to filter PDF attachments by type or name in IronPDF?
Yes, you can filter attachments using the `Attachments` collection based on file extensions or specific name patterns.
What are the best practices for managing PDF attachments in IronPDF?
Some best practices include managing file sizes, using descriptive naming conventions, securing sensitive content with passwords, and leveraging asynchronous operations for better performance.
How can I verify if an attachment was successfully removed?
You can verify attachment removal by checking the number of attachments before and after removal or confirming the absence of the specific attachment by name in the `Attachments` collection.
What happens to the PDF file size when attachments are removed using IronPDF?
Removing attachments with IronPDF reduces the file size roughly by the size of the attachment, as the attachment data is completely removed from the PDF file.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.