Place image in PDF
Today we look into drawing a picture into a PDF document. If you develop in Xojo, you may first look into PDFDocument class. There you can use DrawPicture and pass a picture to draw. This stores the image inside the PDF and shows it on a page.
There are different things to consider when placing an image:
- Does the image come from picture or from file or data in memory?
- What resolution does the image has and what resolution should it have in the PDF document?
- Does the image gets compressed into the PDF?
- What colorspace has the image and in what colorspace should it be stored in the PDF?
Using PDFDocument class
In Xojo's PDFDocument class, we can draw a picture into the PDF page. We scale down the image by passing a rectangle for the output area and the source area in the picture. This will add the picture to the PDF.
This will create a PDF with the full resolution of the given image. The resulting PDF file is over 22 MB big.
Using DynaPDFMBS class
Let's do the same with our graphics class integration in MBS Xojo DynaPDF Plugin. There you can use DrawPicture method to draw the picture on the PDF page. We have plenty of other methods to insert images.
The DrawPicture with a picture object will insert a RGB image.
By default DynaPDF will scale down images to 150 dpi if needed. This avoids bloated PDF files by default. If you need a higher maximum resolution, you can call SetResolution() with the desired number. Like 600 dpi for later printing on a laser printer.
If you like to change the color space, you can do that with DynaPDF using SetColorspace() function. Or change how images get compressed with SetCompressionFilter() to specify whether you like JPEG or Flate compression.
Results
For the test we use a JPEG file with 4 MB. The resulting PDF sizes are:
| PDFDocument | 22.2 MB |
| DynaPDF with 150 dpi | 224 KB |
| DynaPDF with 600 dpi | 1.8 MB |
As you see you have more control for compression with our MBS Xojo DynaPDF Plugin. You can get a license for DynaPDF Starter with OmegaBundle.