所以我为我爸爸编写了一个java程序来打印收据之类的东西。我的初衷是向他的收据打印机打印一些有关他所做的每笔交易的信息。但是,打印机在打印我发送的内容时遇到一些问题,而没有将其剪切到极点。
我的下一个想法,效果很好,是将“收据”保存到 XPS 文件中,然后打印 XPS,这样就不会剪辑它,并且会使一切变得更好。现在,我可以使用 Microsoft 的 XPS Document Writer PrintService 打印到 XPS 文件。问题是,当我这样做时,它总是弹出一个框,询问文件名和保存位置。
是否有办法将其设置为根本不显示该弹出窗口?
当前代码:
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
try {
job.print();
} catch (PrinterException ex) {
// The job did not successfully complete
}
-
@Override
public int print(Graphics g, PageFormat pf, int page) throws PrinterException {
String temp;
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
int lineSize=20;
Font testFont=new Font("Lucida Console", 0, 20);
g.setFont(testFont);
g.drawString(" Fatura/Recibo nº"+nmrRec+" ", 5, 20);
return PAGE_EXISTS;
}
最佳答案
您应该可以通过设置 Destination 来做到这一点属性:
static void print(Printable printable, PrintService service)
throws PrintException,
IOException {
Path outputFile = Files.createTempFile(
Paths.get(System.getProperty("user.home")), null, ".xps");
Doc doc = new SimpleDoc(printable,
DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);
PrintRequestAttributeSet attributes =
new HashPrintRequestAttributeSet();
attributes.add(new Destination(outputFile.toUri()));
DocPrintJob job = service.createPrintJob();
job.print(doc, attributes);
}
关于JAVA - 打印 XPS 时不弹出文件名/位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14765105/