TPDFContentProgram
内容流编辑
描述
TPDFContentProgram(单元 Lib\PDFlibContentModel.pas)是构建在一个已解析 PDF 内容流之上的可编辑指令列表:每条指令由一个 PDF 内容算子(ISO 32000-1 Table 51)和它的已解码操作数对象组成,让代码能以结构化视图检查和编辑页面层
用 ParsePageContentLayer 取得程序,编辑指令列表,再用 WritePageContentLayer 序列化回去;输出过程保持行为不变,因此写回未经修改的程序,页面渲染与之前完全一致
TraceGraphicsStates、TraceGraphicsStatePairs 和 GetGraphicsState 通过 TPDFContentStateTracker 求值程序,重建有效的图形状态
语法
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;备注
Parse 只有在严重结构损坏(字符串或字典未闭合)时才返回 False;未知算子会被容忍并保留
CanonicalNumbers 控制解析出的数字是保留源文本(默认,重新输出时只改动被编辑的操作数),还是按程序固定的四位小数精度重写;ParsePageContentLayer 返回的程序已经解析完毕,要切换到规范数字,需在设置标志后再执行一遍 Parse(Emit)
精确内联图像边界回调
在 Parse 之前设置 OnInlineImageBoundary,用调用方提供的字节边界证明替换默认的启发式 BI/EI 捕获
TPDFInlineBoundaryEvent = function(const Source: AnsiString;
BIStart: Integer; out AfterEI: Integer): Boolean of object;
BIStart 是 BI 的 1 起始位置,AfterEI 是完整 EI 标记之后第一个字节的 1 起始位置;无法证明边界时返回 False,解析会直接失败,而不会回退到猜测的分隔符
默认的 nil 回调保留既有启发式行为,适用于已通过判定的流,但无法证明任意二进制内联图像边界;可选内容扁平化会安装自己的有界精确证明
另请参阅
ParsePageContentLayer、WritePageContentLayer、TPDFContentStateTracker、TraceGraphicsStates、ApplyContentFilterChain