JAVA - 在没有文件名/位置弹出的情况下打印XPS

时间:2013-02-08 03:13:53

标签: java printing xps

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

2 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分: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;
    }
}