【问题标题】:JAVA - Printing XPS without File Name/Location Pop-upJAVA - 在没有文件名/位置弹出窗口的情况下打印 XPS
【发布时间】:2013-01-23 18:40:43
【问题描述】:

所以我为我父亲编写了一个 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;
}

【问题讨论】:

    标签: java printing xps


    【解决方案1】:

    你应该可以通过设置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);
    }
    

    【讨论】:

    • 这段代码只是从“文件”读取并写入位于 C:\Users\ 的新 XPS 文件,对吗?我怎么画东西?以前我重写了 PrinterJob 的 print 方法(因为它是抽象的),我从那里得到了一个 Graphics2D。我可以用那个 Graphics 对象而不是输入流来构造“doc”吗?如果是这样,我如何初始化它?
    • 实现 java.awt.print.Printable 并将实现对象传递给 SimpleDoc 构造函数,而不是 InputStream。我已经相应地更新了代码。
    【解决方案2】:

    所以我听从了 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多