TPDFlibFormSession

表單、互動、資料類型

描述

維護 AcroForm 命中測試、鍵盤導覽、焦點、編輯值與文字選取狀態,不依賴 GUI 框架

附加的 TPDFlib 執行個體仍由呼叫者擁有,且必須比 session 存活更久

焦點轉換透過 OnAction 把 Widget 的 /Fo 與 /Bl 動作以具型別主機回呼的形式公開

頁面矩形使用頁面旋轉後的視覺 CropBox,單位為程式庫目前的量測單位,原點在視覺左上角

方向性選取保留獨立的錨點與游標位置,可用 UTF-16 code unit、Unicode 碼點或擴展字位叢集設定或查詢

雙向編輯保留邏輯欄位值順序,同時由嵌入層級解析逐行視覺游標槽,實體的左右移動因此遵循混合的由左至右與由右至左文字

選取取代會向主機分派 /K、透過 OnTextEdit 公開所選與取代文字、在完整字位邊界強制 /MaxLen,並在編輯被接受後收合選取範圍

每個作用中編輯保留有界差異歷程,可還原暫時值、顯示值與方向性選取,且不重新分派表單動作或主機編輯回呼

DispatchInputEvent 把平台中立的指標、滾輪、具名鍵與組字文字記錄,轉換為受限的 Widget 動作、焦點轉換、字位安全編輯、按鈕啟用與選擇導覽

session 保留進入與按住的 Widget 身分,因此 /E、/X、/D 與 /U 即使跨指標擷取與取消,仍遵循 PDF 觸發順序

提交會分派 /V 與 /F 動作、在改變文件前執行其支援的有界 JavaScript 子集;驗證、格式化或主機回呼否決交易時,作用中的編輯會被保留

驗證過的原始值儲存進欄位,DisplayValue 與產生的 Widget 外觀則保留格式化結果

宣告

type
  TPDFlibFormFieldIndices = array of Integer;

  TPDFlibFormSelectionUnit = (fsuUTF16CodeUnit,
    fsuUnicodeCodePoint, fsuGraphemeCluster);

  TPDFlibFormTextDirection = (ftdAuto,
    ftdLeftToRight, ftdRightToLeft);
  TPDFlibFormCaretIndices = array of Integer;

  TPDFlibFormSelection = record
    IndexUnit: TPDFlibFormSelectionUnit;
    AnchorIndex: Integer;
    CaretIndex: Integer;
    StartIndex: Integer;
    SelectionLength: Integer;
    IsReversed: Boolean;
  end;

  TPDFlibFormSessionActionEvent = procedure(Sender: TObject;
    FieldIndex: Integer; const TriggerEvent: AnsiString;
    const ActionInfo: TPDFlibActionInfo; var Allow: Boolean) of object;
  TPDFlibFormSessionFocusEvent = procedure(Sender: TObject;
    OldFieldIndex, NewFieldIndex: Integer) of object;
  TPDFlibFormSessionValueEvent = procedure(Sender: TObject;
    FieldIndex: Integer; const OldValue, NewValue: WideString) of object;
  TPDFlibFormEditKind = (fekReplaceSelection,
    fekCutSelection, fekPasteText);
  TPDFlibFormSessionTextEditEvent = procedure(Sender: TObject;
    FieldIndex: Integer; EditKind: TPDFlibFormEditKind;
    SelectionStart, SelectionLength: Integer;
    const SelectedText: WideString; var ReplacementText: WideString;
    var Allow: Boolean) of object;

  TPDFlibFormInputEventKind = (fiekPointerMove,
    fiekPointerDown, fiekPointerUp, fiekPointerDoubleClick,
    fiekPointerLeave, fiekPointerCancel, fiekWheel,
    fiekKeyDown, fiekKeyUp, fiekTextInput);
  TPDFlibFormInputModifiers = set of TPDFlibFormInputModifier;

  TPDFlibFormInputEvent = record
    Kind: TPDFlibFormInputEventKind;
    Modifiers: TPDFlibFormInputModifiers;
    Button: TPDFlibFormInputButton;
    Key: TPDFlibFormInputKey;
    Page: Integer;
    X, Y: Double;
    WheelDeltaX, WheelDeltaY: Double;
    Text: WideString;
  end;

  TPDFlibFormSession = class
  public
    constructor Create(ALibrary: TPDFlib);
    procedure AttachLibrary(ALibrary: TPDFlib);
    procedure Reset;
    function FocusField(FieldIndex: Integer;
      CommitCurrent: Boolean = True): Integer;
    function GetFieldPageRect(FieldIndex: Integer; out Page: Integer;
      out Left, Top, Width, Height: Double): Integer;
    function HitTest(Page: Integer; X, Y: Double): Integer;
    function GetTabSequence(out FieldIndices: TPDFlibFormFieldIndices): Integer;
    function FocusAt(Page: Integer; X, Y: Double;
      CommitCurrent: Boolean = True): Integer;
    function FocusNext(Backwards: Boolean = False;
      WrapAround: Boolean = True; CommitCurrent: Boolean = True): Integer;
    function Blur(CommitCurrent: Boolean = True): Integer;
    function BeginEdit(FieldIndex: Integer): Integer;
    function SetEditValue(const NewValue: WideString): Integer;
    function SetSelection(StartIndex, SelectionLength: Integer): Integer;
    function SetSelectionRange(AnchorIndex, CaretIndex: Integer;
      IndexUnit: TPDFlibFormSelectionUnit): Integer;
    function GetSelectionRange(IndexUnit: TPDFlibFormSelectionUnit;
      out Selection: TPDFlibFormSelection): Integer;
    function SetTextDirection(
      Direction: TPDFlibFormTextDirection): Integer;
    function GetVisualCaretOrder(IndexUnit: TPDFlibFormSelectionUnit;
      out CaretIndices: TPDFlibFormCaretIndices): Integer;
    function MoveVisualCaret(ToRight: Boolean;
      ExtendSelection: Boolean = False): Integer;
    function GetSelectedText: WideString;
    function ReplaceSelection(const ReplacementText: WideString): Integer;
    function CutSelection(out CutText: WideString): Integer;
    function PasteText(const Text: WideString): Integer;
    function CanUndoEdit: Boolean;
    function CanRedoEdit: Boolean;
    function UndoEdit: Integer;
    function RedoEdit: Integer;
    procedure ClearEditHistory;
    function DispatchInputEvent(
      const InputEvent: TPDFlibFormInputEvent;
      out InputResult: TPDFlibFormInputResult): Integer;
    function CommitEdit: Integer;
    procedure CancelEdit;
    property TextDirection: TPDFlibFormTextDirection;
    property ResolvedTextDirection: TPDFlibFormTextDirection;
    property VisualCaretPosition: Integer;
    property MaxUndoSteps: Integer;
    property UndoEditCount: Integer;
    property RedoEditCount: Integer;
    property HoveredFieldIndex: Integer;
    property PressedFieldIndex: Integer;
  end;

方法

Create建立附加到呼叫端擁有之程式庫執行個體的工作階段
AttachLibrary重設所有暫時狀態,並附加另一個呼叫端擁有的程式庫執行個體或 nil
Reset清除焦點、編輯與選取狀態,而不變更欄位值或分派動作
FocusField提交或取消目前的編輯、為先前的 Widget 分派 /Bl,然後為要求的可見 Widget 分派 /Fo
GetFieldPageRect在旋轉後的視覺 CropBox 座標中傳回可見 Widget 矩形,而不變更所選頁面
HitTest傳回視覺頁面點上最上層的可見 Widget,以頁面 /Annots 陣列中較後面的項目作為最前方的命中
GetTabSequence依頁面順序傳回跨頁面的可見 Widget,並套用 /Tabs /R、/C、/S、PDF 2.0 的 /A 與 /W 語意、頁面旋轉以及 ViewerPreferences 的 /Direction;缺少或無效的 /Tabs 值會使用附註陣列順序
FocusAt對視覺頁面點進行命中測試,並透過一般的提交、失焦與聚焦動作路徑聚焦傳回的 Widget
FocusNext在計算出的定位順序中向前或向後移動,可選擇循環,並遵循一般的焦點轉換規則
Blur提交或取消目前的編輯,並在清除焦點之前分派 /Bl
BeginEdit聚焦可編輯的文字或選擇欄位,並擷取其原始值的快照
SetEditValue更新暫時值,並強制執行文字欄位設定的最大長度,且不切斷延伸字素叢集
SetSelection限制並儲存暫時值內以零為基底的 UTF-16 選取範圍
SetSelectionRange以要求的索引單位限制方向性錨點與插入點位置、將其轉換為內部 UTF-16 範圍,且在使用任一 Unicode 單位時絕不切斷碼位或字素叢集
GetSelectionRange以要求的索引單位傳回錨點、插入點、正規化起點與長度,以及原始方向
SetTextDirection選取自動、明確由左至右或明確由右至左的段落方向,並使快取的視覺插入點順序失效
GetVisualCaretOrder以實際由左至右的順序傳回字素安全的邏輯插入點索引,包括混合方向邊界上不同的分割位置
MoveVisualCaret以一個實際視覺位置移動或延伸方向性選取範圍,並將現有的選取範圍朝要求的實際邊緣收合
GetSelectedText傳回目前選取的 UTF-16 文字或空字串
ReplaceSelection透過單一可否決的 /K 與 OnTextEdit 交易取代正規化選取範圍,然後在接受取代後收合插入點
CutSelection透過相同的原子取代路徑傳回並移除選取的文字
PasteText以呼叫端提供的剪貼簿文字取代目前的選取範圍,不引入平台剪貼簿相依性
CanUndoEdit回報作用中的編輯是否有可復原的歷程項目
CanRedoEdit回報作用中的編輯是否有可重新套用的反向歷程項目
UndoEdit反轉最近保留的編輯,並還原其先前的方向性選取範圍
RedoEdit重新套用下一個保留的編輯,並還原其產生的方向性選取範圍
ClearEditHistory捨棄作用中編輯的復原與重做歷程,而不變更其目前值或選取範圍
DispatchInputEvent分派一個平台中立的指標、滾輪、按鍵或組合文字事件,並傳回產生的焦點、懸停、按壓、值與選取狀態
CommitEdit在儲存原始值之前驗證並格式化候選值、重新整理格式化後的外觀,然後引發 OnValueChanged
CancelEdit捨棄暫時值與選取範圍,而不變更欄位

狀態屬性

LibraryInstance目前非擁有的 TPDFlib 執行個體
FocusedFieldIndex取得焦點的欄位或暫時 Widget 索引,沒有 Widget 取得焦點時為零
EditingFieldIndex正在編輯的基礎文字或選擇欄位,沒有作用中的編輯時為零
OriginalValueBeginEdit 所擷取的欄位值
EditValue目前的暫時值
DisplayValue目前的編輯值,或最近一次成功提交所產生的格式化顯示值
SelectionStart以零為基底的 UTF-16 起始索引
SelectionLengthUTF-16 選取長度
SelectionAnchor以零為基底的 UTF-16 錨點位置,在插入點延伸方向性選取範圍時保持固定
SelectionCaret方向性選取範圍以零為基底的 UTF-16 移動端
TextDirection設定的方向,預設為 ftdAuto 並在工作階段的多次編輯間保留
ResolvedTextDirection明確方向,或從作用中編輯值的第一個強方向性字元選出的自動方向
VisualCaretPosition快取實際插入點順序中的目前位置,尚未有任何視覺移動選取位置時為 -1
MaxUndoSteps保留編輯歷程項目的最大數量,範圍 0 到 10000;預設為 1000,零表示停用記錄
UndoEditCount目前可供 UndoEdit 使用的保留項目數量
RedoEditCount目前可供 RedoEdit 使用的保留項目數量
ModifiedTrue 表示 EditValue 與 OriginalValue 不同
HoveredFieldIndex目前處於指標進入狀態的 Widget,或為零
PressedFieldIndex保留主要指標擷取直到對應放開或指標取消的 Widget,或為零

事件

OnAction為可用的焦點、失焦、按鍵、驗證與格式化動作接收具型別的 TPDFlibActionInfo;將 Allow 設為 False 可否決擱置中的作業
OnFocusChanged回報每次已完成的焦點轉換,包括新的焦點動作遭否決後轉換為零的情況
OnValueChanged在欄位與其待更新外觀更新之後,回報一次成功提交的值變更
OnTextEdit在暫時值變更之前接收編輯類型、UTF-16 選取範圍、選取的文字與可變更的取代內容;將 Allow 設為 False 可進行原子式拒絕

傳回值

變更狀態的函式成功時傳回 1;請求無效、重新進入、遭否決或無法提交時傳回 0;HitTest 傳回欄位或暫時 Widget 索引,GetTabSequence 則傳回項目數量

選取單位

fsuUTF16CodeUnit 保留舊式 0-based WideString 索引契約,明確要求時也包括代理對內部的位置

fsuUnicodeCodePoint 把有效代理對算作一個輔助碼點,未配對的代理視為獨立的單一碼點

fsuGraphemeCluster 只計算完整擴展字位叢集,組合序列與接合的 emoji 因此保持不可分割

Unicode 邊界表為目前編輯值快取,該值變更時即失效

雙向導覽

ftdAuto 使用第一個強字元;編輯內容沒有強方向性字元時預設為由左至右

視覺 run 順序逐行獨立解析,不改變邏輯的 UTF-16 欄位值、PDF 字串或文字對齊

邏輯索引在混合方向邊界可能佔兩個不同的視覺槽,VisualCaretPosition 因此能分辨反覆穿越該邊界的移動

範例

var
  Session: TPDFlibFormSession;
  Selection: TPDFlibFormSelection;
begin
  Session := TPDFlibFormSession.Create(PDF);
  try
    if Session.FocusAt(PageNumber, MouseX, MouseY) = 1 then
      Session.FocusNext(False, True);
    if Session.BeginEdit(Session.FocusedFieldIndex) = 1 then
    begin
      Session.SetEditValue('A' + WideChar($D83D) + WideChar($DE00) + 'B');
      Session.SetSelectionRange(1, 2, fsuUnicodeCodePoint);
      Session.GetSelectionRange(fsuUTF16CodeUnit, Selection);
      Session.SetTextDirection(ftdAuto);
      Session.MoveVisualCaret(True, True);
      Session.PasteText('replacement');
      if Session.CanUndoEdit then
        Session.UndoEdit;
      if Session.CanRedoEdit then
        Session.RedoEdit;
      Session.CommitEdit;
    end;
  finally
    Session.Free;
  end;
end;

另請參閱

SetTextDirection, GetVisualCaretOrder, MoveVisualCaret, TPDFlibFormTextDirection, DispatchInputEvent, TPDFlibFormInputEvent, TPDFlibFormInputResult, UndoEdit, RedoEdit, ClearEditHistory, ReplaceSelection, CutSelection, PasteText, TPDFlibFormEditKind, SetSelectionRange, GetSelectionRange, ExecuteFormFieldScript, TPDFlibActionInfo