Generating PDF Documents via MXS and .NET

Apr 14, 2009 by     No Comments    Posted under: 3dsMax, DotNet, Imaging, Maxscript, Program Automation, Technical Research

Recently, I discovered an open source library written in C# called PDFSharp. This came about because I was working on a script that generated a text file with information in at the end. Unfortunately, when it came to refining the presentation, my ASCI skills are seriously limited. For example, my attempt at the LoneRobot logo looked like this –

............o
............|
......... ___
........ /../
......8=/- /=8  - BeepBeepDoWopDoDoopDeep!
....... ---
........| |
........o.o

In the magazine “ASCI Art Lovers Monthly” , this attempt gained a four asterisk rating, although others have subsequently informed me that the **** peppered over the artistic comments page are not related to this.

Because this generated document was to be printed and used as reference, It got me thinking about whether I could generate better printable documents directly from 3dsMax without having to use OpenOffice, Acrobat or any of my presentation skills. Fortunately, PDFSharp is an assembly that allows you to create PDF documents. The way it does this is to mirror a whole set of GDI+/WPF drawing classes and allow you to construct the page programmatically via DotNet. (If you want to look at a few DotNet GDI+ drawing methods for 3dsmax, check out this page)

Regardless of it’s limited appeal within Max, it is an amazing piece of open source code that should most certainly be looked at.

In order to integrate this, I wrote a small MXS Struct to wrap a few of the functions of PDFSharp’s GDI+ drawing methods, since this was all I was interested in using for my purposes. There are many, many more drawing and formatting options available through this assembly, but in the struct the following methods are available –

  • DrawImage (With optional scale multiplier)
  • DrawRectangle (Specify a corner radius to draw a rounded rectangle)
  • DrawEllipse
  • DrawLine (With different line styles)
  • DrawString (With various paragraph alignment options – Left,Right,Justify,Center)

The PDF is drawn on a GDI+ drawing surface, in this case a GDI bitmap the size of an A4 document. There is a basic example within the test code that creates the following PDF file –

Don’t Forget, you’ll need to replace the line where it loads in and draws the image, or the TestDocument function will fail.

You can get PDFSharp from here, and of course download the example below as normal. It’s well commented so you should be able to get an idea of how to use it. The source code of PDFSharp has a pretty comprehensive array of test projects to pick at.

I’m still waiting for our subscription copy of max 2010, so I’ll probably add a version later using the private/public declarations available in this release.

If anyone is interested, the text on the PDF is from the rather beautiful short story ‘The Man Who Planted Trees‘ by Jean Giono.

download script

404