使用java打印html文件而不向用户显示打印对话框

使用java打印html文件而不向用户显示打印对话框,java,swing,printing,Java,Swing,Printing,我正在尝试将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[

我正在尝试将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\u STREAM.PNG
DocFlavor=DocFlavor.INPUT\u流

请建议此处使用哪种格式

请建议如何避免在运行此代码时出现打印对话框

提前谢谢 桑迪


您可以只使用默认服务,而不用在对话框中显示。

谢谢您的回复。。如果我需要将打印机名称作为参数传递,而不是使用默认参数,该怎么办。。请建议如何使用特定的打印机名称?-您有PrintService PrintService[]=PrintServiceLookup.lookupPrintServices(flavor,pras);浏览数组并将所需名称与每个PrintService的名称进行比较,直到找到所需名称。感谢您的宝贵建议,我们将尝试使用相同的名称并返回给您。。关于打印html而不是png,我们在
DocFlavor flavor=DocFlavor.INPUT_STREAM.png
中应该使用什么格式,请建议使用PrintService数组查找特定的打印机。。谢谢你。。您还可以建议如何使用DocFlavor打印html吗?
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
//    PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
if (service != null) {
     ... the rest code