【问题标题】:print a file in java在java中打印文件
【发布时间】:2013-10-29 10:32:58
【问题描述】:
fis = new FileInputStream(file);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE; //  FILE IS .txt TYPE
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
//pras.add(MediaSizeName.ISO_A4);
//pras.add(new Copies(1));

PrintService printService[] =PrintServiceLookup.lookupPrintServices(flavor, pras);
System.out.println("Print Service:"+printService);

PrintService defaultService =PrintServiceLookup.lookupDefaultPrintService();
System.out.println("Default Service:"+defaultService);
PrintService service = ServiceUI.printDialog(null, 200, 200,printService, defaultService, flavor, pras);

if (service != null) 
{
    System.out.println("Selected Service"+service);
    DocPrintJob job = service.createPrintJob();
    job.addPrintJobListener(new MyPrintJobListener());

    System.out.println("JOB:"+job);
    DocAttributeSet das = new HashDocAttributeSet();
    Doc doc = new SimpleDoc(fis, flavor, das);

    System.out.println("Start of Print");
    job.print(doc, pras);
    System.out.println("End of Print");
    i=1;
}
else
{
    i=0;
}

我正在开发一个 Web 应用程序,用户希望打印保存在文件中的数据,该文件包含根据他的查询来自数据库的详细信息。我将上面的代码放在一个方法中,并在调用它时为用户提供了一个打印对话框,他可以在其中从连接到他的机器的打印机列表中选择打印机。 如果我尝试打印到本地打印机,文件将被发送到 C:\WINDOWS\system32\spool 文件夹并进行打印。 但在网络打印机的情况下不会发生 我在网络上的打印机是佳能 MP280 系列打印机 我可以在打印机列表中看到它,但无法打印我的文件

【问题讨论】:

    标签: java jakarta-ee printing network-printers


    【解决方案1】:

    网络上的打印机是否共享? 如果打印机是共享的并且在您的电脑/笔记本电脑中已安装。 您必须选择打印服务名称列表数组之一

    例如

    PrintService defaultPrintservice = printServices[0];
    

    如果打印机没有安装在您的PC/笔记本电脑中,您必须设置位置网络打印机的路径

    例如

    new PrinterName("ipp:\\\\witnw21va\\ipp\\ITDepartment-HP4050", null);
    

    我希望它能解决你的问题 :) 对不起我的英语 :D

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多