poi-tl设置图片(通过word模板替换关键字,然后转pdf文件并下载)

选中图片右击 选择设置图片格式

例如word模板

maven依赖

        <!-- java 读取word文件里面的加颜色的字体  转pdf 使用  -->
        <dependency>
            <groupId> e-iceblue </groupId>
            <artifactId>spire.doc.free</artifactId>
            <version>3.9.0</version>
        </dependency>
 
  <!--poi 的相关组件 -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.1.2</version>
        </dependency>
 
        <!-- 不添加此包会提示错误 :   org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory   -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>
 
        <!-- 用于  word 转pdf  -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.2</version>
        </dependency>
 
        <!-- pdf 添加水印  对PDF文件的操作 -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.1</version>
        </dependency>
        <!-- PDF文件 字体 防止中文乱码  -->
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>
 
        <!--基于 poi实现word数据的替换 -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>
 
 
  
 
 
        <!--
         如果下载jar失败,说明下载jar失败,需要以下的maven依赖
         [ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException
  --> 
        <repository>
            <id>com.e-iceblue</id>
               <url>http://repo.e-iceblue.cn/repository/maven-public/</url>
           </repository>
      </repositories>
 
 
        <!--        <dependency>
                    <groupId>org.apache.pdfbox</groupId>
                    <artifactId>pdfbox</artifactId>
                    <version>2.0.25</version>
                </dependency>
               <dependency>
                    <groupId>fr.opensagres.xdocreport</groupId>
                    <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
                    <version>1.0.6</version>
                </dependency>
               <dependency>
                    <groupId>fr.opensagres.xdocreport</groupId>
                    <artifactId>fr.opensagres.xdocreport.document</artifactId>
                    <version>2.0.2</version>
                </dependency>
                <dependency>
                    <groupId>fr.opensagres.xdocreport</groupId>
                    <artifactId>org.apache.poi.xwpf.converter.core</artifactId>
                    <version>1.0.6</version>
                </dependency>-->
 
 
 
 
 
 
    <!-- 打包使用,需要配置 -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <!-- 打包时会将本地jar一起打包 -->
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
            </plugin>
        </plugins>
    </build>
 
  <!--基于 poi实现word数据的替换 -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>

<!-- 不添加此包会提示错误 :   org.openxmlformats.schemas.wordprocessingml.x2006.main.FontsDocument$Factory   -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
            <version>1.0.6</version>
        </dependency>

<!-- 用于  word 转pdf  -->
        <dependency>
            <groupId>fr.opensagres.xdocreport</groupId>
            <artifactId>xdocreport</artifactId>
            <version>2.0.2</version>
        </dependency>

读取

代码

 @GetMapping("exportPDF")
    public AjaxResult exportPDF(HttpServletResponse response) throws Exception {
        LoginUser loginUser = getLoginUser();
        OpenCompanyInfo companyInfo = openCompanyInfoService.selectEnterpriseInfo(loginUser.getId());
        if (companyInfo != null) {
            Map<String, Object> data = new HashMap<>();
            data.put("legalDeputy", companyInfo.getLegalDeputy());
            data.put("companyName", companyInfo.getCompanyName());
            data.put("companyCode", companyInfo.getCompanyCode());
            if (StringUtils.isNoneBlank(companyInfo.getBusinessLicenseUrl())) {
                InputStream businessLicenseUrl = minioClientUtils.returnInputStream(null, companyInfo.getBusinessLicenseUrl());
                data.put("businessLicenseUrl", Pictures.ofStream(businessLicenseUrl, PictureType.JPEG).size(120, 120).create());
            }
            if (StringUtils.isNoneBlank(companyInfo.getCompanyLogoUr())) {
                InputStream companyLogoUrl = minioClientUtils.returnInputStream(null, companyInfo.getCompanyLogoUr());
                data.put("companyLogoUrl", Pictures.ofStream(companyLogoUrl, PictureType.JPEG).size(120, 120).create());
            }
            InputStream inputStreamFile = ResourceReader.class.getResourceAsStream("/templates/模版.docx");
            XWPFTemplate template = XWPFTemplate.compile(inputStreamFile).render(data);
//            XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\11949\\Desktop\\模版.docx").render(data);
            byte[] array = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            template.writeAndClose(baos);
            array = baos.toByteArray();
            baos.close();
            template.close();
            InputStream inputStream = new ByteArrayInputStream(array);
            ByteArrayOutputStream pdfBaos = new ByteArrayOutputStream();
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
            fr.opensagres.poi.xwpf.converter.pdf.PdfConverter.getInstance().convert(xwpfDocument, pdfBaos, PdfOptions.create());
            inputStream.close();
            xwpfDocument.close();
            response.setContentType("application/octet-stream");
            String format = DateUtil.format(new Date(), "yyyy-MM-dd");
            response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("认证授权函" + format + ".pdf", "UTF-8"));
            OutputStream out = response.getOutputStream();
            BufferedOutputStream bos = new BufferedOutputStream(out);
            pdfBaos.writeTo(bos);
            bos.flush();
            out.flush();
            PoitlIOUtils.closeQuietlyMulti(template, bos, out);
        }
        return null;
    }
相关推荐
小鲨鱼-LFSJXY2 天前
【数据分享】2024年我国省市县三级的生活服务设施数量(46类设施/Excel/Shp格式)
poi·数据分享·立方数据学社·生活服务设施
cyt涛5 天前
Apache POI—读写Office格式文件
apache·excel·poi·office·单元格·xssfworkbook·sxssfworkbook
-南帝-21 天前
EasyExcel填充模板导出excel.xlsx
java·excel·poi·easyexcel·模板导出
ziyue75751 个月前
java给word设置复选框
java·开发语言·word·poi·复选框·aspose-words
ziyue75751 个月前
POI从3.14升级为5.2.0
java·poi
拾光师2 个月前
POI操作EXCEL增加下拉框
excel·poi
辞半夏丶北笙2 个月前
word、pdf、excel及zip加密(含示例效果及工具类)
java·pdf·word·excel·zip·poi·加密
不见长安见晨雾3 个月前
EasyExcel基本使用
java·excel·poi·easyexcel
茉么乔3 个月前
java excel poi导出 支持多表头的公共导出方法
java·excel·poi
刘程云4 个月前
org/openxmlformats/schemas/spreadsheetml/x2006/main/CTWorkbook$Factory
java·poi·easyexcel