2013-02-08 2 views
1

없는 그래서 영수증과 물건을 인쇄하는 아빠를위한 자바 프로그램을 작성 메신저. 내 원래의 의도는 자신의 영수증 프린터에 그가 만든 각 거래에 관한 정보를 인쇄하는 것이 었습니다. 그러나 프린터는 내가 보낸 것을 극한 지점까지 클리핑하지 않고 인쇄하는 데 약간의 문제가 있습니다. 아주 잘했다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를 얻었습니다. 입력 스트림 대신 해당 그래픽 객체로 "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; 
    } 
} 
관련 문제