java 编码填充 word 模板生成 word合同,并将word合同转成pdf 文档
一、
1、
package com.test;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.openxmlformats.schemas.drawingml.x2006.main.CTNonVisualDrawingProps;
import org.openxmlformats.schemas.drawingml.x2006.main.CTPositiveSize2D;
import org.openxmlformats.schemas.drawingml.x2006.wordprocessingDrawing.CTInline;
import org.apache.xmlbeans.*;
/**
* 自定义 XWPFDocument,并重写 createPicture()方法
* 重写的类 CustomXWPFDocument
*/
public class CustomXWPFDocument extends XWPFDocument {
public CustomXWPFDocument(InputStream in) throws IOException {
super(in);
}
public CustomXWPFDocument() {
super();
}
public CustomXWPFDocument(OPCPackage pkg) throws IOException {
super(pkg);
}
/**
* @param id
* @param width 宽
* @param height 高
* @param paragraph 段落
*/
public void createPicture(int id, int width, int height,XWPFParagraph paragraph) {
final int EMU = 9525;
width *= EMU;
height *= EMU;
String blipId = getAllPictures().get(id).getPackageRelationship().getId();
CTInline inline = paragraph.createRun().getCTR().addNewDrawing().addNewInline();
String picXml = ""
+ "<a:graphic xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\">"
+ " <a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
+ " <pic:pic xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
+ " <pic:nvPicPr>" + " <pic:cNvPr id=\""
+ id
+ "\" name=\"Generated\"/>"
+ " <pic:cNvPicPr/>"
+ " </pic:nvPicPr>"
+ " <pic:blipFill>"
+ " <a:blip r:embed=\""
+ blipId
+ "\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\"/>"
+ " <a:stretch>"
+ " <a:fillRect/>"
+ " </a:stretch>"
+ " </pic:blipFill>"
+ " <pic:spPr>"
+ " <a:xfrm>"
+ " <a:off x=\"0\" y=\"0\"/>"
+ " <a:ext cx=\""
+ width
+ "\" cy=\""
+ height
+ "\"/>"
+ " </a:xfrm>"
+ " <a:prstGeom prst=\"rect\">"
+ " <a:avLst/>"
+ " </a:prstGeom>"
+ " </pic:spPr>"
+ " </pic:pic>"
+ " </a:graphicData>" + "</a:graphic>";
inline.addNewGraphic().addNewGraphicData();
XmlToken xmlToken = null;
try {
xmlToken = XmlToken.Factory.parse(picXml);
} catch (XmlException xe) {
xe.printStackTrace();
}
inline.set(xmlToken);
inline.setDistT(0);
inline.setDistB(0);
inline.setDistL(0);
inline.setDistR(0);
CTPositiveSize2D extent = (CTPositiveSize2D) inline.addNewExtent();
extent.setCx(width);
extent.setCy(height);
CTNonVisualDrawingProps docPr = inline.addNewDocPr();
docPr.setId(id);
docPr.setName("图片" + id);
docPr.setDescr("测试");
}
}
2、
package com.test;
import java.util.Date;
import java.util.concurrent.Callable;
public class MyCallable implements Callable<Object> {
private String taskNum;
public MyCallable(String taskNum) {
this.taskNum = taskNum;
}
public Object call() throws Exception {
System.out.println(">>>" + taskNum + "任务启动");
Date dateTmp1 = new Date();
Thread.sleep(3000);
Date dateTmp2 = new Date();
long time = dateTmp2.getTime() - dateTmp1.getTime();
System.out.println(">>>" + taskNum + "任务终止");
return taskNum + "任务返回运行结果,当前任务时间【" + time + "毫秒】";
}
}
3、
package com.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class mythred {
public void WordRun(int i) throws Throwable{
Map<String, Object> param = new HashMap<String, Object>();
param.put("${name}", "金正恩");
param.put("${profession}", "特战组");
param.put("${sex}", "男");
param.put("${school_name}", "中国国防大学");
param.put("${twocode}", "遗留问题");
param.put("${header}", "风控预防针");
param.put("${date}", new Date().toString());
CustomXWPFDocument doc = WordUtil.generateWord(param, "c://mouth.docx");
FileOutputStream fopts = new FileOutputStream("c:/"+i+".docx");
doc.write(fopts);
fopts.close();
//Thread.sleep(20000);
System.out.println("模板生成");
}
}
4、
package com.test;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
/**
* 有返回值的线程
*/
@SuppressWarnings("unchecked")
public class Test {
public void PrintWord(final ArrayList<String> li, final Map<String,Object> map) throws Throwable, Throwable{
System.out.println("----程序开始运行----");
Date date1 = new Date();
int taskSize = 1;
// 创建一个线程池
ExecutorService pool = Executors.newFixedThreadPool(taskSize);
//创建缓存线程池,根据任务来自动创建线程的数量,可以完成创建的所有任务
// ExecutorService threadPool = Executors.newCachedThreadPool();
// 创建多个有返回值的任务
List<Future> list = new ArrayList<Future>();
for (int i=0; i < taskSize; i++) {
Callable c = new MyCallable(i+" ");
// 执行任务并获取Future对象
// Future f = pool.submit(c);
Future f = pool.submit(c);
final int task = i;
pool.execute(new Runnable() {
@Override
public void run() {
for(int j=0;j<=task;j++){
System.out.println(Thread.currentThread().getName()
+" is looping of