簡體   English   中英

使用Java打印html文件而不向用戶顯示打印對話框

[英]Printing an html file using java without showing print dialog to the user

我正在嘗試將html文件直接打印到默認打印機,而不向用戶顯示打印對話框。

我只是從一些在線教程中獲得了以下代碼,它對於PNG文件非常有效。

import javax.print.*;
import javax.print.attribute.*;
import java.io.*;

public class Printing {
  public static void main(String args[]) throws Exception {
    String filename = args[0];
    PrintRequestAttributeSet pras = 
      new HashPrintRequestAttributeSet();
    DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
    PrintService printService[] = 
      PrintServiceLookup.lookupPrintServices(flavor, pras);
    PrintService defaultService = 
      PrintServiceLookup.lookupDefaultPrintService();
    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
    if (service != null) {
      DocPrintJob job = service.createPrintJob();
      FileInputStream fis = new FileInputStream(filename);
      DocAttributeSet das = new HashDocAttributeSet();
      Doc doc = new SimpleDoc(fis, flavor, das);
      job.print(doc, pras);
      Thread.sleep(10000);
    }
    System.exit(0);
  }
}

我想更改DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG; DocFlavor flavor = DocFlavor.INPUT_STREAM.<some html format>;

請建議在這里使用哪種格式合適?

並建議在運行此代碼時如何避免彈出打印對話框。

在此先感謝桑迪

PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
     ... the rest code

您可以只使用默認服務,而不是從對話框中顯示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM