2013-02-08 15 views
1

所以即時通訊編寫一個Java程序,爲我的父親打印出收據和東西。我的初衷是打印出他的收據打印機關於他所做的每筆交易的一些信息。但是,打印機在打印我發送的內容時會遇到一些麻煩,而不會將其限制在極端點。JAVA - 打印XPS沒有文件名/位置彈出

我的下一個想法很成功,那就是將「收據」保存到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

您應該能夠通過設置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); 
} 
+0

這個代碼是剛剛從「文件」進行讀取和寫入新的XPS文件在C:\ Users \ ,對不對?我如何畫東西?以前我重寫PrinterJob的打印方法(自其摘要以來),我從那裏得到了一個Graphics2D。我可以使用該Graphics對象而不是輸入流構造「doc」嗎?如果是這樣,我該如何初始化它? – BlueMoon93

+0

代替InputStream,實現'java.awt.print.Printable'並將該實現對象傳遞給SimpleDoc構造函數。我已經相應地更新了代碼。 – VGR

+0

工作過,非常感謝你=) – BlueMoon93

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