2

所以我为我爸爸写了一个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;
}
4

2 回答 2

2

您应该可以通过设置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);
}
于 2013-02-08T03:43:51.643 回答
1

所以我听从了 VGR 的建议,我得到了它的工作。这是我的最终代码,以防有人遇到同样的问题:

Date data = new Date();                                         //Data
DateFormat dataform = new SimpleDateFormat("dd-MM-yyyy");       //Data

PrintService service=getPrinterService("Microsoft XPS Document Writer");
if(service!=null){
    try{
        File outputFile = new File(dataform.format(data)+"-Recibo"+nmrRec+".xps");

        Doc doc = new SimpleDoc(new myReceipt(), DocFlavor.SERVICE_FORMATTED.PRINTABLE, null);

        PrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
        attributes.add(new Destination(outputFile.toURI()));

        DocPrintJob job = service.createPrintJob();
        job.print(doc, attributes);
    } catch(Exception e){
        System.out.println("kaboom"+e);
    }
}
else{
    System.out.println("XPS Printer not found");
}

还有我的收据类:

class myReceipt implements Printable{

    @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;
        }

        /* User (0,0) is typically outside the imageable area, so we must
         * translate by the X and Y values in the PageFormat to avoid clipping
         */
        Graphics2D g2d = (Graphics2D)g;
        g2d.translate(pf.getImageableX(), pf.getImageableY());
        int lineSize=20;

        Font testFont=new Font("Lucida Console", Font.BOLD, 20);
        // font name, style (0 for Plain), font size
        g.setFont(testFont);
        int line=20;

        g.drawString("        Fatura/Recibo nº"+nmrRec+"      ", 5, line);
        return PAGE_EXISTS;
    }
}
于 2013-02-09T16:55:02.897 回答