PDFium Delphi Component Docs

ExtractAttachmentsToStreams method

Component: TPdf  ·  Unit: PDFium
Decodes every document-level embedded attachment and writes the payload bytes to caller-supplied streams under explicit size, chunking, cancellation and error-reporting controls

Syntax

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

DestinationsOpen array of TStream. One stream per attachment, in document order. A nil slot skips that attachment without writing anything. The streams stay owned by the caller
OptionsTPdfAttachmentBatchOptions. Batch limits and behaviour: MaxAttachments, MaxFileBytes, MaxTotalBytes, ChunkBytes and StopOnError. Start from TPdfAttachmentBatchOptions.Default
ReportTPdfAttachmentBatchReport. On return carries one item per attachment (Metadata, State, BytesWritten, ErrorMessage) plus aggregate counters (AttachmentCount, ExtractedCount, SkippedCount, FailedCount, BytesWritten, PeakBufferBytes, Cancelled, Completed)
CancellationTokenIPdfCancellationToken. Optional token polled before each attachment and between write chunks; nil runs the batch without cancellation checks
ResultBoolean. True only when the batch completed without cancellation and with zero failed attachments; skipped entries do not count as failures

Description

Batch counterpart of reading Attachment one index at a time. The document attachments are captured once, and each payload is decoded and pushed into its destination stream, with every outcome recorded per attachment in Report.Items

The destination array must contain exactly one stream per attachment in the document, otherwise an EPdfError is raised before any byte is read. A nil entry in the array marks that attachment as skipped (paesSkipped) rather than aborting the batch

Options are validated up front and raise EPdfError when invalid: MaxAttachments must be greater than zero, MaxFileBytes and MaxTotalBytes cannot be negative, and ChunkBytes must be greater than zero. TPdfAttachmentBatchOptions.Default supplies 10000 attachments, a 256 MB per-file limit, a 1 GB aggregate limit, 1 MB write chunks and StopOnError = True

Each attachment ends in one of the TPdfAttachmentExtractionState states: paesExtracted on success, paesSkipped for a nil destination, paesUnreadable when the payload cannot be opened or decoded, paesFileLimit or paesAggregateLimit when a size limit is exceeded, paesWriteFailed when the destination stream raises, and paesCancelled when the token fires. With StopOnError set, the first failure stops the loop and the remaining items stay paesNotProcessed

The function returns True only when Report.Completed is set (no cancellation, nothing left unprocessed) and FailedCount is zero. The cancellation path returns False with Report.Cancelled set instead of raising

Remarks

Example

var
  Options: TPdfAttachmentBatchOptions;
  Report: TPdfAttachmentBatchReport;
  Streams: array of TStream;
  I: Integer;
begin
  Pdf1.FileName := 'with-attachments.pdf';
  Pdf1.Active := True;
  SetLength(Streams, Pdf1.AttachmentCount);
  for I := 0 to High(Streams) do
    Streams[I] := TFileStream.Create('out\' + Pdf1.AttachmentName[I], fmCreate);
  try
    Options := TPdfAttachmentBatchOptions.Default;
    Options.StopOnError := False;
    if not Pdf1.ExtractAttachmentsToStreams(Streams, Options, Report) then
      ShowMessage(Format('%d extracted, %d failed, %d skipped',
        [Report.ExtractedCount, Report.FailedCount, Report.SkippedCount]));
  finally
    for I := 0 to High(Streams) do
      Streams[I].Free;
  end;
end;

See Also

ReadAttachmentMetadata, Attachment, AttachmentCount, AttachmentName