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

时间:2014-07-14 08:35:05

标签: java printing jcombobox aspose

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

通过使用JComboBox和PrintService类进行设置,我使用PrintServiceLookup.lookupPrintServices获取计算机上已安装打印机的列表,并使用其结果填充组合框。要执行查找和替换,并发送到打印机我使用Aspose.Words。要使用Aspose将文档发送到打印机,只需Document.print(“打印机名称”)。这就像传递组合框“getSelectedItem.toString();”一样简单。到Document.print函数,但我的问题是程序严重依赖的联网打印机。

简而言之,如果网络打印机位于服务器UKTESTSERVER01上,则将其指定为“UKTESTSERV01上的HQ打印机”,但PrintServiceLookup中返回的值为“\ UKTESTSERV01 \ HQ Printer”。传入打印功能时,它不会被识别为Microsoft字打印机,因此会打印为默认值。

我正在寻找另一种解决方案,可能使用另一种打印类,或者可能是列出Microsoft字打印机的类。任何帮助是极大的赞赏!

由于 罗斯。

代码在下面,我已经留下了很多,只包括相关区域。

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 个答案:

答案 0 :(得分:0)

您可以使用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;
    }
}

PS。我是Aspose的开发人员。