Skip to content

PDFDocument: No More Blurry Images

Among the major enhancements made to the images support in PDFDocument in Xojo 2021r3 release, in addition to transparency support, alpha channel support, and the new way the data is codified in the resulting PDF stream (no more DCTEncoding), we’ve also tackled an issue with images not being as sharp as they should have been.

With Xojo 2021r3 we are handling the images added to PDFDocument in a different way. When images are added to the page using a smaller size than the original size, which is probably more common, using something like:

Var d As New PDFDocument
var g as Graphics = d.Graphics

g.DrawPicture myImage, 10, 10, 100, 100, 0, 0, myImage.Width, myImage.Height


g.DrawPicture myImage, 10, 10, myImage.Width/3, myImage.Width/3, 0, 0, myImage.Width, myImage.Height

“myImage” being an image added to the Xojo project, loaded from disk or from a database query.

You can appreciate how the quality has improved from left to right, below.

Also, the new way the image PDF streams are created in order to get this sharpness means that all the data of the source image is added to the PDF document. In other words, it will increase its size. Comparing the same document, the one created with Xojo 2021r2.1 is 521 KB, and the one created with Xojo 2021r3 is 3.1 MB. This is because adding the full size of the the image takes a total of 950 KB, plus support for alpha and masks…

But the good side of the extra document size is that, because images are embedded at their full resolution, they will keep looking sharp even when the PDF document is displayed beyond 100% its original scale. You can see in the PDF document below created with Xojo 2021r2.1:

And the PDF document created with Xojo 2021r3:

Document Size vs Image Quality

The winner of this battle depends what do you want to offer to your users. Using our example document we have the following figures:

  • PDF document resolution (w x h): 612 x 792 pixels
  • The apple image resolution (w x h): 607 x 576 pixels

As you can see, the apple image has nearly the same resolution that the full page of the PDF Document! But it is rendered at a final size of 100 x 100 pixels. That means that the image will keep sharper even if we use a zoom value of 800% in the viewer app, but that has a cost in the size of the generated PDF document.

You’ll probably want to keep the images added to the PDFDocument pages at a maximum of twice the final width and height. That will produce good looking images in your PDF documents while keeping the document to a reasonable size. Learn more about PDFDocument and what’s new in the Xojo Documentation.