Skip to content

TTMSFMXNativePDFDocumentViewer

The TTMSFMXNativePDFDocumentViewer is a view controller that is capable of displaying a PDF.

Methods

Methods name Description
LoadDocument Loads the document attached to the Document property (TTMSFMXNativePDFDocument)
SupportsPencil Returns a boolean whether PencilKit is supported (iOS 16 and newer)
GoToPage Navigates to a specific page
GoToNextPage Navigates to the next page
GoToPreviousPage Navigates to the previous page

Properties

Properties name Description
IsUserInteractionEnabled Sets a Boolean whether the PDF viewer can be interacted with
Document The PDF document to be viewed
ActivePage The current active page
ActivePageIndex The current active page index

Events

Events name Description
OnPageChanged Event called when a page has changed
OnSelectionChanged Event called when text selection changes
OnScaleChanged Event called when zooming in/out
OnAnnotationWillHit Event called when an annotation will be hit
OnAnnotationHit Event called when an annotation is hit
OnAnnotationsUpdated Event called when annotations are update
OnPDFViewWillClickOnLink Event called when a link is clicked on the page

TTMSFMXNativePDFDocument

The TTMSFMXNativePDFDocument is a non visual component that is capable of loading and saving a PDF. Additionally, the PDF can be annotated via custom or free drawn annotations.

Methods

Methods name Description
LoadFromFile Loads the document from a location on the disk/operating system
LoadFromURL Loads the document from an URL
SaveToFile Saves the document to a location on the disk/operating system
SaveToStream Saves the document to a stream
RemovePage Removes a specific page
RemoveAllPages Removes all pages
GetPage Retrieves a specific page
GetPageIndex Retrieves a specific page index
AddPage Adds a new page
InsertPage Inserts a new page

Properties

Properties name Description
PageCount Sets a Boolean whether the PDF viewer can be interacted with
DocumentURL The PDF document to be viewed

Enabling the Pencil/FreeDraw

The main feature of the TTMSFMXNativePDFDocumentViewer is to annotate a PDF. This can be done by enabling the free draw view which lies on top of the PDF. To enable the freedraw view, you have 2 properties, but it depends on the iOS framework. Starting from iOS 16, it's possible to use the built-in PencilKit tool picker.

if TMSFMXNativePDFDocumentViewer1.SupportsPencil then
  TMSFMXNativePDFDocumentViewer1.FreeDraw.PencilEnabled := True
else
  TMSFMXNativePDFDocumentViewer1.FreeDraw.Enabled := True;

Toggling the Enabled (or PencilEnabled) property will automatically convert the drawings to annotations and store it inside the PDF.

Adding custom annotations

Free drawn annotations are added by drawing on top of the PDF page view, but it's also possible to add custom annotations by code. To add an annotation to a specific area on a page use the following code:

procedure TForm1.AddAnnotation(AType: TTMSFMXNativePDFAnnotationType);
begin
  if not Assigned(docview.DocumentViewer.currentSelection) then
    ShowMessage('Please select text first')
  else
  begin
    docview.ActivePage.AddAnnotation(docview.GetBoundsForCurrentSelection, AType);
  end;
end;

procedure TForm1.DoFreeDrawTouch(Sender: TObject; ARect: TRectF;
  APage: TTMSFMXNativePDFPage);
var
  r: TRectF;
  a: TTMSFMXNativePDFAnnotation;
begin
  if (ARect.Width <= 0) or (ARect.Height <= 0) then
    r := RectF(ARect.Left, ARect.Top, ARect.Left + 100, ARect.Top + 50)
  else
    r := ARect;

  a := APage.AddAnnotation(r, atFreeText);
  a.Contents := 'Hello World !';
end;

The type of annotation depends on the usage. Below are the various types of annotations that can be added to a page/document.

atCircle, atFreeText, atHighlight, atInk, atLine, atLink, atPopup, atSquare, atStamp, atStrikeOut, atText, atUnderline, atWidget.

There are a couple of properties at the annotation that can be used to define the annotation.

property Modificationdate: TDateTime;
property UserName: string;
property Contents: string;
property Bounds: TRectF;
property Color: TAlphaColor;
property ShouldDisplay: Boolean;
property ShouldPrint: Boolean;
property FieldName: string;
property WidgetFieldType: TTMSFMXNativePDFAnnotationWidgetSubtype;
property WidgetControlType: TTMSFMXNativePDFAnnotationWidgetControlType;

Saving a document

After manipulating / annotating a document, you can save the document to a new location and store the annotations alongside the document.

procedure TForm1.Button1Click(Sender: TObject);
var
  fn: string;
begin
  fn := TPath.GetDocumentsPath + PathDelim + 'saved.pdf';
  doc.SaveToFile(fn);
  doc.LoadFromFile(fn);
  docview.Document := doc;
end;