132 lines
3.9 KiB
Plaintext
132 lines
3.9 KiB
Plaintext
package com.bonus.utils;
|
|
|
|
import java.io.*;
|
|
|
|
import org.aspectj.weaver.ast.Test;
|
|
|
|
import com.aspose.words.*;
|
|
import com.aspose.words.net.System.Data.DataRelation;
|
|
import com.aspose.words.net.System.Data.DataRow;
|
|
import com.aspose.words.net.System.Data.DataSet;
|
|
import com.aspose.words.net.System.Data.DataTable;
|
|
|
|
|
|
public class DocToPdf {
|
|
public static boolean getLicense() {
|
|
boolean result = false;
|
|
try {
|
|
InputStream is = DocToPdf.class.getClassLoader().getResourceAsStream("license.xml");
|
|
License aposeLic = new License();
|
|
aposeLic.setLicense(is);
|
|
result = true;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return result;
|
|
}
|
|
|
|
public static void doc2pdf(String Address, String outPath) {
|
|
/**
|
|
* 1.加载授权文件
|
|
*/
|
|
if (!getLicense()) { // 验证License 若不验证则转化出的pdf文档会有水印产生
|
|
return;
|
|
}
|
|
try {
|
|
long old = System.currentTimeMillis();
|
|
/**
|
|
* 2.读取操作文件、创建接收文件
|
|
*/
|
|
Document doc = new Document(Address); // Address是将要被转化的word文档
|
|
File file = new File(outPath); // 新建一个空白pdf文档,用于接收转换
|
|
FileOutputStream os = new FileOutputStream(file);
|
|
|
|
/**
|
|
* 3.1替换文本域
|
|
*/
|
|
//替换单个文本
|
|
doc.getRange().replace("对A", "要不起", true, false);
|
|
String[] Flds = new String[]{"Title", "Name", "URL", "Note"}; //文本域
|
|
String Name = "小郭软件";
|
|
String URL = "http://xiaoguo123.com";
|
|
String Note = "分享绿色简单易用的小软件";
|
|
Object[] Vals = new Object[]{"小郭软件@2016", Name, URL, Note }; //值
|
|
//批量替换
|
|
doc.getMailMerge().execute(Flds, Vals);
|
|
|
|
|
|
/**
|
|
* 3.2替换单个循环表格
|
|
*/
|
|
//创建表格
|
|
DataTable userList=new DataTable("Visit");
|
|
//设置表头
|
|
userList.getColumns().add("Date");
|
|
userList.getColumns().add("IP");
|
|
userList.getColumns().add("PV");
|
|
//设置数据
|
|
for(int i=0;i<10;i++) {
|
|
DataRow dataRow=userList.newRow();
|
|
dataRow.set(0, "2019年10月"+(8+i)+"日");
|
|
dataRow.set(1, i*300);
|
|
dataRow.set(2, i*400);
|
|
userList.getRows().add(dataRow);
|
|
}
|
|
doc.getMailMerge().executeWithRegions(userList);
|
|
|
|
|
|
/**
|
|
* 3.3替换双层循环表格
|
|
*/
|
|
DataTable userTB=new DataTable("User"); //表格创建
|
|
userTB.getColumns().add("Name");
|
|
userTB.getColumns().add("RegDate");
|
|
DataTable infoTB=new DataTable("Info");
|
|
/**
|
|
* 创建相同表头"Name"以关联
|
|
*/
|
|
infoTB.getColumns().add("Name"); //表格创建
|
|
infoTB.getColumns().add("Date");
|
|
infoTB.getColumns().add("Time");
|
|
//设置数据源
|
|
for(int i=1;i<4;i++) {
|
|
DataRow dataRow=userTB.newRow();
|
|
dataRow.set(0, "User"+i);
|
|
dataRow.set(1, "2019年10月"+(8+i)+"日");
|
|
userTB.getRows().add(dataRow);
|
|
}
|
|
for(int i=1;i<4;i++) {
|
|
for(int j=1;j<3;j++) {
|
|
DataRow dataRow=infoTB.newRow();
|
|
dataRow.set(0, "User"+i);
|
|
dataRow.set(1, "2019年10月"+(8+i)+"日");
|
|
dataRow.set(2, i*j*20);
|
|
infoTB.getRows().add(dataRow);
|
|
}
|
|
}
|
|
//关联User和Info
|
|
DataSet dataSet=new DataSet();
|
|
dataSet.getTables().add(userTB);
|
|
dataSet.getTables().add(infoTB);
|
|
String[] contClos= {"Name"};
|
|
String[] lstCols= {"Name"};
|
|
dataSet.getRelations().add(new DataRelation("UserInfo", userTB, infoTB, contClos, lstCols));
|
|
doc.getMailMerge().executeWithRegions(dataSet);
|
|
|
|
/**
|
|
* 4.以指定格式保存
|
|
*/
|
|
doc.save(os, SaveFormat.PDF);// 全面支持DOC, DOCX, OOXML, RTF HTML,
|
|
long now = System.currentTimeMillis();
|
|
System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化用时
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
doc2pdf("C://documents/test.doc", "C://documents/test.pdf");
|
|
}
|
|
|
|
}
|