TPDFlibFormSession

表单、交互、数据类型

描述

在不依赖 GUI 框架的情况下维护 AcroForm 命中测试、键盘导航、焦点、编辑值和文本选择状态

挂接的 TPDFlib 实例保持为调用方所有,且必须比会话存活更久

焦点迁移以带类型的宿主回调形式经 OnAction 暴露部件的 /Fo 与 /Bl 动作

页面矩形使用页面旋转后的可视 CropBox,单位为库当前的度量单位,原点在可视左上角

方向性选择保留独立的锚点与插入符位置,可以以 UTF-16 代码单元、Unicode 码点或扩展字素簇设置或查询

双向编辑保持逻辑字段值顺序,同时从嵌入级别解析逐行可视插入符槽位,因此物理左右移动遵循从左到右与从右到左混排的文本

选择替换向宿主分派 /K,经 OnTextEdit 暴露所选文本与替换文本,在完整字素边界强制 /MaxLen,并在一次接受的编辑后折叠选择

每个活动编辑保有有界的增量历史,可在不重新分派表单动作或宿主编辑回调的情况下恢复临时值、显示值和方向性选择

DispatchInputEvent 把平台无关的指针、滚轮、命名键和组合文本记录转换为受约束的部件动作、焦点迁移、字素安全编辑、按钮激活和 choice 导航

会话保留进入和按下部件的身份,因此 /E、/X、/D 和 /U 即使跨越指针捕获与取消也遵循 PDF 触发顺序规则

提交会分派 /V 与 /F 动作,在更改文档之前执行其受支持的有界 JavaScript 子集;当验证、格式化或宿主回调拒绝该事务时保留活动编辑

验证后的原始值存入字段,而 DisplayValue 和生成的部件外观保留格式化结果

声明

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在计算出的 Tab 序列中前后移动,支持可选的循环回绕和正常的焦点转换规则
Blur提交或取消当前编辑,并在清除焦点之前派发 /Bl
BeginEdit聚焦可编辑的文本或选择字段并快照其原始值
SetEditValue更新临时值并强制文本字段配置的最大长度,而不拆分扩展字素簇
SetSelection限制并存储临时值内从 0 开始的 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从 0 开始的 UTF-16 起始索引
SelectionLengthUTF-16 选定长度
SelectionAnchor从 0 开始的 UTF-16 锚点位置,插入符扩展有向选定时保持固定
SelectionCaret从 0 开始的 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 返回字段或临时部件索引,GetTabSequence 返回条目数量

选择单位

fsuUTF16CodeUnit 保留传统的零基 WideString 索引契约,包括在显式请求时代理对内部的位置

fsuUnicodeCodePoint 把有效的代理对计为一个增补码点,把未配对代理视为一个独立码点

fsuGraphemeCluster 按完整的扩展字素簇计数,因此组合序列和连写的 emoji 保持不可分割

Unicode 边界表针对当前编辑值缓存,该值变化时失效

双向导航

ftdAuto 使用第一个强字符;编辑中没有强方向字符时默认从左到右

可视片段顺序逐行独立解析,不改变逻辑 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