Java PDF Blog

Java and PDF development - our personal experiences and discoveries

Download JPedal

Download JPedal PDF viewers

PDF to Image service

Try our PDF to image conversion service now.

Java PDF Ebook Solution

Try our Ebook solution now.

Subscribe

Your email:

Java PDF blog

Current Articles | RSS Feed RSS Feed

PDF to image quality

Posted by Mark Stephens on Thu, Jul 23, 2009 @ 04:48 AM
Submit to Digg digg it | Submit to Reddit reddit | Add to delicious delicious | Submit to StumbleUpon StumbleUpon 

The PDF file format was designed as much as possible to be Vector graphics. The problem with bitmaps and pixels is that pixels can only be on or off. While you can use some clever tricks such as anti-aliasing and hinting to smooth lines, you can't draw fractions of a pixel. If a line is half a pixel wide, you need to make it either zero or one pixel wide...

In principle, this means that as soon as you convert a PDF to an image, you can lose detail. This article describes one possible answer.

The solution we have adopted in our JPedal software is to provide an example to allow the user to draw the PDF onto a much bigger image (providing more pixels) which can then be scaled down or adjusted to take account of various dpis. You can get the details of the example here and the example source code here.

There are a number of possible scenarios so we added some flags to allow the user to choose how to improve the image quality.

Scenario One - Bigger initial image

We added a flag EXTRACT_AT_PAGE_SIZE which takes a set of values

//alternatively secify a page size (aspect ratio preserved so will do best fit)
//set a page size (JPedal will put best fit to this)
mapValues.put(JPedalSettings.EXTRACT_AT_PAGE_SIZE, new String[]{"2000","1500"});

JPedal will attempt to scale up the page to fit into this box (so a PDF page of dimensions 1000x750 would be scaled up by a factor of 2, which a page which is 1000x1000 would be scaled up by a factor of 1.5 to 1500x1500. 

If you wanted to optimise a page for 96 dpi rather than 72 dpi (ie 1.33333 times more detail), you could get the page size (its in the PdfPageData class in JPedal) multiple the dimensions by 1.333 and then use this method to produce an image. If saving as a jpeg, you can then alter the dpi settings and it will display as intended at 96 dpi.

If you wanted the highest possible image, you could create a larger version (providing the maximum number of physical pixels) and then bicubically rescale to desired size.

All these processes are slower and use more memory but give a higher quality result - like so much in life, there is always a trade-off.

Scenario Two - Use the embedded images

PDFs contain raw images, which are often scaled down when drawn onto the PDF and detail is lost. So we thought, rather than scale down the image to fit the PDF, why not scale up the PDF to fit the image!

This worked very well until someone found a PDF with a image which was actually scaled down 47 times. When we use this file, the amount of memory required to create the page was way beyond the capabilities of any current machine....

So we add a limit as to how much the page could be scaled up.

//do not scale above this figure    mapValues.put(JPedalSettings.EXTRACT_AT_BEST_QUALITY_MAXSCALING, new Integer(2));

So here we allow the page to scale up to twice its size. If the raw image was bigger than that, it will be scaled down, but the results should still be better. 

Lastly we need a way to choose which of these 2 rules takes priority, so we added a flag

//which takes priority (default is false)
mapValues.put(JPedalSettings.PAGE_SIZE_OVERRIDES_IMAGE, Boolean.TRUE);

So we now have a flexible way to generate higher quality images and the user can choose the best tradeoffs for them in terms of speed, memory, quality, etc.

 

Tags: 

COMMENTS

I use jpedal to convert PDF images stored as longblob fields and in some cases I´m losing the first line (1 pixel), so that an image with 1pixel wide border lose the top border. 
How can I fix it?

posted @ Thursday, August 06, 2009 1:09 PM by Alexandre


Post Comment
Name
 *
Email
 *
Website (optional)
Comment
 *

Allowed tags: <a> link, <b> bold, <i> italics

Receive email when someone replies.