PDFium Delphi Component Docs

TryGetAnnotationFileAttachment method

Component: TPdf  ·  Unit: PDFium
Reads the embedded file carried by a file-attachment annotation on the current page, returning its name, description, MIME subtype and decoded payload bytes

Syntax

function TryGetAnnotationFileAttachment(AnnotIndex: Integer; out Attachment: TPdfAnnotationAttachment; MaxBytes: Cardinal = 67108864): Boolean;

AnnotIndexInteger. Zero-based index of the annotation on the current page, in the range 0 .. AnnotationCount - 1. An out-of-range index raises an EPdfError
AttachmentTPdfAnnotationAttachment. On success: Name, Description, MimeType, Relationship (the /AFRelationship name, empty when absent or when the export is missing) and the decoded payload in Data (TBytes). Cleared to its default before the call runs
MaxBytesCardinal. Upper bound on the payload size. The default of 67108864 allows up to 64 MB; a larger payload raises an EPdfError
ResultBoolean. True when the annotation carries a file attachment and the payload was read completely; False when it has none or the payload could not be decoded

Description

A file-attachment annotation pins an embedded file to a point on a page. This call opens the annotation through the native interface (FPDFAnnot_GetFileAttachment) and, when the annotation carries an attachment, reads its descriptive fields and the full decoded payload into a TPdfAnnotationAttachment record

The result is a Try-pattern call: an annotation without an attachment returns False instead of raising, and a payload that fails to decode also returns False. Only an oversized payload becomes an exception, so the MaxBytes limit can act as a hard guard against memory exhaustion from hostile documents

Annotation attachments are separate from the document-level EmbeddedFiles tree exposed by Attachment: an attachment can sit on a page annotation without appearing in that tree, and vice versa. Use AddAnnotationFileAttachment to attach data to an existing annotation in one call

Remarks

Example

var
  I: Integer;
  Attachment: TPdfAnnotationAttachment;
begin
  for I := 0 to Pdf1.AnnotationCount - 1 do
    if (Pdf1.Annotation[I].Subtype = anFileAttachment) and
      Pdf1.TryGetAnnotationFileAttachment(I, Attachment) then
    begin
      TFile.WriteAllBytes('extracted\' + Attachment.Name, Attachment.Data);
      Memo1.Lines.Add(Format('%s (%s, %d bytes)',
        [Attachment.Name, Attachment.MimeType, Length(Attachment.Data)]));
    end;
end;

See Also

AddAnnotationFileAttachment, Annotation, AnnotationCount, Attachment