TPDFContentProgram
Content stream editing
Description
TPDFContentProgram (unit Lib\PDFlibContentModel.pas) is the editable instruction list over one parsed PDF content stream: each instruction pairs a PDF content operator (ISO 32000-1 Table 51) with its decoded operand objects, giving code a structured view of a page layer for inspection and editing
Obtain a program with ParsePageContentLayer, edit the instruction list, and serialise it back with WritePageContentLayer; the emitter is behaviour-preserving, so writing back an unmodified program renders the page exactly as before
TraceGraphicsStates, TraceGraphicsStatePairs and GetGraphicsState evaluate the program through a TPDFContentStateTracker to reconstruct effective graphics states
Syntax
Delphi
TPDFContentProgram = Class
Public
Constructor Create;
Destructor Destroy; Override;
Procedure Clear;
Function Parse(Const Source: AnsiString): Boolean;
Function Emit: AnsiString;
Function EmitRange(FirstInstruction, LastInstruction: Integer): AnsiString;
Function Count: Integer;
Procedure Delete(Index: Integer);
Function DeleteRange(FirstInstruction, LastInstruction: Integer): Boolean;
Function WrapRangeWithMatrix(FirstInstruction, LastInstruction: Integer;
M11, M12, M21, M22, MDX, MDY: Double): Boolean;
Function InsertFromSource(Index: Integer; Const Source: AnsiString): Integer;
Function SetOperand(Index, OperandIndex: Integer; Const Source: AnsiString): Boolean;
Function ReplaceInlineImage(Index: Integer; Const ResourceName: AnsiString): Boolean;
Function OperandText(Index, OperandIndex: Integer): AnsiString;
Function InstructionGroup(Index: Integer): TPDFOpGroup;
Function FindNextOp(StartIndex: Integer; Op: TPDFContentOperator): Integer;
Function FindNextGroup(StartIndex: Integer; Group: TPDFOpGroup): Integer;
Function LiftPath(StartIndex: Integer): TPDFPath;
Function GetTextRuns: TPDFTextRunArray;
Function FindInstructionsInRect(Left, Top, Right, Bottom: Double;
Out Ranges: Array Of TPDFInstructionRange; IncludeText: Boolean = True): Integer;
Function EnumImagePlacements: TPDFImagePlacementArray;
Function TraceGraphicsStates(Tracker: TPDFContentStateTracker;
AfterInstruction: Boolean): TPDFContentGraphicsStateArray;
Function TraceGraphicsStatePairs(Tracker: TPDFContentStateTracker;
Out BeforeStates, AfterStates: TPDFContentGraphicsStateArray): Integer;
Function GetGraphicsState(InstructionIndex: Integer; AfterInstruction: Boolean;
Out State: TPDFContentGraphicsState): Boolean;
Function ReplaceTextRun(InstructionIndex: Integer; Const NewText: AnsiString): Boolean;
Function ReplaceTextStringRange(InstructionIndex, OperandIndex, ArrayElementIndex,
ByteOffset, ByteLength: Integer; Const NewText: AnsiString): Boolean;
Function SetMarkedContentTextProperty(InstructionIndex: Integer;
Const PropertyName: AnsiString; Const PropertyValue: WideString): Boolean;
Function SetMarkedContentPropertySource(InstructionIndex: Integer;
Const PropertyName, PropertyValueSource: AnsiString): Boolean;
Function DeleteMarkedContentProperty(InstructionIndex: Integer;
Const PropertyName: AnsiString): Boolean;
Function SetMarkedContentPropertyResource(InstructionIndex: Integer;
Const ResourceName: AnsiString): Boolean;
Function ClearMarkedContentProperties(InstructionIndex: Integer): Boolean;
Function LowerPath(Path: TPDFPath): Boolean;
Property Item[Index: Integer]: TPDFContentInstruction; Default;
Property CanonicalNumbers: Boolean;
End;Remarks
Parse returns False only on gross structural damage (an unterminated string or dictionary); unknown operators are tolerated and preserved
CanonicalNumbers controls whether parsed numbers keep their source text (default, so re-emitting changes only edited operands) or are rewritten at the program's fixed four-decimal precision; a program returned by ParsePageContentLayer is already parsed, so switching it to canonical numbers takes Parse(Emit) after setting the flag
Exact inline-image boundary callback
Set OnInlineImageBoundary before Parse to replace the default heuristic BI/EI capture with a caller-supplied byte-boundary proof
TPDFInlineBoundaryEvent = function(const Source: AnsiString;
BIStart: Integer; out AfterEI: Integer): Boolean of object;
BIStart is the 1-based position of BI and AfterEI is the 1-based first byte after the complete EI token; return False when the boundary cannot be proved, which makes parsing fail without falling back to a guessed delimiter
The default nil callback preserves the existing heuristic behaviour, which is suitable for already-qualified streams but does not prove arbitrary binary inline-image boundaries; optional-content flattening installs its bounded exact proof
See also
ParsePageContentLayer, WritePageContentLayer, TPDFContentStateTracker, TraceGraphicsStates, ApplyContentFilterChain