TPDFlibViewer
查看器
描述
TPDFlibViewer(单元 Lib\PDFlibViewer.pas)是一个 VCL 滚动控件,可在你的应用程序中显示 PDF 页面。它通过与 TPDFlib 相同的引擎渲染(GDI+、Cairo 或 PDFium,取决于所附加实例上的 SelectRenderer)
主要成员
Function LoadFromFile(Const FileName, Password: WideString): Integer;
Procedure AttachLibrary(ALib: TPDFlib);
Property Lib: TPDFlib;
Procedure CloseDocument;
Function PageCount: Integer;
Function GoToPage(NewPage: Integer): Integer;
Function GoToPagePoint(NewPage: Integer; XPt, YPt: Double): Integer;
Procedure ScrollBy(DX, DY: Integer);
Function GetScrollPosForPage(APage: Integer): Integer;
Function ClientPointToPage(X, Y: Integer; Out APage: Integer; Out XPt, YPt: Double): Boolean;
Function PageRectToClientRect(APage: Integer; XPt, YPt, WPt, HPt: Double; Out R: TRect): Boolean;
Property HScrollPos: Integer;
Property VScrollPos: Integer;
Function NextPage: Integer;
Function PrevPage: Integer;
Procedure SetZoomPercent(Percent: Double);
Property ZoomPercent: Double;
Procedure SetZoomMode(Mode: TPDFlibViewerZoomMode);
Property ZoomMode: TPDFlibViewerZoomMode;
Procedure SetDisplayMode(Mode: TPDFlibViewerDisplayMode);
Procedure SetCacheLimit(MegaBytes: Integer);
Function IsPageCached(APage: Integer): Boolean;
Procedure DrainPrefetched;
Property Cache: TPDFlibViewerBitmapCache;
Procedure RefreshPage;
Function RenderedWidth: Integer;
Function RenderedHeight: Integer;
Function EffectiveZoomPercent: Double;
Function LinkActionAtPagePoint(APage: Integer; XPos, YPos: Double; Out Info: TPDFlibActionInfo): Integer;
Function AnnotAtPagePoint(APage: Integer; XPos, YPos: Double; Out Subtype: WideString): Integer;
Function SaveAnnotAttachmentToFile(APage, AnnotIndex: Integer; Const FileName: WideString): Integer;
Property AnnotHints: Boolean;
Property OnAnnotClick: TPDFlibViewerAnnotClick;
Function FormFieldAtPagePoint(APage: Integer; XPos, YPos: Double): Integer;
Function GetFormFieldPageRect(FieldIndex: Integer; Out APage: Integer; Out XPt, YPt, WPt, HPt: Double): Boolean;
Function ToggleCheckBoxField(FieldIndex: Integer): Integer;
Function SetFormFieldValueAndRefresh(FieldIndex: Integer; Const NewValue: WideString): Integer;
Function BeginEditFormField(FieldIndex: Integer): Integer;
Procedure CommitInplaceEditor(Save: Boolean);
Function FocusNextFormField(Backwards: Boolean= False): Integer;
Function SaveToFile(Const FileName: WideString): Integer;
Function SaveToStream(OutStream: TStream): Integer;
Property HighlightFormFields: Boolean;
Property FormHighlightColor: TColor;
Property InteractiveForms: Boolean;
Property InteractiveAnnotations: Boolean;
Property AutoExecuteFormScripts: Boolean;
Property FormTextColor: TColor;
Property OnFormFieldValueChanged: TPDFlibViewerFormFieldChange;
Function GetFormFieldTabSequence(Out FieldIndices: TPDFlibViewerFormFieldIndices): Integer;
Function GetFormFieldEditorKind(FieldIndex: Integer): TPDFlibViewerFormEditorKind;
Function ActivateFormField(FieldIndex: Integer; Const TriggerEvent: WideString= 'U'): Integer;
Property EditingFieldIndex: Integer;
Property FocusedFormFieldIndex: Integer;
Property OnFormFieldAction: TPDFlibViewerFormFieldAction;
Function GetTextInRegion(APage: Integer; XPos, YPos, RWidth, RHeight: Double): WideString;
Function CopySelectedText: Integer;
Property ViewerMode: TPDFlibViewerMode;
Function CaptureRegion(Const Region: TRect): TBitmap;
Function CaptureCurrentView: TBitmap;
Function SaveCurrentViewToFile(Const FileName: WideString): Integer;
Function SelectRegion(APage: Integer; XPos, YPos, RWidth, RHeight: Double): Integer;
Function GetSelectedRegion(Index: Integer; Out Selection: TPDFlibViewerSelection): Boolean;
Procedure ClearSelectedRegions;
Function SelectedRegionCount: Integer;
Property SelectionColor: TColor;
Property OnMarqueeSelect: TPDFlibViewerMarqueeSelect;
Procedure AddHighlightRegion(APage: Integer; XPos, YPos, RWidth, RHeight: Double);
Procedure ClearHighlightRegions;
Function HighlightRegionCount: Integer;
Function CommitHighlightRegionsToAnnotations: Integer;
Property HighlightColor: TColor;
Property DrawColor: TColor;
Property DrawLineWidth: Double;
Function CommitDrawnShape(Mode: TPDFlibViewerMode; Const ClientPoints: Array Of TPoint): Integer;
Function HighlightSearchHits(Const Query: WideString; CaseSensitive: Boolean= False): Integer;
Procedure ClearSearchHighlights;
Function SearchHighlightCount: Integer;
Property SearchHighlightColor: TColor;
Property SnapshotToClipboard: Boolean;
Property DiskCacheFolder: WideString;
Property DiskCacheHits: Integer;
Property DiskCacheMaxDocuments: Integer;
Property PrefetchEnabled: Boolean;
Property OnBeforeSnapshot: TPDFlibViewerBeforeSnapshot;
Property OnSnapshot: TPDFlibViewerSnapshot;
Function PrintDoc(ShowPrintDialog: Boolean= True): Integer;
Function PrintDocRange(Const PrinterName: WideString; StartPage, EndPage, Copies: Integer; Collate: Boolean): Integer;
Procedure ZoomInStep;
Procedure ZoomOutStep;
Procedure RotateClockwise90;
Procedure RotateAntiClockwise90;
Property ViewRotation: Integer;
Property SelectedText: WideString;
Property Page: Integer;
Property ScreenDPI: Integer;
Property DisplayMode: TPDFlibViewerDisplayMode;
Property ScrollHintEnabled: Boolean;
Property OnZoomChange: TPDFlibViewerZoomChange;
Property OnHScroll: TPDFlibViewerScroll;
Property OnVScroll: TPDFlibViewerScroll;
Property OnPageChange: TPDFlibViewerPageChange;
Property OnLinkClick: TPDFlibViewerLinkClick;备注
LoadFromFile 通过控件自有的 TPDFlib 实例打开文档;AttachLibrary 显示你自己管理的实例的所选文档(所有权仍在调用方)。缩放模式有固定百分比(通过 SetZoomPercent 设置 zmPercent)、适宽(zmFitWidth,默认)和适页(zmFitPage),两种适应模式会在控件大小变化时自动重新计算。导航会限制在文档页码范围内,只有可见页面真正变化时才触发 OnPageChange;OnZoomChange 报告每一次实际生效的缩放变化(包括 resize 或旋转时适应模式的重新计算),OnHScroll / OnVScroll 跟随滚动位置,工具栏和状态栏无需轮询即可保持同步。拖动垂直滚动条滑块时,浮动的"第 N / M 页"气泡预览目标页面(ScrollHintEnabled,默认开启)。GoToPagePoint 跳到某页并把指定的页面坐标滚到视口左上角(链接目标、搜索命中);ScrollBy、可读写的 HScrollPos / VScrollPos 属性(适合同步两个并排查看器)和 GetScrollPosForPage 提供程序化滚动控制,公开的桥接方法 ClientPointToPage / PageRectToClientRect 则在客户区像素与页面用户空间之间按缩放、旋转和滚动换算。SetDisplayMode 在单页显示(dmSinglePage,默认)和贯穿整个文档的连续垂直滚动(dmContinuous)之间切换,连续模式下只渲染视口内可见的页面,当前页面跟随滚动位置。已渲染页面保存在最近最少使用的位图缓存中(默认 128 MB,可用 SetCacheLimit 调整),往回滚动不会重新渲染。设置 DiskCacheFolder 还会把渲染好的页面持久化到磁盘、跨会话复用:每个文档的页面按文件路径、大小和写入时间的指纹存放(编辑文件自然使其条目失效),超出十个文档预算时按最近最少使用淘汰,重新打开最近看过的文件无需重新渲染即可显示页面(DiskCacheHits 报告磁盘命中页面的频率)。鼠标滚轮垂直滚动,按住 Ctrl 时沿标准预设档位(1–3200%)缩放;ZoomInStep / ZoomOutStep 以编程方式使用同样的预设步进(工具栏缩放动作也用它)。PageUp/PageDown、方向键、Home 和 End 用于导航;Ctrl+'+'/'-' 调整缩放,Ctrl+1/2/3 切换实际大小、适宽和适页,Shift+Up/Down 跳到首页和末页。相邻页面在后台线程预渲染,翻页即时完成(PrefetchEnabled,默认开启,会停止并重启工作线程),DiskCacheMaxDocuments 调节磁盘页面缓存保留的文档数量。OnBeforeSnapshot 在快照拖拽被捕获前触发,设置 Handled 可完全接管该区域。点击链接注释会触发 OnLinkClick,附带页码和类型化的动作摘要(TPDFlibActionInfo:网址、文档目标、命名动作等);LinkActionAtPagePoint 直接暴露命中测试,坐标与 GetAnnotRectEx 报告的用户空间一致。点击其他注释会触发 OnAnnotClick,附带页面、注释索引和子类型名称(AnnotAtPagePoint 是程序化命中测试);AnnotHints 和 ShowHint 开启时,悬停注释会把其 Contents 文本显示为控件提示,SaveAnnotAttachmentToFile 把点击的 FileAttachment 注释的内嵌文件写到磁盘——弹出批注、保存附件和检查评论因此都很直接。AcroForm 字段可交互填写:点击复选框或单选按钮切换状态,点击文本或选择字段在字段上打开就地编辑器(Enter 提交,Escape 取消,离开编辑器也提交),变化的页面自动重新渲染,每次写入都触发 OnFormFieldValueChanged;HighlightFormFields 在每个可填写区域上绘制半透明底色,FormFieldAtPagePoint、ToggleCheckBoxField、SetFormFieldValueAndRefresh 和 BeginEditFormField 以编程方式驱动同一套机制(只读字段不受影响)。多行文本字段在 memo 编辑器中编辑(Enter 插入换行,Ctrl+Enter 提交);Tab 和 Shift+Tab 让就地编辑器在文档的可编辑文本和选择字段间循环(FocusNextFormField 是对应的编程接口),SaveToFile / SaveToStream 把显示的文档——包括已提交的表单修改——写回磁盘或流,在控件内完成填写加保存的闭环。按住鼠标左键拖拽选择文本(点击链接则改为触发链接);Ctrl+C 或 CopySelectedText 复制选区,GetTextInRegion 以编程方式执行同样的基于文本块的区域查找。ViewerMode 属性决定鼠标左键的行为:vmSelect(默认)拖出文本选区,vmHand 用抓取光标平移视图、普通点击仍跟随链接,vmZoom 拖出选框并把框选区域放大到填满视口(普通点击向上步进缩放),vmSnapshot 把拖拽区域作为位图复制到剪贴板并触发 OnSnapshot,vmHighlight 给拖拽区域打上持久的半透明高亮。CaptureRegion 以编程方式暴露快照引擎,AddHighlightRegion / ClearHighlightRegions 管理持久高亮(页面坐标系,缩放、旋转和导航后仍然有效;颜色由 HighlightColor 控制)。PrintDoc 打印显示的文档:默认弹出标准 Windows 打印对话框(选择打印机、页码范围、份数、逐份打印),再把作业交给库的打印引擎;PrintDoc(False) 无 UI 地把所有页面打到默认打印机,PrintDocRange 是完全编程化的版本,接受打印机名和页码范围。RotateClockwise90 和 RotateAntiClockwise90 以 90 度为步长旋转整个视图(也可通过 ViewRotation 设置为 0、90、180 或 270);旋转只是视图属性,不动文档本身,任何角度下链接和文本选择照常工作,打开新文档时复位。配套控件 TPDFlibOutlineViewer 在查看器旁显示文档的书签树:把查看器实例传给它的 AttachViewer,点击书签即导航到目标页面。HighlightSearchHits 搜索整个文档并在每个命中处绘制半透明色条(SearchHighlightColor);色条在滚动、缩放和旋转时保持原位,直到 ClearSearchHighlights 移除。TPDFlibSearchPanel 以同样方式挂接:RunSearch 列出文档中的每个命中及其页面和匹配文本,点击命中即跳到该页,HighlightInView(默认开启)开启时面板还会在挂接的查看器里绘制命中色条。TPDFlibThumbnailBar 补齐了这个家族,为每页提供懒渲染的缩略图;点击缩略图即导航到该页。配套单元 Lib\PDFlibViewerActions.pas 提供二十个现成的 TAction 子类(注册在 losLab PDF 分类下),工具栏和菜单无需再写胶水代码:把每个 action 的 Viewer 属性指向控件,TPDFlibViewerFirstPage / TPDFlibViewerPriorPage / TPDFlibViewerNextPage / TPDFlibViewerLastPage、TPDFlibViewerZoomIn / TPDFlibViewerZoomOut / TPDFlibViewerFitWidth / TPDFlibViewerFitPage / TPDFlibViewerActualSize、TPDFlibViewerRotateClockwise / TPDFlibViewerRotateAntiClockwise、TPDFlibViewerPrint、TPDFlibViewerFileOpen / TPDFlibViewerFileSaveAs、TPDFlibViewerCopyText 以及工具模式 action TPDFlibViewerSelectMode / TPDFlibViewerHandMode / TPDFlibViewerMarqueeZoomMode / TPDFlibViewerSnapshotMode / TPDFlibViewerHighlightMode 会自动保持 enabled、checked 状态与文档和当前工具同步。安装设计期包 Lib\PDFlibViewerPkg.dpk,即可在设计期从 losLab PDF 组件面板页放置该控件。其余成员补全编程接口:RenderedWidth / RenderedHeight 报告当前渲染页面位图的像素尺寸(显示文档前为 0),EffectiveZoomPercent 报告实际生效的缩放(含计算出的适应系数),ScreenDPI 是点到像素的映射(从宿主显示器自动检测,无头渲染测试可覆盖;修改会清空位图缓存),IsPageCached / Cache 用于检视位图缓存,DrainPrefetched 为没有消息泵的宿主解码已完成的后台渲染,Lib 返回挂接的 TPDFlib 实例。表单字段的底层机制也完整暴露:GetFormFieldTabSequence 按稳定的 Tab 顺序返回所有可见控件,GetFormFieldEditorKind 解析字段将使用的编辑器类型,ActivateFormField 把字段的鼠标抬起动作派发给宿主(即使是按钮和签名控件,没有动作也会派发),EditingFieldIndex / FocusedFormFieldIndex 报告就地编辑器状态,字段动作每次触发都通过 OnFormFieldAction 通知宿主