我在使用java打印几个html文档时遇到问题。我需要为所有可打印文件显示 ONE 打印对话框的应用程序(文件计数可能很大)。首先,我尝试使用标准的java方法:
if (Desktop.isDesktopSupported()){
Desktop desktop = Desktop.getDesktop();
if (desktop.isSupported(Desktop.Action.PRINT))
{
try {
File html1 = new File("c://file1.html");
File html2 = new File("c://file2.html");
desktop.print(html1);
desktop.print(html2);
} catch (IOException e) {
e.printStackTrace();
}
}
}
但是我看到每个可打印文件的一个对话框,它不适合我。然后我尝试使用Java Printing API,但事实证明我的打印机不支持html文件的DocFlavor,我支持的DocFlavor列表如下所示:
image/gif; class="[B"
image/gif; class="java.io.InputStream"
image/gif; class="java.net.URL"
image/jpeg; class="[B"
image/jpeg; class="java.io.InputStream"
image/jpeg; class="java.net.URL"
image/png; class="[B"
image/png; class="java.io.InputStream"
image/png; class="java.net.URL"
application/x-java-jvm-local-objectref; class="java.awt.print.Pageable"
application/x-java-jvm-local-objectref; class="java.awt.print.Printable"
application/octet-stream; class="[B"
application/octet-stream; class="java.net.URL"
application/octet-stream; class="java.io.InputStream"
然后我尝试将html文件打印为图像(png,我画了Paint :)),我的代码:
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.PNG;
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//test.png");
DocAttributeSet das = new HashDocAttributeSet();
Doc doc1 = new SimpleDoc(fis, flavor, das);
FileInputStream fis2 = new FileInputStream("c://test//test2.png");
DocAttributeSet das2 = new HashDocAttributeSet();
Doc doc2 = new SimpleDoc(fis2, flavor, das2);
DocPrintJob job1 = service.createPrintJob();
DocPrintJob job2 = service.createPrintJob();
try {
job1.print(doc1, pras);
job2.print(doc2, pras);
} catch (PrintException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
它完美无缺,但从html到图像的转换并不是一个简单的问题。我尝试使用swing组件,实现Printable接口并使用Cobra库,但它需要在表单上显示文档,这对我来说并不是必需的,因为我需要在“静音”模式下打印,而无需打开文档。
有什么想法吗?
答案 0 :(得分:0)
在这里,您可以找到通过Java代码访问打印机的完整代码。
它提供了像
这样的功能1. cancel print job,
2. display print dialog,
3. print file etc..
答案 1 :(得分:0)
最后,我选择这种方式:
将html文件转换为pdf。
在静音打印模式下使用PDFBox打印pdf文件。
List<PDDocument> docs = new ArrayList<PDDocument>();
try {
docs.add(PDDocument.load("c://test/test.pdf"));
docs.add(PDDocument.load("c://test/test2.pdf"));
} catch (IOException e) {
e.printStackTrace();
}
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
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 && !docs.isEmpty()) {
for (PDDocument doc : docs) {
PrinterJob printJob = PrinterJob.getPrinterJob();
printJob.setPrintService(service);
doc.silentPrint(printJob);
}
}
} catch (PrinterException e) {
e.printStackTrace();
} finally {
for (PDDocument doc : docs) {
if (doc != null) {
try {
doc.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}