最近公司要求实现打印文档,由于使用的打印机不能识别文档格式所以只能先选用第三方插件来完成功能,整理了一下分享给大家
package com.zhang.generalJava;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import javax.print.*;
import javax.print.attribute.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: zhangguodong
* @Descriptions:
* @Data: Created in 14:35 2017/11/8/008
*/
public class PrintServiceOfMy {
private static String path="E:\\工作\\测试文档\\3.pdf";
/**
* @Author: ***
* @Description:使用原生java打印文档(文件可以传输到打印机,但无法打印,暂时不知原因)
* @Date: 14:45 2017/11/10/010
* @return:
* @throws:
*/
public static void print(){
FileInputStream in = null;
try {
in = new FileInputStream(path);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(in == null);
DocFlavor docFlavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
// PrintService[] services = PrintServiceLookup.lookupPrintServices(docFlavor,aset);
PrintService myPrinter = PrintServiceLookup.lookupDefaultPrintService();
DocAttributeSet das = new HashDocAttributeSet();
Doc myDoc = new SimpleDoc(in,docFlavor,das);
if (myPrinter != null){
DocPrintJob docPrintJob = myPrinter.createPrintJob();
try {
docPrintJob.print(myDoc,aset);
System.out.println("完成打印");
} catch (PrintException e) {
e.printStackTrace();
}
}
}
/**
* @Author: ***
* @Description: 使用jacob答应word文档,需要下载jacob的jar包和对应的jacob库,并将库文件放到system32下或启动时加入
* @Date: 14:46 2017/11/10/010
* @return:
* @throws:
*/
public static void printWord(String path){
System.out.println("开始打印");
ComThread.InitSTA();
ActiveXComponent word=new ActiveXComponent("Word.Application");
Dispatch doc=null;
Dispatch.put(word, "Visible", new Variant(false));
Dispatch docs=word.getProperty("Documents").toDispatch();
try {
if(path != null){
doc=Dispatch.call(docs, "Open", path).toDispatch();
Dispatch.call(doc, "PrintOut");//打印
System.out.println(path);
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("打印失败");
}finally{
try {
if(doc!=null){
Dispatch.call(doc, "Close",new Variant(0));
}
} catch (Exception e2) {
e2.printStackTrace();
}
//释放资源
ComThread.Release();
}
}
/**
* @Author: ***
* @Description: 使用jacob打印excel文档,需要下载jacob的jar包和对应的jacob库,并将库文件放到system32下或启动时加入,暂时未测试
* @Date: 14:50 2017/11/10/010
* @return:
* @throws:
*/
public void printExcel(String path){
/**
* 功能:实现excel打印工作
*/
ComThread.InitSTA();
ActiveXComponent xl = new ActiveXComponent("Excel.Application");
try {
// System.out.println("version=" +
// xl.getProperty("Version"));
// 不打开文档
Dispatch.put(xl, "Visible", new Variant(false));
Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
// 打开文档
Dispatch excel = Dispatch.call(workbooks, "Open", path).toDispatch();
// 横向打印(2013/05/24)
// Dispatch currentSheet = Dispatch.get(excel,
// "ActiveSheet")
// .toDispatch();
// Dispatch pageSetup = Dispatch
// .get(currentSheet, "PageSetup").toDispatch();
// Dispatch.put(pageSetup, "Orientation", new Variant(2));
// 每张表都横向打印2013-10-31
Dispatch sheets = Dispatch.get((Dispatch) excel, "Sheets").toDispatch();
// 获得几个sheet
int count = Dispatch.get(sheets, "Count").getInt();
// System.out.println(count);
for (int j = 1; j <= count; j++) {
Dispatch sheet = Dispatch
.invoke(sheets, "Item", Dispatch.Get, new Object[] { new Integer(j) }, new int[1])
.toDispatch();
Dispatch pageSetup = Dispatch.get(sheet, "PageSetup").toDispatch();
Dispatch.put(pageSetup, "Orientation", new Variant(2));
Dispatch.call(sheet, "PrintOut");
}
// 开始打印
if (excel != null) {
// Dispatch.call(excel, "PrintOut");
// 增加以下三行代码解决文件无法删除bug
Dispatch.call(excel, "save");
Dispatch.call(excel, "Close", new Variant(true));
excel = null;
}
xl.invoke("Quit", new Variant[] {});
xl = null;
} catch (Exception e) {
e.printStackTrace();
} finally {
// 始终释放资源
ComThread.Release();
}
}
/**
* @Author: ***
* @Description: 使用sumatra插件打印pdf,需要安装sumatra软件,调用命令行进行打印
* @Date: 14:53 2017/11/10/010
* @return:
* @throws:
*/
public static void printPdf(String path){
// String path = "E:\\工作\\测试文档\\";
File file = new File(path);
List<String> fns = new ArrayList<String>();
if (file.exists() && file.isDirectory()){
String []fs = file.list();
for (String fn : fs){
if (fn.endsWith(".pdf")){
fns.add(fn);
}
}
}
System.out.println(fns);
String [] fs = fns.toArray(new String[1]);
for(int i = 0; i < fs.length; i++){
fs[i] = "d:\\software\\sumatrapdf\\SumatraPDF.exe -print-to-default \""+ path + fs[i]+"\"";
}
try {
for(String fn:fs){
System.out.println(fn);
Runtime.getRuntime().exec(fn);
}
} catch (IOException e) {
e.printStackTrace();
}
}
public static void printByWord(String path){
path="E:\\工作\\测试文档\\Doc1.docx";
System.out.println("开始打印");
ComThread.InitSTA();
ActiveXComponent word=new ActiveXComponent("Word.Application");
Dispatch doc=null;
Dispatch.put(word, "Visible", new Variant(false));
Dispatch docs=word.getProperty("Documents").toDispatch();
try {
doc=Dispatch.call(docs, "Open", path).toDispatch();
Dispatch.call(doc, "PrintOut");//打印
} catch (Exception e) {
e.printStackTrace();
System.out.println("打印失败");
}finally{
try {
if(doc!=null){
Dispatch.call(doc, "Close",new Variant(0));
}
} catch (Exception e2) {
e2.printStackTrace();
}
//释放资源
ComThread.Release();
}
}
public static void main(String[] args) {
System.out.println(111);
printByWord("");
// print3();
}
}