@world转pdf

前期准备工作:

1.先将world要修改的内容用 $ 表示
2.将world 另存为xml,再将xml文件后缀名改为.ftl
3.将用编辑器打开ftl文件,将修改的内容用${name}补充完整

java代码编写

要修改的内容参数

private static Map<String, Object> initWordMap() {Map<String, Object> wordData = new HashMap<String, Object>();wordData.put("contractId", "ht00011");wordData.put("person", "作者1111");wordData.put("userName", "张三22222");wordData.put("account", "账号001");wordData.put("sYear", "2022");wordData.put("sMonth", "10");wordData.put("sDay", "23");wordData.put("eYear", "2032");wordData.put("eMonth", "10");wordData.put("eDay", "23");wordData.put("bigMoney", "贰仟");wordData.put("smellMoney", "27890");wordData.put("signYear", "2022");wordData.put("signMonth", "11");wordData.put("signDay", "16");return wordData;}

** 修改world内容**

public static void downloadWord(Map<String, Object> wordData) throws Exception {String docName = "";Writer writer = null;String wordModePath = "F:\\";String wordModeFile ="财务合同.ftl";// 2. 设置配置内容// 设置版本Configuration configuration = new Configuration(new Version(VERSION));// 指定加载Word模板的路径configuration.setDirectoryForTemplateLoading(new File(wordModePath));// 以UTF-8的编码格式,读取模板文档Template template = configuration.getTemplate(wordModeFile, CHARSET);String contractId = wordData.get("contractId").toString();//合同编号// 3. 输出文档路径及名称docName = wordModePath+contractId+"_"+System.currentTimeMillis()  + ".doc";File outFile = new File(docName);writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), CHARSET), 10240);// 输出template.process(wordData, writer);writer.flush();writer.close();System.out.println(docName+":导出已完成");String pdfName = wordModePath+contractId+"_"+System.currentTimeMillis()  + ".pdf";worldToPdf(docName,pdfName);}

world 转pdf
path:world 文件的存放路径
path2: pdf文件的存放路径

private static Boolean worldToPdf(String path,String path2) {Boolean flag = false;FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream(path);XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);PdfOptions pdfOptions = PdfOptions.create();fileOutputStream = new FileOutputStream(path2);PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);flag = true;System.out.println("生成成功!"+path2);} catch (IOException e) {e.printStackTrace();} finally {try{if(fileInputStream != null) fileInputStream.close();if(fileOutputStream != null) fileOutputStream.close();}catch (IOException e){e.printStackTrace();}}return flag;}

全部代码

{/*** 设置使用的编码格式*/private static final String CHARSET = "UTF-8";/*** 设置使用的版本*/private static final String VERSION = "2.3.0";public static void main(String[] args) throws Exception{//将world转为pdfdownloadWord("F:\\","财务合同。ftl");}public static void downloadWord(String wordModePath,String wordModeFile) throws Exception {String docName = "";Writer writer = null;// 2. 设置配置内容// 设置版本Configuration configuration = new Configuration(new Version(VERSION));// 指定加载Word模板的路径configuration.setDirectoryForTemplateLoading(new File(wordModePath));// 以UTF-8的编码格式,读取模板文档Template template = configuration.getTemplate(wordModeFile, CHARSET);// 3. 输出文档路径及名称docName = wordModePath+"_"+System.currentTimeMillis()  + ".doc";File outFile = new File(docName);writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), CHARSET), 10240);// 输出template.process(initWordMap(), writer);writer.flush();writer.close();System.out.println(docName+":导出已完成");String pdfName = wordModePath+System.currentTimeMillis()  + ".pdf";worldToPdf(docName,pdfName);}private static Boolean worldToPdf(String path,String path2) {Boolean flag = false;FileInputStream fileInputStream = null;FileOutputStream fileOutputStream = null;try {fileInputStream = new FileInputStream(path);XWPFDocument xwpfDocument = new XWPFDocument(fileInputStream);PdfOptions pdfOptions = PdfOptions.create();fileOutputStream = new FileOutputStream(path2);PdfConverter.getInstance().convert(xwpfDocument,fileOutputStream,pdfOptions);flag = true;System.out.println("生成成功!"+path2);} catch (IOException e) {e.printStackTrace();} finally {try{if(fileInputStream != null) fileInputStream.close();if(fileOutputStream != null) fileOutputStream.close();}catch (IOException e){e.printStackTrace();}}return flag;}private static Map<String, Object> initWordMap() {Map<String, Object> wordData = new HashMap<String, Object>();wordData.put("contractId", "ht00011");wordData.put("person", "作者1111");wordData.put("userName", "张三22222");wordData.put("account", "账号001");wordData.put("sYear", "2022");wordData.put("sMonth", "10");wordData.put("sDay", "23");wordData.put("eYear", "2032");wordData.put("eMonth", "10");wordData.put("eDay", "23");wordData.put("bigMoney", "贰仟");wordData.put("smellMoney", "27890");wordData.put("signYear", "2022");wordData.put("signMonth", "11");wordData.put("signDay", "16");return wordData;}
}

Java ${}修改world内容 并转pdf,亲测可以相关推荐

  1. 修改Anaconda默认的启动路径--亲测有效【附anaconda云盘链接】

    因为anaconda默认的启动路径是C盘,所以很多时候使用受限,因此更换启动路劲是很有必要的. 网上各种修改方式都有,但是很多方法都不奏效,本方法是记录我的修改历史,亲测有效 软件版本:Windows ...

  2. 用JAVA实现简单的WORD转PDF 亲测有效

    前阵子由于项目业务的需要,领导要求研究一下如何实现将word转换成pdf,经过一番的百度找到了一个比较简单的方法(本人比较懒),只需要导入一个JAR包和十几行代码就能够实现转换,而且转换的效果比预想的 ...

  3. windows/linux服务器上java使用openoffice将word文档转换为PDF(亲测可用)

    一. 前言 1. 开发过程中经常会使用java将office系列文档转换为PDF, 一般都使用微软提供的openoffice+jodconverter 实现转换文档. 2. openoffice既有w ...

  4. Java PPT转PDF 亲测无水印

    最近有个需求需要把PPT文件转为PDF,整了好久转换完成一直都是有水印的,最终终于找到了去水印的办法,记录一下. jar包自取 链接:https://pan.baidu.com/s/1IBJGQcnu ...

  5. MySQL中 auto_increment如何修改初始值和步长【亲测】

    1.如何查看auto_increment的初始值和步长 打开黑窗口,登录管理员账号和密码后,执行以下命令: show variables like 'auto_inc%'; -- 查看当前数据库的自增 ...

  6. java文件 默认打开方式_修改文件的默认打开方式(亲测有效)

    今天遇到了一个问题,那就是没办法修改文件的默认打开方式,Windos10系统. 可能我稍微有点强迫症什么的,每次默认记事本打开,虽然也可也选择其他的应用打开,但就是没有直接双击打开的爽. 一百度才发现 ...

  7. 如何复制权限受限PDF文件的内容(亲测有效,Microsoft Edge打开pdf文件)

    如何复制权限受限PDF文件的内容(亲测有效,Microsoft Edge打开pdf文件) 当我们阅读大型pdf文档资料时,会有做笔记的习惯,刚开始打字做笔记还好,但后面发现有用的内容好像有点多,于是选 ...

  8. pdf修改文字内容怎么修改

    我们现在随处可见pdf文件,pdf已经成为现在办公必备的文件啦,因为其容易排版,阅读体验好等原因被我们所喜欢,但我们修改起来不容易,pdf修改文字怎么修改呢?但我还是可以教你修改. 使用工具: 迅捷p ...

  9. JAVA-Word转PDF各种版本实现方式--亲测有效。。。持续更新中

    当下做一个项目,就是各种操作office,客户的需求总是各种不按常理,来需求就得搞啊.对JAVA操作office这方面真是头大,弟弟是真滴不懂不会啊.无奈只好试啊试的.网上一大堆好使的,一大堆不好使的 ...

最新文章

  1. java集合——队列和双端队列+优先级队列
  2. Hibernate陷阱
  3. UVa-12333:Revenge of Fibonacci 高精度
  4. 我心中的核心组件~HttpHandler和HttpModule实现图像的缩放与Url的重写
  5. 用c语言输入首字母判断星期几,输入字母,判断星期几,求大神指点
  6. oracle数据库中基础知识,oracle数据库基础知识
  7. FTP(持虚拟用户,并且每个虚拟用户可以具有独立的属性配置)
  8. docker 多容器连接 以Nginx+Php为例
  9. html+css+js实现小游戏flybird(完整版)
  10. C# 添加windows右键菜单
  11. 台式计算机M丅BF是什么,台式机主板的 BIOS ID 代码
  12. 用表格做一个简单地个人简历
  13. endnote中CWYW无文件_文献管理工具(三):EndNote 操作指南(免费在线版)
  14. TensorRT量化-FP16和INT8
  15. 100天精通Andriod逆向——第3天:真机环境配置
  16. posix_memalign函数详解以及使用时的注意事项
  17. 穷人与富人的差距:越有钱的人负债越高
  18. 攻防世界webshell
  19. 万互技术浅谈为什么完美的芭比,非要朝着不完美的方向努力?
  20. 我的算法基础实验代码-上篇

热门文章

  1. oracle gather trace,读懂SQL TRACE TKProf报告
  2. 误删windows启动分区的修复方法
  3. java实现手机开关机_Android实现关机与重启的几种方式(推荐)
  4. Ajax案例-三级联动-搜索框提示语
  5. Android Qcom Audio架构学习
  6. b关于bootstrap官网https://v4.bootcss.com/docs/layout/grid/关于栅格布局的学习和实践博客
  7. 计算机程序设计员理论二
  8. Unity 蓝湖 关于UI工作流优化的思考
  9. 含稀土配合物粒径小/发色性能好的聚苯乙烯荧光微球/交联羧基聚苯乙烯微球/单分散荧光微球/红色荧光微球
  10. 模拟器pubg mobile服务器维修,pubg mobile参数模拟器