import com.aspose.words.*;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.data.PictureRenderData;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
import com.deepoove.poi.util.BytePictureUtils;
public static void main(String[] args) {
try {
String str = "/Users/xiaojundream/Desktop/general.docx";
//XWPFTemplate 模版渲染 word 模板
XWPFTemplate template = XWPFTemplate.compile(str)
.render(initWordMap());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
template.write(baos);
byte[] bytes = baos.toByteArray();
baos.close();
Document doc = new Document(new ByteArrayInputStream(bytes));
//添加 wps fonts字体 转换成PDF
FontSourceBase[] fontSourceBases = FontSettings.get
DefaultInstance().getFontsSources();
ArrayList<FontSourceBase> fontSources = new ArrayList<>();
for (int var3 = 0; var3 < fontSourceBases.length; ++var3) {
fontSources.add(fontSourceBases[var3]);
}
FolderFontSource folderFontSource = new FolderFontSource("/Users/xiaojundream/Desktop".concat("/fonts"), true);
fontSources.add(folderFontSource);
FontSourceBase[] fonts = new FontSourceBase[fontSources.size()];
FontSettings.getDefaultInstance().setFontsSources(fontSources.toArray(fonts));
//将改好的 docx转成PDF
doc.save("/Users/xiaojundream/Desktop/222.pdf",SaveFormat.PDF);
} catch (Exception ex) {
ex.printStackTrace();
}
}
//world模版操作
private static Map<String, Object> initWordMap() {
Map<String, Object> wordData = new HashMap<String, Object>();
//checkbox :o->未打勾; R->打勾
wordData.put("c".concat("006").concat("box1"), new TextRenderData("o",new com.deepoove.poi.data.style.Style("Wingdings",10)));
wordData.put("c".concat("006").concat("box2"), new TextRenderData("R",new com.deepoove.poi.data.style.Style("Wingdings 2",10)));
//
//普通的变量替换
wordData.put("memberName", "张三");
wordData.put("signatureOwnerDate", wordData.put("signatureDate", "____年__月__日"));
//用于word签字 图片插入
wordData.put("rentImage", new PictureRenderData(200, 50, ".png", BytePictureUtils.getUrlByteArray("https://xxx.com/9e728e4e-59f9-477a-a21c-442a4a3a0b58.png")));
return wordData;
}