PDFiumPas Docs

Reviewed font and text resource repair

RepairPdfDocumentText applies explicit font, ToUnicode and ActualText recipes to a SHA-256-bound source snapshot and returns independent PDF bytes

The operation preserves original object identities, clones shared font descriptors and descendants for the selected Type0 resource, and publishes no partial result on failure

Declarations

type
TPdfTextFontRepair= record
    FontObjectNumber, Generation: Integer;
    FontProgram: TBytes;
    CIDToGIDMap: TBytes;
    ReplaceExisting: Boolean;
  end;
  TPdfTextFontRepairs= array of TPdfTextFontRepair;
  TPdfToUnicodeEntry= record
    CharacterCode: Cardinal;
    UnicodeText: string;
  end;
  TPdfToUnicodeEntries= array of TPdfToUnicodeEntry;
  TPdfToUnicodeRepair= record
    FontObjectNumber, Generation, CodeBytes: Integer;
    Entries: TPdfToUnicodeEntries;
    ReplaceExisting: Boolean;
  end;
  TPdfToUnicodeRepairs= array of TPdfToUnicodeRepair;
  TPdfActualTextRepair= record
    ContentObjectNumber, Generation, Mcid: Integer;
    Value: string;
    ReplaceExisting: Boolean;
  end;
  TPdfActualTextRepairs= array of TPdfActualTextRepair;
  TPdfDocumentTextRepairPlan= record
    SourceSha256: string;
    Fonts: TPdfTextFontRepairs;
    ToUnicode: TPdfToUnicodeRepairs;
    ActualText: TPdfActualTextRepairs;
  end;
  TPdfDocumentTextRepairOptions= record
    ReadOptions: TPdfActiveObjectReadOptions;
    MaxRepairs, MaxFontBytes, MaxDecodedStreamBytes, MaxMappings, MaxTextCharacters: Integer;
    MaxDecodedBytes, MaxOutputBytes, MaxWork, MaxVerificationPixels: Int64;
    VerifyUnchangedPixels: Boolean;
    CancellationToken: IPdfCancellationToken;
    class function Default: TPdfDocumentTextRepairOptions; static;
  end;
  TPdfDocumentTextRepairReport= record
    Succeeded, Cancelled: Boolean;
    EmbeddedFonts, ToUnicodeMaps, ActualTextValues: Integer;
    SourceSha256, OutputSha256, ErrorMessage: string;
  end;

function RepairPdfDocumentText(const Source: TBytes;
  const Plan: TPdfDocumentTextRepairPlan; const Options: TPdfDocumentTextRepairOptions;
  out OutputPdf: TBytes; out Report: TPdfDocumentTextRepairReport): Boolean;

Source binding and resource identities

Plan.SourceSha256 is the hexadecimal SHA-256 digest of the exact input Source bytes; every recipe names an active object number and generation from that snapshot

Compressed object streams and incremental revisions are resolved before editing; acquire new identities when a native save or object-stream packer has renumbered a document

Fonts and ToUnicode targets must be unique within their respective arrays; several distinct MCIDs in one content stream are supported, but duplicate content-object/MCID targets are rejected

Signed PDFs, signature fields, encryption and XFA require a separately prepared source and are rejected; an empty recipe returns the exact source after the same source policy checks

Font embedding

TPdfTextFontRepair selects either a Type0 Identity-H or Identity-V font with exactly one indirect descendant and an indirect FontDescriptor (where a TrueType program requires a CIDFontType2 descendant, and a bare CFF or OpenType CFF wrapper program requires a CIDFontType0 descendant) or a single-byte simple /Subtype /TrueType or /Subtype /Type1 font with bounded /FirstChar, /LastChar, /Widths and an indirect FontDescriptor

FontProgram supplies a complete standalone static font: TrueType glyf/loca programs validate table ranges, outline/composite structure, metrics, glyph count, embedding policy and an ASCII PostScript name through the portable TrueType backend before native font loading, bare CFF programs recover their PostScript name and glyph count from the CFF Name and CharStrings indexes, and OpenType CFF wrappers reuse the sfnt directory parser

CFF programs validate through native font loading and embed as /FontFile3 with /Subtype /CIDFontType0C (bare CFF) or /Subtype /OpenType (wrapper); collections, variable, bitmap and color fonts require another profile

CIDToGIDMap optionally supplies a complete big-endian 16-bit CID-to-glyph map for a CIDFontType2 TrueType replacement; when empty, an existing local map stream is decoded and validated, and a source /CIDToGIDMap /Identity name value infers the explicit identity stream over the replacement font's glyph count, which also satisfies the explicit-map requirement that rejects name values; CFF descendants and simple /Subtype /TrueType fonts never carry a CIDToGIDMap and any supplied map is rejected while stale CFF entries are removed

Every map glyph must be within the supplied font's glyph count; the map contains at most 65,536 entries and PDF widths remain unchanged

The selected Type0 or simple TrueType resource points to new descriptor (and for Type0, descendant) objects with the full font's PostScript name; other resources retain their original descendants, descriptors and embedded programs. Native validation loads each replacement program through FPDFText_LoadFont as TrueType or Type1 depending on the detected program kind

ReplaceExisting defaults False and is required to replace an existing FontFile, FontFile2 or FontFile3 declaration

ToUnicode maps

TPdfToUnicodeRepair writes explicit CharacterCode to UnicodeText mappings as a bounded CMap with bfchar groups of at most 100 entries

Entries must be strictly increasing and unique; nonempty UnicodeText values contain at most 256 UTF-16 code units with valid surrogate pairs

CodeBytes is 2 for Type0 Identity-H/V fonts and 1 for simple Type1 or TrueType fonts; other encodings require another repair profile

Replacing a declared ToUnicode stream requires ReplaceExisting True; missing or incomplete mappings are never inferred from appearance or glyph names

ActualText

TPdfActualTextRepair targets exactly one BDC property dictionary (either inline or referenced by name in the owning page or Form XObject /Resources /Properties dictionary) with the requested nonnegative Mcid in ContentObjectNumber and Generation

Value is written as a UTF-16BE string with a byte-order marker, preserving the MCID, surrounding marked content, painting operators, other dictionary keys and existing /FlateDecode stream compression (expanding matched named property operands into inline dictionaries so other references to the shared property name remain unaffected)

Existing ActualText requires ReplaceExisting True; an explicitly empty Value suppresses replacement text for that marked span

The edited stream must have balanced BMC/BDC/EMC nesting, at most 256 operands per operator and depth 256; ambiguous MCIDs, unresolved named property lists and inline images are rejected

Only the selected local stream changes, including when it is a Form content stream; all instances sharing that stream observe the same semantic change

Verification and limits

Source bytes remain unchanged; output is independently reopened and checked for retained page count and source policy before publication

VerifyUnchangedPixels defaults True and compares every page at one pixel per PDF point with bounded native 32-bit scan lines; a changed glyph map or font appearance fails with no output

When an absent font's substitution differs from the reviewed replacement, explicitly disable VerifyUnchangedPixels and compare the result against an application-approved rendering; the API does not guess the intended font

Defaults are 128 MiB source, 4,096 recipes, 64 MiB per font and decoded stream, 1,048,576 mapping entries and text code units, 128 MiB aggregate decoded/generated data, 512 MiB staged/output bytes, 268,435,456 work units and 33,554,432 verification pixels

Hard maxima are 100,000 recipes, 64 MiB per font, 256 MiB per decoded stream, 4,194,304 mappings, 16,777,216 text code units, 512 MiB aggregate decoded data, MaxInt output bytes, 1,073,741,824 work units and 268,435,456 verification pixels

ReadOptions bounds active-object parsing; source size must also fit MaxOutputBytes, and repeated decodes, generated CMaps, rewritten content and supplied font/map bytes consume cumulative budgets

CancellationToken is checked between bounded stages, mappings and pixel scan lines; individual native font/load/render calls and bounded TrueType backend validation remain synchronous

Report returns Succeeded, Cancelled, applied font/map/ActualText counts, source/output digests and ErrorMessage; failures clear publication counts and output digest

Transactional standards repair

TPdfDocumentTextRepairProvider deep-copies the reviewed recipe and runs it through the exact-byte/native PDF/A postvalidation and rollback workflow

Native reopening and pixel equality verify persistence and appearance; they do not establish semantic correctness or external PDF/A or PDF/UA certification

See also

Document repair, Structure editing, Font subsetting