« One month till Xojo.C… | Home | Multithreaded JavaScr… »

Merge PDF with table of contents

We got a new example database to show how to merge PDFs. One of the feature beside adding page numbers is to build a table of contents for the document with clickable entries:

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

#

# create dummy page for table

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

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

# create table for index

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

Set Variable [ $table ; Value: MBS("DynaPDF.Table.Create"; $pdf; 10; 2; 500; 20) ] 

Set Variable [ $r ; Value: MBS("DynaPDF.Table.SetFont"; $table; -1; -1; "Helvetica"; 0; 1; "unicode") ] 

Set Variable [ $r ; Value: MBS("DynaPDF.Table.SetFontSize"; $table; -1; -1; 14) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.Table.SetColWidth"; $Table; 0; 450; 1 ) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.Table.SetColWidth"; $Table; 1; 50;1 ) ] 

# Build Table Of Contents

Go to Record/Request/Page [ First ]

Set Variable [ $destPage ; Value: 2 ] 

Loop

# open PDF

If [ not IsEmpty ( Merge PDFs::InputPDF ) ] 

Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; Merge PDFs::InputPDF) ] 

If [ MBS("IsError") ] 

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

Exit Script [ Text Result:    ] 

End If

Else If [ not IsEmpty ( Merge PDFs::InputPath ) ] 

Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromFile"; $pdf; Merge PDFs::InputPath) ] 

If [ MBS("IsError") ] 

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

Exit Script [ Text Result:    ] 

End If

Else

# no PDF?

End If

# check if we have pages to import

Set Variable [ $pageCount ; Value: MBS( "DynaPDF.GetImportPageCount"; $PDF ) ] 

If [ $pageCount > 0 ] 

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

If [ GetAsNumber($lastPage) >0 ] 

# put in a named destination for this new page

Set Variable [ $NamedDest ; Value: "Record" & Get(RecordID) ] 

Set Variable [ $NamedDestHandle ; Value: MBS( "DynaPDF.CreateNamedDest"; $PDF; $NamedDest; $DestPage; "Fit") ] 

# add table entry

Set Variable [ $rowNum ; Value: MBS("DynaPDF.Table.AddRow"; $table) ] 

Set Variable [ $r ; Value: MBS("DynaPDF.Table.SetCellText"; $table; $rowNum; 0; "left"; "top"; Merge PDFs::InputName) ] 

Set Variable [ $r ; Value: MBS("DynaPDF.Table.SetCellText"; $table; $rowNum; 1; "right"; "top"; $destPage) ] 

# add click action

Set Variable [ $Action ; Value: MBS( "DynaPDF.CreateGoToActionEx"; $PDF; $NamedDestHandle) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.Table.SetCellAction"; $Table; $rowNum; 0; $action; "Invert" ) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.Table.SetCellAction"; $Table; $rowNum; 1; $action; "Invert" ) ] 

Set Variable [ $destPage ; Value: $lastPage + 1 ] 

End If

End If

# optionally close import files

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

# next file

Go to Record/Request/Page [ Next ; Exit after last: On ]

End Loop

# Draw Table

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

Set Variable [ $r ; Value: MBS("DynaPDF.Table.Draw"; $table; 50; 50; 742) ] 

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

# show final PDF

Set Field [ Merge PDFs::FinalPDF ; MBS("DynaPDF.Save"; $pdf; "Merged.pdf") ] 

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

The example file will be included in the next plugin prerelease. Please do not hesitate to contact us with questions.

25 02 20 - 17:10