Java代码 把doc、docx的word文档进行动态填写信息,生成pdf

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  可以进行保存、 上传、直接输出等操作了

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值