PDFium Delphi Component API Reference

Attachment batch extraction

Component: TPdf  ·  Unit: PDFium
Inventory embedded attachments once and write their decoded payloads to caller-owned streams under explicit resource controls

ReadAttachmentMetadata

function ReadAttachmentMetadata(MaxAttachments: Integer = 10000): TPdfAttachmentMetadataArray;

The method enumerates document-level attachments once and returns caller-owned records containing Index, Name, Description, MimeType, Relationship, decoded Size, and Readable

MaxAttachments is checked before the result array is populated, preventing an untrusted document from causing an unbounded metadata allocation

The size query does not copy payload bytes into PDFium Delphi Component memory, although PDFium may decode a stream internally to determine its decoded length

ExtractAttachmentsToStreams

function ExtractAttachmentsToStreams(const Destinations: array of TStream; const Options: TPdfAttachmentBatchOptions; out Report: TPdfAttachmentBatchReport; const CancellationToken: IPdfCancellationToken = nil): Boolean;

Destinations must contain exactly one slot per document attachment and follows the same zero-based order as Attachment[Index]

A nil destination marks that item as paesSkipped, allowing callers to select attachments without a second enumeration

Each non-empty payload is decoded through PDFium into one temporary PDFium Delphi Component buffer and then written from that buffer in ChunkBytes pieces

The buffer is released before the next attachment, so PDFium Delphi Component never retains the whole batch, but the public PDFium attachment API does not support range reads and may use its own full-payload decode memory internally

Streams remain owned by the caller, writing begins at each current stream position, and the method does not rewind or truncate them

Cancellation or a destination exception can leave bytes already written to that stream, so callers that require transactional output should supply temporary streams and publish them only after a successful report

Options

FieldDefaultMeaning
MaxAttachments10000Maximum metadata and report entries
MaxFileBytes256 MiBMaximum decoded size of one attachment
MaxTotalBytes1 GiBMaximum bytes written across all destinations
ChunkBytes1 MiBMaximum bytes passed to one stream write
StopOnErrorTrueStop after the first unreadable, over-budget, or write-failed item

Report

Each TPdfAttachmentMetadata record describes one native attachment and TPdfAttachmentMetadataArray stores the metadata snapshot

Each TPdfAttachmentBatchItem combines metadata, a TPdfAttachmentExtractionState, bytes written, and any error text, while TPdfAttachmentBatchItems stores all per-item results

TPdfAttachmentExtractionState values are paesNotProcessed, paesSkipped, paesExtracted, paesUnreadable, paesFileLimit, paesAggregateLimit, paesCancelled, and paesWriteFailed

The TPdfAttachmentBatchReport counts AttachmentCount, ExtractedCount, SkippedCount, and FailedCount alongside BytesWritten and PeakBufferBytes

Completed means every item reached a terminal state without cancellation, while the function result additionally requires FailedCount = 0

PeakBufferBytes reports the largest PDFium Delphi Component payload buffer and does not include private memory used by PDFium during decode

Example

var Options: TPdfAttachmentBatchOptions;
var Report: TPdfAttachmentBatchReport;
Options := TPdfAttachmentBatchOptions.Default;
Options.MaxFileBytes := 64 * 1024 * 1024;
Options.MaxTotalBytes := 256 * 1024 * 1024;
if not Pdf1.ExtractAttachmentsToStreams(Destinations, Options, Report) then
  HandleAttachmentBatchFailure(Report);

See Also

Attachment, AttachmentCount, AttachmentDescription, AttachmentName, AttachmentType