Class PdfAttachment
Represents a file attachment embedded within a PDF document. Provides access to attachment metadata and binary data for extraction or modification.
PDF attachments are complete files stored within the PDF container. They appear as paperclip icons in PDF readers and can contain any file type (spreadsheets, images, documents, etc.).
Example - Extract attachment to disk:
var pdf = PdfDocument.FromFile("invoice_with_receipt.pdf");
foreach (var attachment in pdf.Attachments)
{
Console.WriteLine($"Found: {attachment.Name} at index {attachment.Index}");
File.WriteAllBytes(attachment.Name, attachment.Data);
}
Namespace: IronPdf
Assembly: IronPdf.dll
Syntax
public class PdfAttachment : PdfClientAccessor
PDF generation in IronPDF is handled through PdfAttachment. It represents a file attachment embedded within a PDF document.
PdfAttachment 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 PdfAttachment, instantiate or obtain it from the relevant entry point in the IronPDF C# API. Key properties include Data, Index, Name. Assign options or invoke methods on the instance to configure or perform the operation. The extract text and images covers typical usage in C# end to end.
using IronPdf;
// Obtain PdfAttachment from the relevant entry point in the IronPDF API
void Configure(PdfAttachment instance)
{
var current = instance.Data;
}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 PdfAttachment directly. PdfAttachment instances inherit additional members from PdfClientAccessor that may be relevant in advanced scenarios. In application code, treat PdfAttachment 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 PdfAttachment property after each operation to confirm the configured state. See the constructors, properties, and methods tables below for the complete API surface of PdfAttachment. Application code typically obtains or instantiates a single PdfAttachment and shares it across multiple IronPDF operations rather than recreating it per call.
Properties
Data
Gets or sets the raw binary data of the attachment. Read to extract, write to replace content while preserving the filename.
Example - Extract and modify:
// Extract attachment data:
byte[] data = attachment.Data;
File.WriteAllBytes($"extracted_{attachment.Name}", data);
// Replace attachment content:
attachment.Data = File.ReadAllBytes("updated_receipt.pdf");
pdf.SaveAs("invoice_updated.pdf");
Declaration
public byte[] Data { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Byte[] | The complete binary content of the attached file. |
Remarks
Reading this property retrieves data from the PDF. For large attachments, consider caching the result if accessed multiple times.
Index
Gets the zero-based index of this attachment within the PDF document. Index may change if attachments are added or removed (alphabetically sorted).
Declaration
public int Index { get; }
Property Value
| Type | Description |
|---|---|
| System.Int32 | Zero-based position in the attachment collection. |
Name
Gets the filename of this attachment as stored in the PDF document. Typically includes file extension (e.g., "receipt.pdf", "data.xlsx").
Use this name when saving extracted attachments to preserve original filenames.
Declaration
public string Name { get; }
Property Value
| Type | Description |
|---|---|
| System.String | The attachment filename including extension. |