TMS FNC WX PDF Viewer — Guides
Loading documents
Use LoadFromFile for local files, LoadFromUrl to load from a remote URL, or LoadFromBase64 for a document you already hold in memory. Handle OnLoaded to know when the document is open and its page count is known, and OnLoadError when the document cannot be opened.
Password protected documents
A protected document needs its password before it is opened: Password is read while the document loads, so assigning it after the load call has no effect on the document already loading. A missing or wrong password does not raise an exception — it arrives as OnLoadError, which is why that handler should be wired whenever the password can come from a user.
uses
FMX.TMSFNCWXPDFViewer;
procedure TForm1.OpenProtectedButtonClick(Sender: TObject);
begin
PDFViewer1.OnLoadError := PDFViewer1LoadError;
// Password is read while the document is being opened, so assign it before
// the LoadFromFile, LoadFromUrl or LoadFromBase64 call - setting it
// afterwards has no effect on the document that is already loading. Leave it
// empty for documents that are not protected.
PDFViewer1.Password := 'letmein';
PDFViewer1.LoadFromFile('C:\Documents\payslip.pdf');
end;
procedure TForm1.PDFViewer1LoadError(Sender: TObject; ErrorMsg: string);
begin
// A missing or wrong password surfaces here instead of through OnLoaded.
ShowMessage('Could not open the document: ' + ErrorMsg);
end;
Navigating pages
Set ActivePage or call ShowPage with a one-based page number to jump to any page, and NextPage / PreviousPage to step through the document. The OnShowPage event fires with the new page number when the visible page changes.
Searching text
Call SearchText to highlight occurrences. Handle OnFoundSearchText to receive match results, OnNotFoundSearchText when a page has no match, and OnSearchTextFinished when the search scan completes.
Zoom and rotation
Set Zoom to a factor, where 1 is 100% scale, or step it with ZoomIn and ZoomOut. Change PDFRotationAngle using a TTMSFNCWXPDFViewerRotationAngle value to rotate all pages, or turn one step at a time with TurnAngleLeft and TurnAngleRight.
Fitting the page to the viewer
FitToPage replaces the manual zoom arithmetic: it derives a new Zoom from the rendered page size and the viewer's own size. Because it reads the rendered page dimensions, call it once the page has been rendered — OnPageRendered is the natural place, and re-calling it on resize keeps the fit.
uses
FMX.TMSFNCWXPDFViewer;
procedure TForm1.FormCreate(Sender: TObject);
begin
PDFViewer1.OnPageRendered := PDFViewer1PageRendered;
PDFViewer1.LoadFromFile('C:\Documents\report.pdf');
end;
procedure TForm1.PDFViewer1PageRendered(Sender: TObject);
begin
// FitToPage computes a new Zoom from the rendered page dimensions, so it
// needs a page that has actually been rendered. Calling it before the first
// OnPageRendered has no page size to work from.
PDFViewer1.FitToPage(True, False);
end;
procedure TForm1.FitWidthButtonClick(Sender: TObject);
begin
// Fit the page width instead of its height.
PDFViewer1.FitToPage(False, True);
end;
procedure TForm1.FitPageButtonClick(Sender: TObject);
begin
// With both flags the smaller of the two zoom factors wins, so the complete
// page stays inside the viewer.
PDFViewer1.FitToPage(True, True);
end;
Thumbnail generation
Call GenerateThumbnails to produce thumbnails for the whole document, or the GenerateThumbnails(StartPage, EndPage) overload to limit the work to a page range — worth doing on long documents, since each thumbnail is rendered individually. Set ThumbnailSize for the edge length in pixels, handle OnGenerateThumbnail to receive each result image as base64, and OnThumbnailProgress to drive a progress indicator. Set CalculateThumbnails to have them produced automatically while a document loads.
Form fields
GetFormFields asks the loaded document for its interactive form fields. It is asynchronous: the result never comes back as a return value, it arrives in OnGetFormFields as a JSON string describing the field objects. Call it no earlier than OnLoaded, since it queries the open document.
uses
FMX.TMSFNCWXPDFViewer, FMX.TMSFNCUtils, System.JSON;
procedure TForm1.FormCreate(Sender: TObject);
begin
PDFViewer1.OnLoaded := PDFViewer1Loaded;
PDFViewer1.OnGetFormFields := PDFViewer1GetFormFields;
PDFViewer1.LoadFromFile('C:\Documents\application-form.pdf');
end;
procedure TForm1.PDFViewer1Loaded(Sender: TObject; NumPages: Integer);
begin
// GetFormFields queries the loaded document, so it can only be called once
// the document is open. OnLoaded is the earliest safe point.
PDFViewer1.GetFormFields;
end;
procedure TForm1.PDFViewer1GetFormFields(Sender: TObject; AFormFields: string);
var
LFields: TJSONValue;
begin
// AFormFields is a JSON array whose single element holds the form field
// objects of the document, keyed by field name. The call is asynchronous:
// the result arrives here, never as a return value of GetFormFields.
LFields := TTMSFNCUtils.ParseJSON(AFormFields);
if LFields = nil then
Exit;
try
Memo1.Lines.Text := LFields.ToString;
finally
LFields.Free;
end;
end;