Reputation: 20581
I have a List of java.awt.Image
and each of them has a resolution of 300 DPI. I want to print them and when I start to print this images (using javax.PrintService API), printed only piece of some piece of image, because Java's Print/3D classes by default use 72DPI (vs 300 DPI of my images). But when I use images with 72 DPI (with same resolution as the Java default) all images are printed fine (when printing whole images, not only piece of it).
Question: where I can set the printing resolution of my images to fit the printing area?
I tried to set PrintRequestAttributeSet.add( new PrinterResolution(300, 300, ResolutionSyntax.DPI))
but this has no effect.
For now, I scale my images to fit printing area, but after scaling my images I lose quality, so the printed document isn't readable.
Upvotes: 2
Views: 2552
Reputation: 415
You can use drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2, ImageObserver observer)
Parameters dx1, dy1, dx2 and dy2 define bounds that your image must fit into. You need to do a few calculations. This should print the high quality image on that coordinates (without scaling it down).
class MyPrintable implements Printable {
public int print(Graphics g, PageFormat pf, int pageIndex) {
if (pageIndex != 0) return NO_SUCH_PAGE;
Graphics2D g2 = (Graphics2D) g;
g2.printImage(....);
return PAGE_EXISTS;
}
}
Then
PrinterJob pj = PrinterJob.getPrinterJob();
...
PageFormat pf = ...;
...
pj.setPrintable(new MyPrintable(), pf);
You still need to set the resolution as you did before.
Upvotes: 7