有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java打印到打印机取决于选择

我正在开发一个Java应用程序,它从notes数据库中提取附件,对文档执行查找和替换,并根据程序设置中指定的打印机将其发送到打印机

设置通过使用JComboBox和PrintService类来工作,我使用PrintServiceLookup获取机器上已安装打印机的列表。lookupPrintServices并使用其结果填充组合框。执行查找和替换,并发送到正在使用Aspose的打印机。话。要使用Aspose将文档发送到打印机,它只是一个文档。打印(“打印机名称”)。这就像传递组合框“getSelectedItem.toString();”一样简单输入到文档中。打印功能,但我的问题是网络打印机的程序是严重依赖

换句话说,如果网络打印机位于服务器UKTEServer01上,但PrintServiceLookup中返回的值为“\UKTEServ01\HQ printer”,则网络打印机将被指定为“UKTEServ01上的HQ printer”。当传递到打印功能时,它不会被识别为Microsoft word打印机,因此会打印到默认值

我正在寻找另一个解决方案,可能使用另一个打印类,也可能是一个将列出MicrosoftWords打印机的类。非常感谢您的帮助

谢谢 罗斯

代码如下,我遗漏了很多,只包含了相关的区域

PrintService[] printServices = PrintServiceLookup.lookupPrintServices(
        null, null);
String[] printers = {"No Printer", "No Printer", "No Printer", 
        "No Printer", "No Printer", "No Printer", "No Printer",
        "No Printer","No Printer"};
int i = 0;

for (PrintService printer : printServices){
    printers[i] = printer.getName();
    i++;
}

final JComboBox printerCombo1 = new JComboBox(printers);
printerCombo1.setBounds(109, 11, 295, 20);
getContentPane().add(printerCombo1);

String specifiedPrinter = printerCombo1.getSelectedItem().toString();

document.print(specifiedPrinter);

共 (1) 个答案

  1. # 1 楼答案

    可以使用Document.print(AttributeSet)而不是Document.print(String)方法来指定打印机(选中API Reference。下面是代码示例

    String printerName = combobox.getSelectedItem().toString();
    for (PrintService p: PrintServiceLookup.lookupPrintServices(null, null)) {
        if (p.getName().equals(printerName)) {
            doc.print(p.getAttributes());
            break;
        }
    }
    

    另外,我是Aspose的开发者