« MBS Plugin Advent cal… | Home | Upcoming changes for … »

MBS Plugin Advent calendar: 15 - DynaPDF

Door 15 - DynaPDF

Fact of the day
The groundwork for today's PDF documents was laid back in 1991 with the Camelot project. The aim was to develop a file format that can capture data from all programs, this document can be shared and displayed in the same way on any end device so that it can also be printed

Today I would like to introduce you to an area that is very appreciated by our customers. We are talking about DynaPDF. With our DynaPDF functions you can work with your PDF documents. You can create PDF documents according to your wishes, write in these documents, sign them, create forms and much much more. In order to be able to work with DynaPDF without a watermark being permanently superimposed on your PDF pages, you need the correct additional DynaPDF license. Which license you need depends on what you want to do with DynaPDF. If you are only working create a PDF from scratch, for example, then all you need is a Starter license. If you want to load an existing PDF file, you need a Lite license. If you also want to optimize or render your PDF file, you need a Professional license. This table can help you decide which licenses you need.

Today in the example I would like to show you how to merge two PDF files and then add page numbers to them. As with LibXL, we first have to initialize DynaPDF and specify a library to work with. From the examples, you can use the InitDynaPDF script in your database and place the appropriate library file in the same folder as your database, or you can use the DynaPDF.Initialize function and enter the path to the appropriate library and your license key in the parameters. If you do not have a license key yet, because you want to test DynaPDF, then leave the license key blank. If you want to test whether your solution works with a specific license, then write the license name in the place of the license key. If you are using Windows, you must use the dll files. There are two dll files, one is for use on 32 bit systems and the other on 64 bit systems. On Mac you need the file with the extension .dylib. For use on a Linux server you need the dynapdf.linux.so file.

You need to perform the initialization before you use the first DynaPDF function. If you are working with the initialization script from the examples, it is advisable to place the following lines before each script in which DynaPDF functions are used.

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

First we have to create a fresh working environment into which we can import our two files one after the other. To do this, we use the DynaPDF.New function. This creates the working environment and returns a reference number with which we can continue working in the other functions.

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

Before we can import a file, we have to open this file. We can either import files from a PDF file on disk, in which case we use the DynaPDF.OpenPDFFromFile function, or we can import the file from a container, in that case we use the DynaPDF.OpenPDFFromContainer function to open the file. In the parameters, we first specify our working environment, followed by the file path or container. If your document to be opened here has a password, we can first enter the password type and then the password itself. Now we come to the import. If you only want to import one page, it is best to use the DynaPDF.ImportPDFPage function. If you want to import several pages, as in this example, use the DynaPDF.ImportPDFFile function. Here we specify the working environment in which we want to write our import and specify the page on which the first page of the imported document is to be placed. In Document 1, this is the first page. The function returns how many pages it has imported. We need this information for the start page of document two. We repeat the process with the second document.

# Combine 
Set Variable [ $pdf1 ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; DoorFifteen::FirstDocument) ] 
Set Variable [ $pages ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf; 1) ] 
Set Variable [ $pages ; Value: $pages + 1 ] 
Set Variable [ $pdf2 ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; DoorFifteen::SecondDocument) ] 
Set Variable [ $pages ; Value: MBS("DynaPDF.ImportPDFFile"; $pdf; $pages) ] 

Now we have a document in the working environment that contains both PDF files and it would be nice if we could number these pages. To do this, we write a text with the page number in a specific place on each individual page. First we set the coordinate alignment so that we can orient ourselves better. Normally the coordinates run from bottom to top and from left to right. We now want to change this and set the coordinates to take on larger values from top to bottom. This orientation is more intuitive for most people.

Then we determine the number of pages that are in the working environment. To do this we use the DynaPDF.GetPageCount function. We also specify the page number we want to start with. Before we can write the text on the page, we want to determine the dimensions of the page with DynaPDF.GetPageWidth and DynaPDF.GetPageHeight. With DynaPDF.EditPage we make a page whose index we specify in the parameters editable. We then use the DynaPDF.SetFont function to set the font and font size of the text that we want to write with the DynaPDF.WriteFTextEx function. This function writes the text with formatting commands to the current open page. In the parameters we specify our reference again, followed by the position of the text. We determine the position of the top left corner of the text field in our case 50 pixels away from the left and bottom edge. Then comes the size of the text field. In our case, we take the page width minus the margins to the right and left of 50 pixels. The height is 30. Now we can use the text alignment to decide exactly where our page number should be. We want it to be on the right-hand side, so we choose right-aligned text. Last but not least follows our text. This is made up as follows: Page number of total number of pages.

After we write our text on the page, we can stop editing this page and run through the loop again until we have reached the last page.

Set Variable [ $r ; Value: MBS( "DynaPDF.SetPageCoords"; $PDF; "TopDown" ) ] 
Set Variable [ $PageCount ; Value: MBS( "DynaPDF.GetPageCount"; $pdf ) ] 
Set Variable [ $PageNumber ; Value: 1 ] 
Set Variable [ $pageWidth ; Value: MBS("DynaPDF.GetPageWidth"; $pdf) ] 
Set Variable [ $pageHeight ; Value: MBS("DynaPDF.GetPageHeight"; $pdf) ] 
	Set Variable [ $r ; Value: MBS("DynaPDF.EditPage"; $pdf; $PageNumber) ] 
	Set Variable [ $r ; Value: MBS( "DynaPDF.SetFont"; $pdf; "Helvetica"; 0; 20) ] 
	Set Variable [ $r ; Value: MBS( "DynaPDF.WriteFTextEx"; $pdf; 50; $pageHeight - 50; 
	 	$pageWidth-100; 30; "right"; GetAsText($PageNumber) & " of " & $PageCount) ] 
	Set Variable [ $r ; Value: MBS("DynaPDF.EndPage"; $pdf) ] 
	Set Variable [ $PageNumber ; Value: $PageNumber +1 ] 
	Exit Loop If [ $PageNumber > $PageCount ] 
End Loop

Finally, we save the PDF document in a separate container. To do this, we use the DynaPDF.Save function.

Set Field [ DoorFifteen::Combine ; MBS( "DynaPDF.Save"; $PDF ; "Combine.pdf") ] 

After we have finished our work we have to release the working memory. If we want to release all references at the same time we use the function "DynaPDF.ReleaseAll. If we only want to release a single reference, we can use the DynaPDF.Release function by specifying it in the parameters.

I hope you liked the door this time as well and we'll see us again tomorrow for the next door.

Monkeybread Software Logo with Monkey with Santa hat
14 👈 15 of 24 👉 16
Claris FileMaker Plugin
15 12 23 - 07:38