Example Script for DynaPDF.FindText and DynaPDF.WebLink
Included with lastest MBS FileMaker Plugin prerelease 10.2pr6 we have a new database named Add Weblinks.fmp12, made for a client in the last days. The script below walks over PDF pages and searches for some text to put a link on that position to reference a website.
This example may be useful for all of you, who want to easily make words clickable in the PDF text. We simply search for a word to get the coordinates with our DynaPDF.FindText function. Then we split the coordinates into individual variables and call DynaPDF.WebLink to add the URLs to the PDF page. Works great if you have a list of products and you want to make them clickable to point to the web shop. As we search for text this only works for text on the page matching the search term exactly (case insensitive option available).
Of course if you would assemble the PDF with DynaPDF functions in MBS FileMaker Plugin, you would know the positions exactly and place the link directly to known coordinates. For example the DynaPDF.Table.SetCellAction function allows to link a cell in a table directly to an action and jump to another page or to an URL. Also if you write text with our DynaPDF.WriteFTextEx function, you know the coordinates and can directly put the link there.
Below the script. If you have questions, please don't hesitate to contact us.
#
# Initialize DynaPDF if needed
#
If [ MBS("DynaPDF.IsInitialized") ≠ 1 ]
Perform Script [ Specified: From list ; “InitDynaPDF” ; Parameter: ]
End If
If [ MBS("DynaPDF.IsInitialized") ≠ 1 ]
Exit Script [ Text Result: ]
End If
#
# go to PDF Layout
Go to Layout [ “PDF” (PDF) ; Animation: None ]
If [ Get(LastError) ≠ 0 ]
Exit Script [ Text Result: ]
End If
#
# Start new PDF
#
Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ]
# Set some flags for import
Set Variable [ $r ; Value: MBS("DynaPDF.SetImportFlags"; $pdf; "ImportAll ImportAsPage") ]
#
# Add first page
Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; PDF::Input PDF) ]
Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf; 1) ]
#
# setup drawing for links
Set Variable [ $r ; Value: MBS("DynaPDF.SetLineWidth"; $pdf; 1) ]
Set Variable [ $r ; Value: MBS("DynaPDF.SetLinkHighlightMode"; $pdf; "invert") ]
#
#
# Add Links
Set Variable [ $count ; Value: 0 ]
Go to Related Record [ Show only related records ; From table: “Links” ; Using layout: “Links” (Links) ]
If [ Get(FoundCount) > 0 ]
Set Variable [ $PageCount ; Value: MBS( "DynaPDF.GetPageCount"; $pdf ) ]
Set Variable [ $PageNumber ; Value: 1 ]
Loop
Set Variable [ $r ; Value: MBS("DynaPDF.EditPage"; $pdf; $PageNumber) ]
#
# check for all texts
Go to Record/Request/Page [ First ]
Loop
Set Variable [ $pos ; Value: MBS( "DynaPDF.FindText"; $pdf; Links::Text; 1) ]
If [ IsEmpty ( $pos ) ]
# not found
Else
# read positions
Set Variable [ $pos ; Value: Substitute($pos; " "; ¶) ]
Set Variable [ $x1 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 1)) ]
Set Variable [ $y1 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 2)) ]
Set Variable [ $x2 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 3)) ]
Set Variable [ $xy ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 4)) ]
Set Variable [ $x3 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 5)) ]
Set Variable [ $y3 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 6)) ]
Set Variable [ $x4 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 7)) ]
Set Variable [ $y4 ; Value: MBS( "Math.TextToNumber"; GetValue($pos; 8)) ]
Set Variable [ $w ; Value: MBS( "Math.TextToNumber"; $x3-$x1) ]
Set Variable [ $h ; Value: MBS( "Math.TextToNumber"; $y3-$y1) ]
# place web link
Set Variable [ $r ; Value: MBS( "DynaPDF.WebLink"; $pdf; $x1; $y1; $w; $h; Links::URL ) ]
#
Set Variable [ $count ; Value: $count +1 ]
End If
#
# next link...
Go to Record/Request/Page [ Next ; Exit after last: On ]
End Loop
#
#
# next page...
Set Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ]
Set Variable [ $PageNumber ; Value: $PageNumber +1 ]
Exit Loop If [ $PageNumber > $PageCount ]
End Loop
End If
#
#
Go to Layout [ “PDF” (PDF) ; Animation: None ]
# save PDF
Set Variable [ $PDFData ; Value: MBS("DynaPDF.Save"; $pdf; GetAsText(PDF::Input PDF)) ]
#
# Free all data
Set Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ]
#
# Put in Container
Set Field [ PDF::Output PDF ; $PDFData ]
Show Custom Dialog [ "done" ; $count & " links placed." ]