1、自行制作自己的word模板,需要动态写入数据的地方,用书签形式。 光标放到特定位置,点击插入-书签,起书签名字。

2、代码读取模板文档。

Map<String, String> details = new HashMap<>(1);
details.put("书签名字", 动态数据);

String templatePath="模板.docx";


//临时文件  用来复制模板文档
File tempFile = new File("xxxx.docx");
String tempFilePath = tempFile.getPath();
InputStream inputStream = new FileInputStream(templatePath);
com.aspose.words.Document doc = new com.aspose.words.Document(inputStream);
DocumentBuilder builder = new DocumentBuilder(doc);
BookmarkCollection bookmarks = doc.getRange().getBookmarks();
//遍历把书签写进去
for (Map.Entry<String, String> entry : details.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();
        if (builder.moveToBookmark(key) && StringUtil.isNotEmpty(value)) {
            builder.write(value);
            bookmarks.remove(key);
        }
    }
//保存临时文件
 doc.save(tempFilePath);

//临时文件转为pdf

File tempFile1 = new File("xxxx.pdf");
FileOutputStream out = new FileOutputStream(tempFile1);
LicenseLoad.getLicense();
com.aspose.words.Document doc = new com.aspose.words.Document(wordPath);
//设置需要的字体,字体下载下来,放到指定文件夹
FontSettings.setFontsFolder("C:\\Users\\Desktop\\font\\", true);
doc.save(out, SaveFormat.PDF);
out.close();

tempFile1 为最后的pdf  可以进行保存、 上传、直接输出等操作了

更多推荐