【发布时间】:2016-04-21 07:37:54
【问题描述】:
我正在尝试使用 JavaFX api 打印图像。不幸的是,它正在剪切图像的一部分,大约 25%,然后将其拉伸到整个 A4 页面并打印。我在打印代码上做错了什么。无论打印机是什么,我如何指示将图像适合打印页面。请告诉我。
代码:
public void printThis() {
System.out.println("I was called");
// note you can use overloaded forms of the Image constructor
// if you want to scale, etc
String path = "resources/img/printouts/image.png";
Image image = new Image(getClass().getResource(path).toExternalForm());
ImageView imageView = new ImageView(image);
new Thread(() -> printImage(imageView)).start();
}
public void printImage(ImageView image) {
Printer printer = Printer.getDefaultPrinter();
PrinterJob printJob = PrinterJob.createPrinterJob(printer);
PageLayout pageLayout = printJob.getJobSettings().getPageLayout();
//PageLayout pageLayout = printer.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.DEFAULT);
printJob.getJobSettings().setPageLayout(pageLayout);
if (printJob != null) {
boolean success = printJob.printPage(image);
if (success) {
printJob.endJob();
}
}
}
请让我知道我做错了什么。谢谢你。 :-)
【问题讨论】:
-
顺便说一句:
ifcheck 语句在那里没有做任何有用的事情:在上一行(成功)执行后条件始终为真。 -
而上一行什么也不做,鉴于它之前的行......
-
现在我在打印机旁,我测试了@fabian 发布的解决方案,它似乎确实有效。我不太清楚为什么,但也许有些关于打印分辨率的事情我不明白。
-
@james_d 是的.. 我花了大约一个小时试图解决这个问题......不幸的是没有运气......最后我明天将研究与位置无关的资源访问,即 2016 年 1 月 16 日。
-
我想你误会了。 @fabian 发布然后删除的解决方案(也许在我的误导性评论之后)工作得很好。只需执行
image.setPreserveRatio(true);、image.setFitWidth(pageLayout.getPrintableHeight());和image.setFitWidth(pageLayout.getPrintableWidth());。我会让他恢复他的答案。
标签: java image printing javafx image-scaling