i am stuck currently when printing a jpeg file with the default printer. In my program when i select an image from a folder, i need to print it using the printer default settings (paper size, margins, orientation).
Currently i got this:
PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
final BufferedImage image = ImageIO.read(new File("car.jpg"));
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(printService);
printJob.setPrintable(new Printable(){
@Override
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException{
if (pageIndex == 0) {
graphics.drawImage(image, 0, 0, (int)pageFormat.getWidth(), (int)pageFormat.getHeight(), null);
return PAGE_EXISTS;
else return NO_SUCH_PAGE;
}
}
printJob.print();
The default settings for my printer right now for size is: 10 x 15 cm (4 x 6 in)
but when i set my program to print the given image, it displays only a small section of the paper.
Please help me out.
EDIT
thanks everyone for their help, i managed to find the answer posted by another user at Borderless printing
Best Answer
Make sure that you are, first, translating the
Graphics
context to fit within inthe imagable area...Next, make sure you are using the
imageableWidth
andimageableHeight
of thePageFormat
and not the
width
/height
properties. Many of these things get translated from different contexts...The
getImageableWidth/Height
returns the page size within the context of the page orientationPrinting pretty much assumes a dpi of 72 (don't stress, the printing API can handle much higher resolutions, but the core API assumes 72dpi)
This means that a page of 10x15cm should translate to
283.46456664x425.19684996
pixels. You can verify this information by using aSystem.out.println
and dumping the results ofgetImageableWidth/Height
to the console.If you're getting different settings, it's possible that Java has overridden the default page properties
For example...
You have two choices...
You could...
Show the
PrintDialog
and ensure that the correct page settings are selectedOr you could...
Just manually set the paper/page values manually...