2012-03-16 252 views
1

我使用java打印多個html文檔時遇到問題。我需要的應用程序顯示ONE所有可打印文件的打印對話框(文件數可能很大)。首先,我試圖做到這一點使用非標準的Java方法:在java中批量打印

if (Desktop.isDesktopSupported()){ 
     Desktop desktop = Desktop.getDesktop(); 
     if (desktop.isSupported(Desktop.Action.PRINT)) 
     { 
      try { 
       File html1 = new File("c://file1.html"); 
       File html2 = new File("c://file2.html"); 
       desktop.print(html1); 
       desktop.print(html2); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
} 

但我看到每個打印的文件一個一個對話框,它不適合我。然後我試圖使用Java打印API,但事實證明,我的打印機不支持DocFlavor的HTML文件,我的支持列表DocFlavor的是這樣的:

image/gif; class="[B" 
image/gif; class="java.io.InputStream" 
image/gif; class="java.net.URL" 
image/jpeg; class="[B" 
image/jpeg; class="java.io.InputStream" 
image/jpeg; class="java.net.URL" 
image/png; class="[B" 
image/png; class="java.io.InputStream" 
image/png; class="java.net.URL" 
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable" 
application/x-java-jvm-local-objectref; class="java.awt.print.Printable" 
application/octet-stream; class="[B" 
application/octet-stream; class="java.net.URL" 
application/octet-stream; class="java.io.InputStream" 

然後我試圖打印HTML文件作爲圖像(PNG,這是我在畫圖:)畫了),我的代碼:

PrintRequestAttributeSet pras = 
        new HashPrintRequestAttributeSet(); 
      DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG; 
      PrintRequestAttributeSet aset = 
        new HashPrintRequestAttributeSet(); 
      aset.add(MediaSizeName.ISO_A4); 
      aset.add(new Copies(1)); 
      aset.add(Sides.ONE_SIDED); 
      aset.add(Finishings.STAPLE); 

      PrintService printService[] = 
        PrintServiceLookup.lookupPrintServices(flavor, pras); 
      PrintService defaultService = 
        PrintServiceLookup.lookupDefaultPrintService(); 
      PrintService service = ServiceUI.printDialog(null, 200, 200, 
        printService, defaultService, flavor, pras); 
      if (service != null) { 
       try { 
        FileInputStream fis = new FileInputStream("c://test//test.png"); 
        DocAttributeSet das = new HashDocAttributeSet(); 
        Doc doc1 = new SimpleDoc(fis, flavor, das); 

        FileInputStream fis2 = new FileInputStream("c://test//test2.png"); 
        DocAttributeSet das2 = new HashDocAttributeSet(); 
        Doc doc2 = new SimpleDoc(fis2, flavor, das2); 

        DocPrintJob job1 = service.createPrintJob(); 
        DocPrintJob job2 = service.createPrintJob(); 

        try { 
         job1.print(doc1, pras); 
         job2.print(doc2, pras); 
        } catch (PrintException e) { 
         e.printStackTrace(); 
        } 
       } catch (FileNotFoundException e) { 
        e.printStackTrace(); 
       } 
      } 

它完美,而是convertation從HTML圖像並不簡單的問題。我嘗試使用swing組件,實現Printable接口並使用Cobra庫,但它要求在窗體上顯示文檔,這對我來說並不是必需的,因爲我需要在「無聲」模式下打印,而無需打開文檔。

任何想法?

回答

0

最後,我選擇這種方式:

  1. 轉換HTML文件爲PDF。

  2. 以無聲打印模式使用PDFBox打印pdf文件。


List<PDDocument> docs = new ArrayList<PDDocument>(); 
try { 
    docs.add(PDDocument.load("c://test/test.pdf")); 
    docs.add(PDDocument.load("c://test/test2.pdf")); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

try { 
    PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); 
    DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; 
    PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet(); 
    aset.add(MediaSizeName.ISO_A4); 
    aset.add(new Copies(1)); 
    aset.add(Sides.ONE_SIDED); 
    aset.add(Finishings.STAPLE); 

    PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras); 
    PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService(); 
    PrintService service = ServiceUI.printDialog(null, 200, 200, 
      printService, defaultService, flavor, pras); 

    if (service != null && !docs.isEmpty()) { 
     for (PDDocument doc : docs) { 
      PrinterJob printJob = PrinterJob.getPrinterJob(); 
      printJob.setPrintService(service); 
      doc.silentPrint(printJob); 
     } 
    } 
} catch (PrinterException e) { 
    e.printStackTrace(); 
} finally { 
    for (PDDocument doc : docs) { 
     if (doc != null) { 
      try { 
       doc.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
} 
+0

在這個例子中,ASET是未使用的。它看起來像pdfbox 2.0.0將支持將PrintRequestAttributeSet傳遞給'silentPrint()'方法。 – Aaron 2014-06-19 23:24:44