« MBS FileMaker Plugin,… | Home | New error log functio… »

Add page links for FileMaker

Recently a client asked how to make existing index pages clickable in a PDF document. They have existing documents in FileMaker and like to customize them with our DynaPDF functions.

One part of the customization is to make some texts clickable. I recently proposed them to use DynaPDF.ExtractText to read the page number and then use DynaPDF.PageLink function to make the page. To know the distance, the layout has fields to enter where to find the numbers on the right side of the index page. Our loop runs from bottom to top of the page, checks every box for text and if there is a text, we make a link for that page.

 

Here is the script:

 

# Initialize DynaPDF if needed

If [ MBS("DynaPDF.IsInitialized") ≠ 1 ] 

Perform Script [ Specified: From list ; “InitDynaPDF” ; Parameter:    ]

End If

# Clear current PDF document

Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ] 

# optionally we can merge to external file

If [ False ] 

Set Variable [ $destPath ; Value: MBS("Folders.UserDesktop") & "/test.pdf" ] 

Set Variable [ $r ; Value: MBS("DynaPDF.OpenOutputFile"; $pdf; $destPath) ] 

If [ MBS("IsError") ] 

Show Custom Dialog [ "Failed to create PDF file." ; $r ] 

Exit Script [ Text Result:    ] 

End If

End If

# Merge files

Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; Add Page Links::Input PDF) ] 

If [ MBS("IsError") ] 

Show Custom Dialog [ "Failed to open PDF." ; $r ] 

Exit Script [ Text Result:    ] 

End If

# check if we have pages to import

Set Variable [ $r ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf) ] 

If [ MBS("IsError") ] 

Show Custom Dialog [ "Failed to import PDF." ; $r ] 

Exit Script [ Text Result:    ] 

End If

# now add page numbers

Set Variable [ $Found ; Value: 0 ] 

Set Variable [ $page ; Value: Add Page Links::First Index Page ] 

Loop

# edit page

Set Variable [ $r ; Value: MBS("DynaPDF.EditPage"; $pdf; $page) ] 

If [ MBS("IsError") = 0 ] 

Set Variable [ $pageHeight ; Value: MBS( "DynaPDF.GetPageHeight"; $PDF ) ] 

Set Variable [ $pageWidth ; Value: MBS( "DynaPDF.GetPageWidth"; $PDF ) ] 

Set Variable [ $x ; Value: Add Page Links::Distance Left ] 

Set Variable [ $y ; Value: Add Page Links::Distance Top ] 

Set Variable [ $rowHeight ; Value: Add Page Links::Height of Row ] 

Loop

# look for number

Set Variable [ $text ; Value: MBS( "DynaPDF.ExtractText"; $pdf; $page; ""; $x; $y; $x + Add Page Links::Width; $y + $rowHeight-5) ] 

If [ Length ( Trim ($text) ) > 0 ] 

# we found some text

If [ 0 ] 

# Optionally, show text we found

Set Variable [ $r ; Value: MBS( "DynaPDF.SetFont"; $pdf; "Helvetica"; ""; 20) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.WriteText"; $pdf; $x-100; $y; $text ) ] 

End If

If [ 0 ] 

# Optionally, draw some red boxes where we read text

Set Variable [ $r ; Value: MBS( "DynaPDF.SetFillColor"; $pdf; Random; 0; 0) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.SetStrokeColor"; $pdf; Random; 0; 0) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.Rectangle"; $pdf; $x; $y; Add Page Links::Width; $rowHeight; "stroke" ) ] 

End If

# add page link for the area we found

Set Variable [ $r ; Value: MBS( "DynaPDF.PageLink"; $pdf; 0; $y; $pageWidth; $rowHeight; $text ) ] 

Set Variable [ $Found ; Value: $Found + 1 ] 

End If

# next

Set Variable [ $y ; Value: $y + $rowHeight ] 

Exit Loop If [ $y > $pageHeight ] 

End Loop

End If

# next

Set Variable [ $page ; Value: $page + 1 ] 

Exit Loop If [ $page > Add Page Links::Last Index Page ] 

End Loop

Set Field [ Add Page Links::Numbers found ; $Found ] 

# to debug, you can disable compression and open PDF in text editor

// Set Variable [ $r ; Value: MBS("DynaPDF.SetCompressionLevel"; $pdf; 0) ] 

# show final PDF

Set Field [ Add Page Links::Output PDF ; MBS("DynaPDF.Save"; $pdf; "Merged.pdf") ] 

Set Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ] 

Export Field Contents [ Add Page Links::Output PDF ; “test.pdf” ; Automatically open ; Create folders: Off ] 

 

Let us know if you have questions. The example file will be added for the next pre-release, so you can easier copy the script.

18 06 22 - 15:41