我在使用java打印几个html文档时遇到了问题。我需要应用程序,为所有可打印文件显示一个打印对话框(文件计数可能很大)。首先,我尝试使用standart 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();
}
}
}但是我看到了每个可打印文件的一个对话框,它不适合我。然后我尝试使用,但事实证明我的打印机不支持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,我在画图中绘制),我的代码:
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组件,实现可打印接口和使用眼镜蛇库,但是它要求在表单上显示文档,这对我来说并不重要,因为我需要以“静默”模式打印,而不需要打开文档。
有什么想法吗?
发布于 2012-04-12 09:35:49
最后,我选择了这样的方式:
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();
}
}
}
}发布于 2012-03-16 12:08:37
在这里,您可以找到通过java代码访问打印机的完整代码。
它提供了如下功能
1. cancel print job,
2. display print dialog,
3. print file etc..http://anonsvn.icesoft.org//repo/icepdf/tags/icepdf-3.1.0/icepdf/viewer/src/org/icepdf/ri/common/PrintHelper.java
https://stackoverflow.com/questions/9736752
复制相似问题