Add SVG to PDF pages as vector graphics in Xojo
With the recent DynaPDF update 5.0.4, you can place SVG drawings on a page in a PDF document. By converting all SVG drawing commands into PDF drawing commands, the graphics is preserved as a vector graphics. This avoids any pixelation that a bitmap graphics would have.
You can load a SVG from file with LoadSVG methods in DynaPDFMBS class either from a file on disk or a buffer in memory. Then query the sizes and insert the graphics to the current open page in a PDF document with InsertSVG function. There you pass a rectangle and optional the name of the symbol.
When you insert a SVG into a PDF page, you can change the size of the drawing by passing a different width and height. You can pick the position where to place and apply transformations if needed. This allows you to rotate the drawing, mirror it or stretch it in one or both axes. You can use SVG drawings for templates and stamp annotations.
Some SVG files have multiple drawings, so you can use SymbolCount function in DynaPDFSVGMBS class to query the count. Then check the internal name of the drawing with SymbolName() function. Or use SymbolNames to get an array with all names. Once you have a name, you can pass it to InsertSVG for the symbol id parameter.
Here is a bit of sample code:
If something goes wrong while drawing a SVG, the error is logged. You can use GetErrLogMessage() to get the list of issues. That may be a font missing or an unsupported operation.
We did have GraphicsMagick classes to render a SVG to a bitmap picture and save it as PNG or JPEG. Using PNG allowed to preserve transparency.
Please try the example file included with MBS Xojo Plugins 26.4.
To load and insert a SVG, you need the Starter license for DynaPDF. In case you like to place the PDF on an existing PDF, you'd need the Lite license to import the PDF. And to render the PDF as bitmap picture like in our example, you would need the Pro license. see also DynaPDF Starter vs. Lite vs. Pro vs. Enterprise.