TTMSFNCWXPDFViewer
The TTMSFNCWXPDFViewer is a component for displaying PDF files inside your application. It supports page navigation, zoom, rotation, text search, thumbnail generation, form field extraction, and page-to-image export. It uses the PDF.js JavaScript library.
Notice
With the default settings this component requires internet connection. If you'd like to run the component in an offline environment, please change the LibraryLocation property. Read more about this at the Loading modes paragraph of this documentation.
Loading a PDF
From a URL
CORS-protected URLs cannot be loaded this way. The PDF is fetched via an HTTP request.
procedure TForm1.Button1Click(Sender: TObject);
begin
TMSFNCWXPDFViewer1.LoadFromUrl('https://example.com/document.pdf');
end;
From a file (VCL, FMX, LCL)
From a Base64 string
The OnLoaded event fires when the PDF is ready. OnLoadError fires if loading fails.
Password-protected PDFs
Set the Password property before calling a load method:
Page navigation
// Navigate page by page
TMSFNCWXPDFViewer1.NextPage;
TMSFNCWXPDFViewer1.PreviousPage;
// Jump to a specific page (1-based)
TMSFNCWXPDFViewer1.ShowPage(5);
// Read current page and total pages
Label1.Caption := IntToStr(TMSFNCWXPDFViewer1.ActivePage) + ' / ' +
IntToStr(TMSFNCWXPDFViewer1.PageCount);
Zoom and rotation
TMSFNCWXPDFViewer1.ZoomIn;
TMSFNCWXPDFViewer1.ZoomOut;
TMSFNCWXPDFViewer1.FitToPage(True, False); // Fit to height
TMSFNCWXPDFViewer1.TurnAngleLeft; // Rotate 90° counter-clockwise
TMSFNCWXPDFViewer1.TurnAngleRight; // Rotate 90° clockwise
Text search
procedure TForm1.Button2Click(Sender: TObject);
begin
TMSFNCWXPDFViewer1.SearchText('annual report');
end;
procedure TForm1.TMSFNCWXPDFViewer1FoundSearchText(Sender: TObject;
AText: string; APage, AIndex: Integer);
begin
Memo1.Lines.Add('Found "' + AText + '" on page ' + IntToStr(APage));
end;
procedure TForm1.TMSFNCWXPDFViewer1NotFoundSearchText(Sender: TObject;
AText: string);
begin
ShowMessage('Text not found: ' + AText);
end;
Thumbnails
Thumbnails are disabled by default. Enable them before loading a PDF:
TMSFNCWXPDFViewer1.CalculateThumbnails := True;
TMSFNCWXPDFViewer1.ThumbnailSize := 128;
TMSFNCWXPDFViewer1.LoadFromFile('report.pdf');
Each thumbnail is delivered through OnGenerateThumbnail as a Base64 data URI:
procedure TForm1.TMSFNCWXPDFViewer1GenerateThumbnail(Sender: TObject;
NumPage: Integer; Base64Img: string);
var
base64Data: string;
bytes: TBytes;
ms: TMemoryStream;
begin
// Strip the data URI prefix: "data:image/png;base64,"
base64Data := Copy(Base64Img, Pos(',', Base64Img) + 1, Length(Base64Img));
bytes := TTMSFNCUtils.Decode64ToBytes(base64Data);
ms := TMemoryStream.Create;
try
ms.Write(bytes[0], Length(bytes));
ms.Position := 0;
// Load into a bitmap, image control, etc.
finally
ms.Free;
end;
end;
Alternatively, connect a TTMSFNCWXPDFThumbnailList which handles thumbnail display automatically.
Getting page text
// Get text for the current page:
TMSFNCWXPDFViewer1.GetPageAsText;
// Get text for a specific page:
TMSFNCWXPDFViewer1.GetPageAsText(3);
// Result arrives in OnGetPageText:
procedure TForm1.TMSFNCWXPDFViewer1GetPageText(Sender: TObject;
AText: string; APage: Integer);
begin
Memo1.Text := AText;
end;
Exporting the current page as an image
TMSFNCWXPDFViewer1.GenerateImageActualPage;
procedure TForm1.TMSFNCWXPDFViewer1GenerateImagePage(Sender: TObject;
APage: Integer; Base64Img: string);
begin
// Decode Base64Img and load into an image control
end;
Form fields
TMSFNCWXPDFViewer1.GetFormFields;
procedure TForm1.TMSFNCWXPDFViewer1GetFormFields(Sender: TObject;
AFields: string);
begin
// AFields is a JSON string describing all form fields in the PDF
Memo1.Text := AFields;
end;
Loading modes (LibraryLocation)
Most of the WX components offer the option to load the JavaScript library it depends on either online or offline. This option can be set via the LibraryLocation property. The available options means the following:
llOnline: The JavaScript library will be loaded live from an external URL. This allows a faster loading of the library, decreasing the loading time of the component and providing a better performance. However, this option also requires an active internet connection.llOffline: The JavaScript library will be loaded from resources. This guarantees the component is going to work in offline mode, but performance can decrease, especially on less powerful devices.
When you are ready to deploy your application, consider the differences above and make a choice based on them.
Properties
| Property | Type | Default | Description |
|---|---|---|---|
ActivePage |
Integer |
1 |
Gets or sets the current page number (1-based). |
CalculateThumbnails |
Boolean |
False |
When True, thumbnails are generated after each PDF load. |
LibraryLocation |
TTMSFNCWXLibraryLocation |
llOnline |
Controls whether PDF.js loads from CDN or embedded resources. |
PageCount |
Integer |
— | Read-only. Total number of pages in the loaded PDF. |
Password |
string |
'' |
Password for encrypted PDFs. Set before calling a load method. |
PDFRotationAngle |
Integer |
0 |
Current rotation angle in degrees (0, 90, 180, 270). |
ThumbnailSize |
Integer |
96 |
Size in pixels of the generated thumbnail images. |
Zoom |
Double |
1.0 |
Current zoom level. |
Methods
Loading
| Method | Description |
|---|---|
LoadFromBase64(FileBase64, PageNumber, AZoom, RotateAngle) |
Loads a PDF from a Base64 string. Optional parameters set the initial page, zoom, and rotation. |
LoadFromFile(FilePath) |
Loads a PDF from a file path. Not available for WEB. |
LoadFromUrl(FileUrl, PageNumber, AZoom, RotateAngle) |
Loads a PDF from a URL. CORS-protected URLs are not supported. |
Navigation
| Method | Description |
|---|---|
FitToPage(AHeight, AWidth) |
Zooms to fit the page height (AHeight=True) or width (AWidth=True). |
NextPage |
Navigates to the next page. |
PreviousPage |
Navigates to the previous page. |
ShowPage(AIndex: Integer) |
Navigates to the specified page (1-based). |
TurnAngleLeft |
Rotates the view 90° counter-clockwise. |
TurnAngleRight |
Rotates the view 90° clockwise. |
ZoomIn |
Increases the zoom level. |
ZoomOut |
Decreases the zoom level. |
Content extraction
| Method | Description |
|---|---|
GenerateImageActualPage |
Exports the current page as an image. Result via OnGenerateImagePage. |
GenerateThumbnails |
Generates thumbnails for all pages. Result via OnGenerateThumbnail. |
GenerateThumbnails(StartPage, EndPage) |
Generates thumbnails for a page range. |
GetFormFields |
Extracts form field data from the PDF. Result via OnGetFormFields. |
GetPageAsText(APageNumber) |
Extracts text from the specified page. Defaults to the current page when APageNumber = -1. Result via OnGetPageText. |
SavePageTextAsJSON |
Saves the current page's text as a JSON object. |
SearchText(TextToSearch: string) |
Searches all pages for the given text. Results via OnFoundSearchText. |
Events
| Event | Description |
|---|---|
OnFoundSearchText |
Fires for each search result from SearchText. |
OnGenerateImagePage |
Fires with the current page as a Base64 image after GenerateImageActualPage. |
OnGenerateThumbnail |
Fires for each generated thumbnail with the page number and Base64 image. |
OnGetFormFields |
Fires with JSON form field data after GetFormFields. |
OnGetPageText |
Fires with extracted text after GetPageAsText. |
OnHyperlinkClick |
Fires when a hyperlink in the PDF is clicked. |
OnLoadError |
Fires when an error occurs during loading. |
OnLoaded |
Fires when the PDF has fully loaded. Receives the total page count. |
OnNotFoundSearchText |
Fires when SearchText finds no matches. |
OnPageLoaded |
Fires when the current page content has been loaded. |
OnPageRendered |
Fires when the current page has been rendered. |
OnPinchIn |
Fires on pinch-in gesture (not WEB). |
OnPinchOut |
Fires on pinch-out gesture (not WEB). |
OnSearchTextFinished |
Fires when the search across all pages is complete. |
OnShowPage |
Fires when ShowPage is called. Receives the new page number. |
OnThumbnailProgress |
Fires during thumbnail generation with current position and total count. |
OnTouchEnd |
Fires when a touch gesture ends (not WEB). |
OnTouchMove |
Fires during a touch gesture (not WEB). |
OnTouchStart |
Fires when a touch gesture starts (not WEB). |
OnViewerInitialized |
Fires when the PDF.js viewer has fully initialized and is ready to load. |