Class PdfAttachmentCollection
Manages the collection of file attachments embedded in a PDF document. Supports adding, removing, and iterating through attachments.
Access this collection via Attachments property.
Example - Manage attachments:
var pdf = PdfDocument.FromFile("document.pdf");
// Add attachment:
byte[] excelData = File.ReadAllBytes("report.xlsx");
pdf.Attachments.AddAttachment("Q4_Report.xlsx", excelData);
// List all attachments:
foreach (var att in pdf.Attachments)
Console.WriteLine($"{att.Index}: {att.Name}");
// Remove first attachment:
pdf.Attachments.RemoveAttachment(pdf.Attachments[0]);
pdf.SaveAs("modified.pdf");
Namespace: IronPdf
Assembly: IronPdf.dll
Syntax
public class PdfAttachmentCollection : PdfClientAccessor
PDF generation in IronPDF is handled through PdfAttachmentCollection. It manages the collection of file attachments embedded in a PDF document.
PdfAttachmentCollection matters when an application needs to configure or invoke PDF generation from C# code. The class encapsulates the related options and behavior in a single object that is set up once and reused across render or processing calls. Typical scenarios include batch generation pipelines, templated document workflows, and integration with existing C# document services.
To use PdfAttachmentCollection, instantiate or obtain it from the relevant entry point in the IronPDF C# API. Key properties include Item[Int32]. Assign options or invoke methods on the instance to configure or perform the operation. The add remove attachments covers typical usage in C# end to end.
using IronPdf;
// Obtain PdfAttachmentCollection from the relevant entry point in the IronPDF API
void Configure(PdfAttachmentCollection instance)
{
var current = instance.Item[Int32];
instance.AddAttachment();
}For the broader workflow, see the access PDF DOM object guide in the IronPDF C# documentation. For broader context, the PDF generation portion of the IronPDF C# API contains related types that work with PdfAttachmentCollection directly. PdfAttachmentCollection instances inherit additional members from PdfClientAccessor that may be relevant in advanced scenarios. In application code, treat PdfAttachmentCollection as a configured object that is constructed once and reused across operations rather than instantiated per call. Configuration is generally idempotent: assigning the same property value twice has the same effect as assigning it once. For diagnostic purposes, inspect the relevant PdfAttachmentCollection property after each operation to confirm the configured state. See the constructors, properties, and methods tables below for the complete API surface of PdfAttachmentCollection. Application code typically obtains or instantiates a single PdfAttachmentCollection and shares it across multiple IronPDF operations rather than recreating it per call.
Properties
Item[Int32]
Gets the attachment at the specified index within this collection.
Example:
var firstAttachment = pdf.Attachments[0];
File.WriteAllBytes(firstAttachment.Name, firstAttachment.Data);
Declaration
public PdfAttachment this[int index] { get; }
Parameters
| Type | Name | Description |
|---|---|---|
| System.Int32 | index | Zero-based index of the attachment to retrieve. |
Property Value
| Type | Description |
|---|---|
| PdfAttachment | The PdfAttachment at the specified index. |
Exceptions
| Type | Condition |
|---|---|
| System.ArgumentOutOfRangeException | Index is outside the valid range. |
Methods
AddAttachment(String, Byte[])
Embeds a file as an attachment within the PDF document. The attachment appears as a paperclip icon in PDF readers and can be extracted by users.
Example - Attach supporting files:
// Attach Excel report:
byte[] excelData = File.ReadAllBytes("financials.xlsx");
pdf.Attachments.AddAttachment("Q4_Financials.xlsx", excelData);
// Attach multiple files:
foreach (var file in Directory.GetFiles("receipts"))
{
var data = File.ReadAllBytes(file);
pdf.Attachments.AddAttachment(Path.GetFileName(file), data);
}
Declaration
public PdfAttachment AddAttachment(string name, byte[] data)
Parameters
| Type | Name | Description |
|---|---|---|
| System.String | name | Filename for the attachment including extension (e.g., "report.xlsx", "receipt.pdf"). This name will appear in PDF readers. |
| System.Byte[] | data | Complete binary content of the file to attach.
Use |
Returns
| Type | Description |
|---|---|
| PdfAttachment | The newly created PdfAttachment for further manipulation. |
Remarks
Attachments increase PDF file size by the size of attached files.
For PDF/A-3 compliant attachments (e.g., ZUGFeRD invoices), see
Standards:
See Also
GetEnumerator()
Retrieve enumerator
Declaration
public IEnumerator<PdfAttachment> GetEnumerator()
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerator<PdfAttachment> | Enumerator |
RemoveAttachment(PdfAttachment)
Removes the specified attachment from the PDF document permanently.
Example - Remove sensitive attachments:
// Remove specific attachment by reference:
var pdf = PdfDocument.FromFile("document.pdf");
var sensitiveFile = pdf.Attachments.FirstOrDefault(
a => a.Name.Contains("confidential"));
if (sensitiveFile != null)
pdf.Attachments.RemoveAttachment(sensitiveFile);
// Remove all attachments:
while (pdf.Attachments.Any())
pdf.Attachments.RemoveAttachment(pdf.Attachments[0]);
pdf.SaveAs("cleaned.pdf");
Declaration
public void RemoveAttachment(PdfAttachment attachment)
Parameters
| Type | Name | Description |
|---|---|---|
| PdfAttachment | attachment | The PdfAttachment instance to remove. Obtain from indexer or enumeration. |
Remarks
After removal, indexes of remaining attachments may change due to automatic alphabetical sorting.