我正在尝试打印我的程序生成的HTML文件,但它不起作用.在Ubuntu上,".isSupported(Desktop.Action.PRINT)"返回false,即使我安装了gnome库,并且在Windows 7上,java抛出以下异常:
java.io.IOException: Failed to print file:/C:/Users/user/Documents/document.html. Error message: Unspecified error
Run Code Online (Sandbox Code Playgroud)
然后是堆栈跟踪.下面是代码,我正在使用java.awt.Desktop.
File doc = DocumentComposer.writeDocument(new File(System.getProperty("user.dir") + File.separator + "docs" + File.separator + docName + ".html"), case, data);
if (Desktop.isDesktopSupported())
{
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT))
{
desktop.print(doc);
}
else
printError();
}
else
printError();
Run Code Online (Sandbox Code Playgroud)
任何形式的帮助将非常感激:).
我最终没有使用java.awt.Desktop,它根本行不通.相反,我按照IBM教程http://www.ibm.com/developerworks/java/library/j-mer0322/中的说明进行操作.确切地说,我现在使用的代码如下(它在Linux和Windows上运行完美!):
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
PrintService service = ServiceUI.printDialog(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration(), 200, 200,
printService, defaultService, flavor, pras);
if (service != null) {
DocPrintJob job = service.createPrintJob();
FileInputStream fis = new FileInputStream(doc);
DocAttributeSet das = new HashDocAttributeSet();
Doc document = new SimpleDoc(fis, flavor, das);
job.print(document, pras);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12156 次 |
| 最近记录: |