我想使用“Microsoft Print to PDF”打印机将节点打印到 pdf 文件。假设 Printer 对象已经被提取,我有下一个功能,它可以完美地工作。
public static void printToPDF(Printer printer, Node node) {
PrinterJob job = PrinterJob.createPrinterJob(printer);
if (job != null) {
job.getJobSettings().setPrintQuality(PrintQuality.HIGH);
PageLayout pageLayout = job.getPrinter().createPageLayout(Paper.A4, PageOrientation.PORTRAIT,
Printer.MarginType.HARDWARE_MINIMUM);
boolean printed = job.printPage(pageLayout, node);
if (printed) {
job.endJob();
} else {
System.out.println("Printing failed.");
}
} else {
System.out.println("Could not create a printer job.");
}
}
我遇到的唯一问题是弹出一个对话框,询问保存 pdf 的目标路径。我正在努力寻找以编程方式设置路径的解决方案,但没有成功。有什么建议吗?预先感谢您。
经过更多研究,我得到了一个丑陋的黑客。我从 PrinterJob 访问了 jobImpl 私有字段,并从中取出了属性。因此,我插入了目标属性,显然它正在按要求工作。我知道这不太好,但是……是可行的。如果您有更好的建议,欢迎留言。
try {
java.lang.reflect.Field field = job.getClass().getDeclaredField("jobImpl");
field.setAccessible(true);
PrinterJobImpl jobImpl = (PrinterJobImpl) field.get(job);
field.setAccessible(false);
field = jobImpl.getClass().getDeclaredField("printReqAttrSet");
field.setAccessible(true);
PrintRequestAttributeSet printReqAttrSet = (PrintRequestAttributeSet) field.get(jobImpl);
field.setAccessible(false);
printReqAttrSet.add(new Destination(new java.net.URI("file:/C:/deleteMe/wtv.pdf")));
} catch (Exception e) {
System.err.println(e);
}
感谢上述解决方案。这是没有 *Impl 实例的同类,它的工作方式为 Object xxx = ... :
ObservableSet<Printer> printers = Printer.getAllPrinters();
if (printers != null && !printers.isEmpty())
{
Printer defaultPrinter = printers.iterator().next();
PrinterJob printerJob = PrinterJob.createPrinterJob(defaultPrinter);
if(printerJob != null)
{
// printerJob.showPrintDialog(thisState);
String selectedPrinter = printerJob.getPrinter().getName();
PageLayout pageLayout
= defaultPrinter.createPageLayout(Paper.A4, PageOrientation.PORTRAIT, Printer.MarginType.HARDWARE_MINIMUM);
PrinterAttributes attr = defaultPrinter.getPrinterAttributes();
// HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
// set the output file as a destination
File currDir = new File(".");
File destinationFile = new File(currDir.getAbsolutePath()
+File.separatorChar +"output.ps");
BasicFileAttributes fileAttr = null;
Path filePath = Paths.get(destinationFile.getAbsolutePath());
fileAttr = getFileAttributes(filePath);
Destination destination = new Destination(destinationFile.toURI());
// attributes.add(destination);
// set the attributes to the printerjob
JobSettings jobSettings = printerJob.getJobSettings();
jobSettings.setPrintQuality(PrintQuality.HIGH);
// PrinterAttributes defAttributes = defaultPrinter.getPrinterAttributes();
try {
java.lang.reflect.Field field = printerJob.getClass().getDeclaredField("jobImpl");
field.setAccessible(true);
Object jobImpl = field.get(printerJob);
field.setAccessible(false);
field = jobImpl.getClass().getDeclaredField("printReqAttrSet");
field.setAccessible(true);
PrintRequestAttributeSet printReqAttrSet = (PrintRequestAttributeSet) field.get(jobImpl);
field.setAccessible(false);
printReqAttrSet.add(destination);
BasicFileAttributes fileAttrAfter = getFileAttributes(filePath);
if (fileAttr != null && fileAttrAfter != null)
{
if (fileAttrAfter.equals(fileAttr))
{
labelMsg.setText("Printing into " +destinationFile.getName() +
" successfully. Now generated .pdf file!");
generatingPdfFileAfterPsFile(destinationFile);
}
}
} catch (Exception e) {
System.err.println(e);
labelMsg.setText("Error destination in print: " +e.getMessage());
return;
}
// add(new Destination(new File(currDir.getAbsolutePath() +File.separatorChar +"output.gs").toURI()));
// MyPrintable will have to be defined to implement the print method
// pjob.setPrintable(new MyPrintable(), new PageFormat())
try {
WebEngine webEngine = webView.getEngine();
webEngine.print(printerJob);
printerJob.endJob();
} catch (Exception ex) {
System.out.println(ex);
labelMsg.setText("Error in printing: " +ex.getMessage());
}