Using PDFNet to resample / downscale images in PDF

Q:

We have a client who sends us pdfs, which include pages of images. The images have been known to be in strange dpi values. We’re processing this file using your PDFNet toolkit, and the original developer wrote this section of code to “adjust” the dpi of the image if it was “strange”:

width = image.GetImageWidth()

height = image.GetImageHeight()

mtx = image.GetCTM()

dpi = width/mtx.m_a * 72

if dpi != 300:

newWidth = width / 21600

mtx.m_a = newWidth

image.GetGState().SetTransform(mtx)

FYI, this is Python code. I do not know WHY the developer chose to use 21600, and I have no idea what that magic number represents. Today, we received the files and they were in a strange dpi (1200 – not all that strange, but pretty high). When we run this code, the images are horizontally scaled to 0 (which I would expect, given the code above).

Has there ever been any bug behavior in SetTransform regarding setting the horizontal scale to 0 that would explain the code within the if block?

More importantly, is there any kind of Element.Resample function I could use to resample an image to a smaller dpi? We eventually create postscript files for this print file, and I’m concerned about the file size getting out of hand – resampling these images could make a big difference in that regard.

A:

➢ … I have no idea what that magic number represents …

Likewise. In any case you can compute image DPI as described in the following article:

https://groups.google.com/d/topic/pdfnet-sdk/7v1cg3PBlgg/discussion

In case you need to optimize images you can simply use ‘pdftron.PDF.Optimizer’ as shown in the Optimizer sample:

http://www.pdftron.com/pdfnet/samplecode.html#Optimizer

This is much simpler and less error prone than dealing with PDF internals and low-level APIs. Please note that the use of Optimizer requires add-on permission (http://www.pdftron.com/store/pdfnet.html). If you already have a PDFNet license, it can be easily upgraded to include the extra permission.