Let me explain how to get started with MBS Xojo DynaPDF Plugin.
But first what is DynaPDF?
The DynaPDF library is a full featured PDF library for C developers and we provide you a Xojo integration as a plugin. With DynaPDF you get functionalities to create, edit, merge, analyze or sign PDF files within your Xojo application. With DynaPDF you can e.g. write an invoice for your customer in which you integrate a barcode with payment information and attach the always same terms and conditions to the PDF document. This invoice can also be converted to PDF/A for archiving purposes.
While your users can open a PDF and do some operations in Acrobat Reader (or the full Acrobat product), our plugin allows you to do things automatically in your code. You write the code to use the plugin to apply changes to many PDFs. Like process a folder of 100 invoices, extract text from each and look for vendor names in them to tag them automatically. Or use regular expressions to find the bank account and amounts.
If you want to use the MBS Xojo DynaPDF Plugin, you need a suitable license of DynaPDF and the MBS Xojo DynaPDF Plugin. You can of course just test the plugin without a license and try all functions before you order. There are four different license levels available for DynaPDF. Which license you need depends strongly on which methods you plan to use. For example, if you only want to insert links in a new PDF document, a starter license is sufficient. If you also want to add a page to an existing PDF document, you need a Lite license for the import feature. If you want to read images from existing PDF files or import single PDF pages from another PDF document, you need a Professional license. Same applies for rendering pages for display or printing. With the additional PDF/A converter license, you get the possibility to convert already existing documents from PDF to PDF/A. All MBS DynaPDF licenses can be used for Xojo, FileMaker, C/C++, C#, Delphi, Lazarus, PHP, VB, VBA, and VB .Net with the appropriate DynaPDF versions.
I want to show you an example that use DynaPDF functionalities. In this example we want to create a new PDF and write a text in the upper right corner.
Before we code, we have to think a minute about error handling. Since MBS Xojo Plugins in version 20.4, we have a feature to use exceptions for reporting errors. The older and still well recommended way is to subclass DynaPDFMBS and implement the error event. There you can react to errors, log them and decide what to do. For example if a font is not found, you may ignore it and handle it in your normal code and load a replacement font. But otherwise the PDF creation can continue.
For each time you use DynaPDF functions, please create an instance of DynaPDFMBS class or a subclass. You may use multiple instances on different threads to do processing in parallel.
In case you have a license key, please call DynaPDFMBS.SetLicenseKeyGlobal method early in app.open and set the license code. If you have no key, you use demo mode and enjoy the watermark. To test whether your solution can work with Pro, Lite or Starter level, you can pass "Pro", "Lite" or "Starter" as license key. In that case you get an error if you use a command which is not available for that level.
Now we can finally start with PDF code. First we create a new working environment using the method CreateNewPDF and save it in a folder item f. Pass nil for the folderitem to create a PDF in memory. This document has no page at the moment. With method Append we attach a new page.
Call pdf.CreateNewPDF f
We would like to position the text. For this we need the page height and the page width which we get with the following methods.
Dim h As Double = pdf.GetPageHeight
Dim w As Double = pdf.GetPageWidth
Default page size is A4, but with SetPageFormat other sizes like US Letter are available, too.
call pdf.SetPageFormat pdf.kpfDIN_A4
By default the orientation of the coordinates of DynaPDF environments is in the lower left corner, that means the higher the coordinate values become in the y axis we go up the page. For humans this is quite difficult to imagine. Therefore we can set the coordinate origin with SetPageCoords and the constant „kpcTopDown" in the parameter to be in the upper left corner.
call pdf.SetPageCoords pdf.kpcTopDown
Now we want to set the informations of the font. For this we enter the following line.
Call pdf.SetFont "Times", pdf.kfsItalic, 40.0, True, pdf.kcpUnicode
In the parameters we then specify the font, the style, the font size, whether the font should be embedded in the PDF and the encoding. In our case, we want the font Arial in italics and in font size 40. Unicode encoding is best, although other variants exist for ancient PDF readers older than 10 years.
Call pdf.WriteFTextEx(50, 50, pdf.GetPageWidth-100, 100, pdf.katRight, "Hello World")
Now we write some text. For this we have a couple of methods and the WriteFTextEx method is good to fit text in a rectangle. The upper left corner of our text field should be 50 points away from the upper and left border. For this we specify the 50 for the x and y position in the parameters. Then the size of the text field follows with a size of page width -100. This number results from the fact that we want to write a text as long as possible and use the whole page width. But there should always be 50 points distance to the edges. Left margin + right margin is therefore 100 points. PDF uses by default a scale with 72 points per inch, which is about 28.35 per cm. The height of the text box should be 50. We want the text in the upper right corner. That means the text alignment must be right-aligned. Then follows our text. In our case "Hello World"
Many methods from the DynaPDF world require a page to be set to editable. Also the WriteFTextEx method needs an editable page. The page is already automatically editable by the method Append. So we do not have to call the method EditPage separately. When the editing of the page is finished we have to call the method EndPage to finish the editing and assemble the content stream. Then we can close the working environment and open and display the file.
If you created an in-memory PDF, you can get it after the CloseFile command with the GetBuffer function.
But the MBS Xojo DynaPDF Plugin can do much more. Check the documentation and our example projects that are included in the plugin download. Here you can find a lot of examples that shows you the possibilities.
For example you find projects that show how you can extract images or text, convert a PDF to PDF/A, add watermarks and annotations and much more.
If you have any questions or would like to purchase a license you can contact us.
I hope you enjoy the DynaPDF functionalities.
Written by Stefanie and Christian @ MBS.