高分请教---有谁知道怎样实现Java打印PDF,Excel,HTML等文件

blueyxr blueyxr 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
这是我的源码:


import java.io.FileInputStream;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;


public class PrintTestByCommand {

/**
* @param args
*/

public static void main(String args[]) throws Exception {
String filename = args[0];
PrintRequestAttributeSet pras =
new HashPrintRequestAttributeSet();
DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;
// PrintService printService[] =
// PrintServiceLookup.lookupPrintServices(flavor, pras);
PrintService defaultService =
PrintServiceLookup.lookupDefaultPrintService();
DocFlavor printerFlavor[] = defaultService.getSupportedDocFlavors();
if(printerFlavor!=null){
for(int i=0; i < printerFlavor.length; i++)
System.out.println(defaultService.getName()+" surpport type: "+i+" - "+printerFlavor[i].getMimeType());
}
if (!defaultService.isDocFlavorSupported(flavor)) {
System.out.println("Printer dosn't surpport the flavor:"+flavor.getMediaType());
}
if (defaultService != null &&defaultService.isDocFlavorSupported(flavor)) {
DocPrintJob job = defaultService.createPrintJob();
FileInputStream fis = new FileInputStream(filename);
DocAttributeSet das = new HashDocAttributeSet();
Doc doc = new SimpleDoc(fis, flavor, das);
System.out.println("print data: "+doc.getPrintData());
System.out.println("print data attributs: "+doc.getAttributes());
job.print(doc, pras);
Thread.sleep(10000);
}else
System.out.println("Printer dosn't surpport the flavor:"+flavor.getMimeType());
System.exit(0);
}

}
但是我执行结果是
D:\TaiFook\MSS Surport\iReport\src\APP-INF\classes>java PrintTestByCommand E:\\I
TSR.pdf
hp deskjet 9300 series surpport type: 0 - image/gif
hp deskjet 9300 series surpport type: 1 - image/gif
hp deskjet 9300 series surpport type: 2 - image/gif
hp deskjet 9300 series surpport type: 3 - image/jpeg
hp deskjet 9300 series surpport type: 4 - image/jpeg
hp deskjet 9300 series surpport type: 5 - image/jpeg
hp deskjet 9300 series surpport type: 6 - image/png
hp deskjet 9300 series surpport type: 7 - image/png
hp deskjet 9300 series surpport type: 8 - image/png
hp deskjet 9300 series surpport type: 9 - application/x-java-jvm-local-objectref
hp deskjet 9300 series surpport type: 10 - application/x-java-jvm-local-objectref
hp deskjet 9300 series surpport type: 11 - application/octet-stream
hp deskjet 9300 series surpport type: 12 - application/octet-stream
hp deskjet 9300 series surpport type: 13 - application/octet-stream

Printer dosn't surpport the flavor:application
Printer dosn't surpport the flavor:application/pdf
居然说打印机(我的打印机是:hp deskjet 9300 series)不支持PDF格式哦,有点让人不能接受,平常打印PDF文件没有任何问题啊。
请各位大侠帮忙看看什么原因啊。。。。
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
blueyxr
Java SE 62,621 成员 307,251 内容