我正在开发一个小应用程序来在 Ubuntu 中打印数据,问题是我的应用程序在 Windows 中运行良好,使用:
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
FileInputStream fis = new FileInputStream(myfile);
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(fis, flavor, null);
job.print(doc, null);
fis.close();
但是在 Ubuntu 中,它只是不打印。对于我正在使用的打印 API,Linux 打印是否有任何特殊配置?还是我还漏掉了其他东西?
最佳答案
我认为您的打印机安装在操作系统中不是默认的。检查一下您的“服务”是什么。 您也可以从打印对话框中选择打印机,如下所示:
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.TEXT_PLAIN_UTF_8;
PrintRequestAttributeSet aset =
new HashPrintRequestAttributeSet();
aset.add(MediaSizeName.ISO_A4);
aset.add(new Copies(1));
aset.add(Sides.ONE_SIDED);
aset.add(Finishings.STAPLE);
PrintService printService[] =
PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(null, 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
try {
FileInputStream fis = new FileInputStream("c://test.txt");
DocAttributeSet das = new HashDocAttributeSet();
Doc doc1 = new SimpleDoc(fis, flavor, das);
DocPrintJob job1 = service.createPrintJob();
try {
job1.print(doc1, pras);
} catch (PrintException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
某些打印机不支持文本 DocFlavors,仅支持图像。您还可以使用操作系统 native 方法简单地打印 html 文件,如下所示:
if (Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT))
{
try {
File html1 = new File("c://file1.html");
desktop.print(html1);
desktop.print(html2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
关于Ubuntu 中的 Java 打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10107862/