TPDFlibViewer

Viewer

Description

This page describes the TPDFlibViewer control in PDFlibPas under the “Viewer” topic. API names, parameters, class names, filenames, and literals are kept exactly as they appear in the Delphi library.

TPDFlibViewer (unit Lib\PDFlibViewer.pas) is a VCL scrolling control that displays PDF pages inside your application. It renders through the same engine TPDFlib uses (GDI+, Cairo or PDFium, following SelectRenderer on the attached instance).

Key Members

Function LoadFromFile(Const FileName, Password: WideString): Integer;
Procedure AttachLibrary(ALib: TPDFlib);
Procedure CloseDocument;
Function PageCount: Integer;
Function GoToPage(NewPage: Integer): Integer;
Function NextPage: Integer;
Function PrevPage: Integer;
Procedure SetZoomPercent(Percent: Double);
Procedure SetZoomMode(Mode: TPDFlibViewerZoomMode);
Procedure SetDisplayMode(Mode: TPDFlibViewerDisplayMode);
Procedure SetCacheLimit(MegaBytes: Integer);
Procedure RefreshPage;
Function LinkActionAtPagePoint(APage: Integer; XPos, YPos: Double; Out Info: TPDFlibActionInfo): Integer;
Function GetTextInRegion(APage: Integer; XPos, YPos, RWidth, RHeight: Double): WideString;
Function CopySelectedText: Integer;
Property SelectedText: WideString;
Property Page: Integer;
Property DisplayMode: TPDFlibViewerDisplayMode;
Property OnPageChange: TPDFlibViewerPageChange;
Property OnLinkClick: TPDFlibViewerLinkClick;

Remarks

LoadFromFile opens a document through the control's own TPDFlib instance; AttachLibrary displays the selected document of an instance you manage yourself (the caller retains ownership). Zoom modes are a fixed percentage (zmPercent via SetZoomPercent), fit-to-width (zmFitWidth, the default) and fit-to-page (zmFitPage); fit modes recompute automatically when the control resizes. Navigation clamps to the document's page range and fires OnPageChange only when the visible page really changes. SetDisplayMode switches between single-page display (dmSinglePage, the default) and a continuous vertical scroll through the whole document (dmContinuous), where only the pages visible in the viewport are rendered and the current page follows the scroll position. Rendered pages are kept in a least-recently-used bitmap cache (128 MB by default, adjustable with SetCacheLimit), so scrolling back does not re-render. The mouse wheel scrolls vertically; PageUp/PageDown, the arrow keys, Home and End navigate. Neighbouring pages pre-render on a background thread so paging is instant. Clicking a link annotation fires OnLinkClick with the page number and the typed action summary (TPDFlibActionInfo: web address, document destination, named action and more); LinkActionAtPagePoint exposes the hit test directly, taking co-ordinates in the same user space GetAnnotRectEx reports. Dragging with the left mouse button selects text (a click on a link fires the link instead); Ctrl+C or CopySelectedText copies the selection, and GetTextInRegion performs the same block-based region lookup programmatically. The companion control TPDFlibOutlineViewer shows the document's bookmark tree beside the viewer: call its AttachViewer with the viewer instance and clicking a bookmark navigates to its destination page. TPDFlibSearchPanel attaches the same way: RunSearch lists every hit in the document with its page and matching text, and clicking a hit jumps to that page. TPDFlibThumbnailBar completes the family with a lazily rendered miniature of every page; clicking a thumbnail navigates to it. Install the design-time package Package\PDFlibViewerPkg.dpk to place the control from the losLab PDF palette page at design time.