发布于2022-01-22 22:51 阅读(726) 评论(0) 点赞(24) 收藏(5)
我正在开发一个小应用程序来在 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,只支持图像。您也可以使用 OS 本机方法简单地打印 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();
}
}
}
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/377043/1a61cb2852550e3a48f4/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!