gpt4 book ai didi

JavaFX PrintAPI 错误的 PaperSource

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:31:53 26 4
gpt4 key购买 nike

我正在使用 JavaFx Print-Dialog 来自定义打印作业。所有属性都将存储在 PrinterJob#JobSettings 变量中,但是当我从 jobSetting 收到纸张来源时,纸张来源始终是默认值。

如何获取我设置的纸张来源?

这是一个简短的例子:

public class PrinterPaperSourceTest extends Application {
public static void main(String[] args) {
launch( args );
}

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Printer");
Button btn = new Button();
btn.setText("Show Printer Settings ");
btn.setOnAction( new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
PrinterJob job = PrinterJob.createPrinterJob(Printer.getDefaultPrinter());
job.showPageSetupDialog(null);
Alert alert = new Alert(AlertType.INFORMATION);
PaperSource paperSource = job.getJobSettings().getPaperSource();
alert.setContentText("PaperSource: " + paperSource.getName());
alert.show();
}
});

StackPane root = new StackPane();
root.getChildren().add(btn);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
}
}

最佳答案

打印 API 出现在 fx8.0 中。它可以打印节点。您可以使用 javafx.print.PrinterJob 类创建打印机作业。但它只打印适合打印页面的区域,而不是屏幕上的区域。因此,您需要手动使您的节点适合页面(缩放、翻译等)。

您可以使用此代码。希望对您有所帮助。

/**
* Prints the current page displayed within the internal browser, not necessarily the {@link #presentationProperty()}.
* If no printers are installed on the system, an awareness is displayed.
*/
public final void print() {
final PrinterJob job = PrinterJob.createPrinterJob();

if (job != null) {
if (job.showPrintDialog(null)) {
if(this.getPresentation().getArchive() != null) {
final String extension = ".".concat(this.getPresentation().getArchiveExtension());
final int indexOfExtension = this.getPresentation().getArchive().getName().indexOf(extension);
final String jobName = this.getPresentation().getArchive().getName().substring(0, indexOfExtension);
job.getJobSettings().setJobName(jobName);
}

job.getJobSettings().setPrintQuality(PrintQuality.HIGH);
job.getJobSettings().setPageLayout(job.getPrinter().createPageLayout(Paper.A4, PageOrientation.LANDSCAPE, 0, 0, 0, 0));

this.internalBrowser.getEngine().print(job);
job.endJob();
} else {
job.cancelJob();
}
} else {
DialogHelper.showError("No printer", "There is no printer installed on your system.");
}
}

资源链接:

  1. javafx.print.PrinterJob examples
  2. Introduction by Example: JavaFX 8 Printing

关于JavaFX PrintAPI 错误的 PaperSource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37791163/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com