• java word转pdf,word模板


    maven

    1. <dependency>
    2. <groupId>org.apache.poi</groupId>
    3. <artifactId>poi-ooxml</artifactId>
    4. <version>5.2.2</version>
    5. </dependency>
    6. <!-- https://mvnrepository.com/artifact/fr.opensagres.xdocreport/fr.opensagres.xdocreport.converter.docx.xwpf -->
    7. <dependency>
    8. <groupId>fr.opensagres.xdocreport</groupId>
    9. <artifactId>fr.opensagres.xdocreport.converter.docx.xwpf</artifactId>
    10. <version>2.0.4</version>
    11. </dependency>
    12. <dependency>
    13. <groupId>com.deepoove</groupId>
    14. <artifactId>poi-tl</artifactId>
    15. <version>1.12.1</version>
    16. </dependency>

    poi-tl语法参考文档

    http://deepoove.com/poi-tl/

    测试poi-tl

    1. import com.deepoove.poi.XWPFTemplate;
    2. import java.io.FileOutputStream;
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5. import java.util.List;
    6. import java.util.Map;
    7. public class TestPoiTl {
    8. public static void main(String[] args) throws Exception {
    9. List<Map<String,Object>> users = new ArrayList<>();
    10. users.add(user("张三",10,"zs"));
    11. users.add(user("李四",20,"ls"));
    12. XWPFTemplate template = XWPFTemplate.compile("D:\\wordpdf\\template.docx").render(
    13. new HashMap<String, Object>(){{
    14. put("users", users);
    15. }});
    16. template.writeAndClose(new FileOutputStream("D:\\wordpdf\\output.docx"));
    17. }
    18. static Map<String,Object> user(String name,int age,String account){
    19. Map<String,Object> user = new HashMap<>();
    20. user.put("name",name);
    21. user.put("age",age);
    22. user.put("account",account);
    23. return user;
    24. }
    25. }

    测试word转pdf

    1. import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
    2. import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;
    3. import org.apache.poi.xwpf.usermodel.XWPFDocument;
    4. import java.io.*;
    5. public class word2pdf {
    6. /**
    7. * @param args the command line arguments
    8. * @throws java.io.IOException
    9. */
    10. public static void main(String[] args) throws IOException {
    11. String docPath = "D:\\wordpdf\\test.docx";
    12. String pdfPath = "D:\\wordpdf\\test.pdf";
    13. XWPFDocument document;
    14. InputStream doc = new FileInputStream(docPath);
    15. document = new XWPFDocument(doc);
    16. PdfOptions options = PdfOptions.create();
    17. OutputStream out = new FileOutputStream(pdfPath);
    18. PdfConverter.getInstance().convert(document, out, options);
    19. doc.close();
    20. out.close();
    21. }
    22. }

    test.docx 截图

    template.docx截图

    效果截图

  • 相关阅读:
    基于SSM跨境电商网站的设计与实现/海外购物平台的设计
    unity基础3-数据持久化
    关于某些地区延期举办2022年11月27日 PMI认证考试等有关事项的通知
    2022-08-27 第五组 张明敏 学习笔记
    Linux下快速搭建YApi接口管理平台
    reactnative 底部tab页面@react-navigation/bottom-tabs
    网络编程入门介绍:TCP 和 UDP
    JavaWeb项目:smbms(mysql)
    面向面试编程:分布式ID生成策略
    山西电力市场日前价格预测【2023-09-28】
  • 原文地址:https://blog.csdn.net/hyz792901324/article/details/134156553