|
|
@ -110,7 +110,7 @@ public class WordUtils { |
|
|
|
// 打开文档
|
|
|
|
document = Dispatch.call(documents, "Open", wordFile, false, true).toDispatch(); |
|
|
|
// 如果文件存在的话,不会覆盖,会直接报错,所以我们需要判断文件是否存在
|
|
|
|
//addWatermark(wordFile,wordFile,mark);
|
|
|
|
addWatermark(app,documents,document,wordFile,"D:\\anrui\\1.doc",mark); |
|
|
|
//setWaterMark(app,mark);
|
|
|
|
File targetFile = new File(pdfPath); |
|
|
|
if (!targetFile.exists()) { |
|
|
@ -139,88 +139,13 @@ public class WordUtils { |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
public static void main(String[] args) { |
|
|
|
Map<String, Object> dataMap = new HashMap<String, Object>(); |
|
|
|
String curDate = DateUtils.dateConvertStr(new Date(), "yyyy年MM月dd日"); |
|
|
|
dataMap.put("partyA", "甲方卖出方"); |
|
|
|
dataMap.put("partyB", "乙方买受方"); |
|
|
|
// 图片
|
|
|
|
// dataMap.put("imgStr1", getImageStr("D:\\fileTest\\name.png"));
|
|
|
|
// dataMap.put("imgStr2", getImageStr("D:\\fileTest\\zhang.png"));
|
|
|
|
// 列表
|
|
|
|
Car car1 = new Car("奔驰", "1001", "v10"); |
|
|
|
Car car2 = new Car("宝马", "1002", "v10"); |
|
|
|
Car car3 = new Car("奥迪", "1003", "v10"); |
|
|
|
List<Car> list = new ArrayList<>(); |
|
|
|
list.add(car1); |
|
|
|
list.add(car2); |
|
|
|
list.add(car3); |
|
|
|
dataMap.put("carList", list); |
|
|
|
|
|
|
|
dataMap.put("money", "1"); |
|
|
|
dataMap.put("money1", "2"); |
|
|
|
dataMap.put("money2", "3"); |
|
|
|
dataMap.put("address", "河北省石家庄市桥西区"); |
|
|
|
|
|
|
|
// String typeName = "车辆登记合同";
|
|
|
|
String typeName = "新车买卖合同(现车)"; // 模板名
|
|
|
|
String sourcePath = "D:\\anrui\\"; // 模板路径
|
|
|
|
// String fileName = "车辆登记合同New.doc";
|
|
|
|
String fileName = "新车买.doc"; // 生成的word文件名
|
|
|
|
String targetPath = sourcePath; // 生成文件路径
|
|
|
|
|
|
|
|
|
|
|
|
creatWord(dataMap, typeName, sourcePath, targetPath, fileName); // 调用生成word方法
|
|
|
|
String wordPath = targetPath + "\\" + fileName; // 新生成的word路径;
|
|
|
|
String pdfName = typeName + ".pdf"; // 生成pdf的文件名
|
|
|
|
doc2pdf(wordPath, targetPath, pdfName); // 调用将word转换为pdf的方法
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public static class Car { |
|
|
|
private String brand; |
|
|
|
private String vinNo; |
|
|
|
private String remarks; |
|
|
|
|
|
|
|
public Car(String brand, String vinNo, String remarks) { |
|
|
|
this.brand = brand; |
|
|
|
this.vinNo = vinNo; |
|
|
|
this.remarks = remarks; |
|
|
|
} |
|
|
|
|
|
|
|
public String getBrand() { |
|
|
|
return brand; |
|
|
|
} |
|
|
|
|
|
|
|
public void setBrand(String brand) { |
|
|
|
this.brand = brand; |
|
|
|
} |
|
|
|
|
|
|
|
public String getVinNo() { |
|
|
|
return vinNo; |
|
|
|
} |
|
|
|
|
|
|
|
public void setVinNo(String vinNo) { |
|
|
|
this.vinNo = vinNo; |
|
|
|
} |
|
|
|
|
|
|
|
public String getRemarks() { |
|
|
|
return remarks; |
|
|
|
} |
|
|
|
|
|
|
|
public void setRemarks(String remarks) { |
|
|
|
this.remarks = remarks; |
|
|
|
} |
|
|
|
} |
|
|
|
public static void addWatermark(String src, String des, String text) throws Exception { |
|
|
|
public static void addWatermark(ActiveXComponent wordApp, Dispatch documents,Dispatch doc,String src, String des, String text) throws Exception { |
|
|
|
ComThread.InitMTA(); |
|
|
|
ActiveXComponent wordApp = new ActiveXComponent("word.Application"); |
|
|
|
// 文档隐藏时进行应用操作
|
|
|
|
wordApp.setProperty("Visible", new Variant(false)); |
|
|
|
// 实例化模板Workbooks对象
|
|
|
|
Dispatch documents = wordApp.getProperty("Documents").toDispatch(); |
|
|
|
//打开文件
|
|
|
|
Dispatch doc = Dispatch.call(documents, "Open", src, new Variant(false),new Variant(false)).toDispatch(); |
|
|
|
Dispatch docSelect = Dispatch.get(wordApp, "Selection").toDispatch(); |
|
|
|
//取得活动窗体对象
|
|
|
|
Dispatch activeDoc = wordApp.getProperty("ActiveWindow").toDispatch(); |
|
|
@ -281,11 +206,20 @@ public class WordUtils { |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} finally { |
|
|
|
Dispatch.call(doc, "Close", new Variant(-1),new Variant(1),new Variant(true)); |
|
|
|
/* Dispatch.call(doc, "Close", new Variant(-1),new Variant(1),new Variant(true)); |
|
|
|
if (wordApp != null) { |
|
|
|
wordApp.invoke("Quit", new Variant[]{}); |
|
|
|
} |
|
|
|
ComThread.Release(); |
|
|
|
ComThread.Release();*/ |
|
|
|
// 关闭文档
|
|
|
|
// Dispatch.call(doc, "Close", false);
|
|
|
|
// 关闭office
|
|
|
|
// wordApp.invoke("Quit", 0);
|
|
|
|
System.out.println("关闭文档"); |
|
|
|
// if (app != null)
|
|
|
|
// app.invoke("Quit", new Variant[]{});
|
|
|
|
// 如果没有这句话,winword.exe进程将不会关闭
|
|
|
|
// ComThread.Release();
|
|
|
|
} |
|
|
|
} |
|
|
|
/* |
|
|
@ -343,5 +277,77 @@ public class WordUtils { |
|
|
|
Dispatch.put(view, "SeekView", new Variant(0)); |
|
|
|
}*/ |
|
|
|
|
|
|
|
public static void main(String[] args) { |
|
|
|
Map<String, Object> dataMap = new HashMap<String, Object>(); |
|
|
|
String curDate = DateUtils.dateConvertStr(new Date(), "yyyy年MM月dd日"); |
|
|
|
dataMap.put("partyA", "甲方卖出方"); |
|
|
|
dataMap.put("partyB", "乙方买受方"); |
|
|
|
// 图片
|
|
|
|
// dataMap.put("imgStr1", getImageStr("D:\\fileTest\\name.png"));
|
|
|
|
// dataMap.put("imgStr2", getImageStr("D:\\fileTest\\zhang.png"));
|
|
|
|
// 列表
|
|
|
|
Car car1 = new Car("奔驰", "1001", "v10"); |
|
|
|
Car car2 = new Car("宝马", "1002", "v10"); |
|
|
|
Car car3 = new Car("奥迪", "1003", "v10"); |
|
|
|
List<Car> list = new ArrayList<>(); |
|
|
|
list.add(car1); |
|
|
|
list.add(car2); |
|
|
|
list.add(car3); |
|
|
|
dataMap.put("carList", list); |
|
|
|
|
|
|
|
dataMap.put("money", "1"); |
|
|
|
dataMap.put("money1", "2"); |
|
|
|
dataMap.put("money2", "3"); |
|
|
|
dataMap.put("address", "河北省石家庄市桥西区"); |
|
|
|
|
|
|
|
// String typeName = "车辆登记合同";
|
|
|
|
String typeName = "新车买卖合同(现车)"; // 模板名
|
|
|
|
String sourcePath = "D:\\anrui\\"; // 模板路径
|
|
|
|
// String fileName = "车辆登记合同New.doc";
|
|
|
|
String fileName = "新车买.doc"; // 生成的word文件名
|
|
|
|
String targetPath = sourcePath; // 生成文件路径
|
|
|
|
|
|
|
|
|
|
|
|
creatWord(dataMap, typeName, sourcePath, targetPath, fileName); // 调用生成word方法
|
|
|
|
String wordPath = targetPath + "\\" + fileName; // 新生成的word路径;
|
|
|
|
String pdfName = typeName + ".pdf"; // 生成pdf的文件名
|
|
|
|
doc2pdf(wordPath, targetPath, pdfName); // 调用将word转换为pdf的方法
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
public static class Car { |
|
|
|
private String brand; |
|
|
|
private String vinNo; |
|
|
|
private String remarks; |
|
|
|
|
|
|
|
public Car(String brand, String vinNo, String remarks) { |
|
|
|
this.brand = brand; |
|
|
|
this.vinNo = vinNo; |
|
|
|
this.remarks = remarks; |
|
|
|
} |
|
|
|
|
|
|
|
public String getBrand() { |
|
|
|
return brand; |
|
|
|
} |
|
|
|
|
|
|
|
public void setBrand(String brand) { |
|
|
|
this.brand = brand; |
|
|
|
} |
|
|
|
|
|
|
|
public String getVinNo() { |
|
|
|
return vinNo; |
|
|
|
} |
|
|
|
|
|
|
|
public void setVinNo(String vinNo) { |
|
|
|
this.vinNo = vinNo; |
|
|
|
} |
|
|
|
|
|
|
|
public String getRemarks() { |
|
|
|
return remarks; |
|
|
|
} |
|
|
|
|
|
|
|
public void setRemarks(String remarks) { |
|
|
|
this.remarks = remarks; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|