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 token 之後第一個位元組的 1 起始位置;無法證明邊界時傳回 False,解析會直接失敗,不會退回猜測的分隔方式
預設的 nil 回呼保留既有啟發式行為,適合已符合條件的串流,但無法證明任意的二進位內嵌影像邊界;選用內容扁平化會安裝自己的有界精確證明
另請參閱
ParsePageContentLayer, WritePageContentLayer, TPDFContentStateTracker, TraceGraphicsStates, ApplyContentFilterChain