I'm trying to use the javafx Printer methods to print a borderless photo. I've tried using the older awt Printer methods with no luck either.
My issue is that the printer doesn't pick up the borderless paper option e.g. 'Borderless 4x6in.', and instead the print job on Windows appears with '4x6in.' as the selected paper.
Here's my print function:
private boolean print(Paper paper, Printer printer, BufferedImage image) {
Image fxImage = SwingFXUtils.toFXImage(image, null);
ImageView ivImage = new ImageView(fxImage);
PageLayout pageLayout = printer.createPageLayout(paper, PageOrientation.LANDSCAPE, Printer.MarginType.HARDWARE_MINIMUM);
double scaleX = pageLayout.getPrintableWidth() / fxImage.getWidth();
double scaleY = pageLayout.getPrintableHeight() / fxImage.getHeight();
ivImage.getTransforms().add(new Scale(scaleX, scaleY));
javafx.print.PrinterJob job = javafx.print.PrinterJob.createPrinterJob(printer);
job.getJobSettings().setPageLayout(pageLayout);
job.getJobSettings().setJobName(paper.getName());
System.out.println("PageLayout: " + pageLayout.toString());
boolean printed = job.printPage(ivImage);
if (printed)
{
job.endJob();
return true;
}
else
{
return false;
}
}
And here's the output of the println:
PageLayout: Paper=Paper: Borderless 4x6in. size=101.6x152.4 MM Orient=LANDSCAPE leftMargin=0.0 rightMargin=0.0 topMargin=0.0 bottomMargin=0.0
Which all looks good, but when it gets to the print queue in Windows, these are its properties: Print job properties
As you can see the borderless box isn't checked and the resulting print has a ~5mm border on the top and right sides (not on bottom or left interestingly). Are there any gotchas I've missed? My printer can print borderless fine from the Windows photoviewer..
I'm selecting the paper from the printer.getPrinterAttributes().getSupportedPapers()
set, so it definitely supports the size!
Any suggestions appreciated (though ideally don't want to back to java.awt)
Cheers!
So after a fair amount of frustration, someone suggested I just use a Windows call to the printer. This worked first time and was very simple! The only limitation is it prints with the default settings of the printer, which means you have to set them beforehand - not a massive issue for my use case. The resulting print function was this:
private boolean print(Printer printer, String imagePath) {
Process p;
try {
String execString = String.format("rundll32 C:\\WINDOWS\\system32\\shimgvw.dll ImageView_PrintTo \"%s\" \"%s\"",imagePath,printer.getName());
p = Runtime.getRuntime().exec(execString);
p.waitFor();
if (p.exitValue() == 0)
{
// Success
return true;
}
else
{
// Write Error Message
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (InterruptedException e) {
e.printStackTrace();
return false;
}
}