Skip to content

Guest Post: Using the Graphics Class to Create PDF Documents

Monkeybread Software started with the DynaPDF plugin for Xojo about twelve years ago. As DynaPDF is a C++ library, the wrapping plugin mimics the original C API and offers it for Xojo. Over the years a lot more convenient methods have been added to make the plugin more Xojo-like. For example, newer methods can process pictures directly, take colors as Xojo color values and allow drawing of styled text directly.

Since the early days there has been the feature request to use the graphics class in Xojo to draw to a PDF document. We recently came back to this old feature request and decided to try a new way to implement it and our new code seems to work just fine. With some help from Xojo engineers, we even got the alignment right.

At the Xojo Developer Conference 2019 in Miami in the presentation about news in MBS Xojo Plugin, Christian Schmitz presented this new feature. We can draw the normal drawing commands for lines, rectangles, polygons and pictures, but also text and even vector graphics. With the support of vector graphics, you can even use the report engine in Xojo and let it output your reports as PDF documents.

In your project, you can have a central draw(g as graphics) method, which handles drawing your content. For a print preview, you can let it draw to a picture and show the picture scaled down in a canvas. The same code can be called for drawing to a graphics object from printer received via OpenPrinterDialog function. And for saving as PDF, you can use the graphics object from DynaPDF. Just query PageGraphics object while you have a PDF page open.

Here is a code example:

Dim pdf As New DynapdfMBS
Dim f As FolderItem = SpecialFolder.Desktop.Child(“DynaPDF Graphics.pdf”)

// For this example you can use a Starter, Lite, Pro or Enterprise License
‘pdf.SetLicenseKey “Starter” 

// Create new PDF. Use f = nil for in-memory PDF
Call pdf.CreateNewPDF(f)

// append a new page, so we have an open page
Call pdf.Append 

// get graphics object to draw on the page
Dim g As Graphics = pdf.PageGraphics

// draw as usual
g.DrawString “Hello World”, 105, 100
g.DrawRect 100, 80, 100, 100

// close page and file
Call pdf.EndPage 
Call pdf.CloseFile

// for in-memory PDF, use GetBuffer here to grab it.
‘Dim PDFData As String = pdf.GetBuffer

As you see, we use normal drawing commands for adding text to the page. A few lines are needed to setup the PDF environment and after drawing to properly close the file. If you like to customize the new PDF pages, you can implement NextPage event on DynaPDFMBS class and for example import an existing PDF document as letter paper for your background. You should also implement Error event to handle any problem while creating the PDF document. 

Depending on your DynaPDF license, other features like rendering PDF Pages, importing of other PDF pages or digital signatures may be possible. But even with starter license, you can create new PDF documents and draw into them. If you have questions, please do not hesitate to contact Monkeybread Software.

Christian Schmitz has been active in the Xojo community for over 20 years. As the developer behind the Monkeybread Software Plugins he provides thousands of classes and example projects to help you build better applications with Xojo. In Europe, Christian has organized over 10 conferences, including the upcoming conference in Cologne this fall. You may have also read his articles in Xojo Developer Magazine. DynaPDF and other MBS plugins are available in the Xojo Add-Ons Store and at Monkeybread Software.