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;
    }
相关推荐
dangkei4 天前
【分享-POI工具,Excel字段取值容错小工具】
java·excel·poi
mikey棒棒棒13 天前
Apache Echarts和POI
apache·echarts·poi
小鲨鱼-LFSJXY1 个月前
【数据分享】2024年我国省市县三级的住宿服务设施数量(8类住宿设施/Excel/Shp格式)
poi·数据分享·立方数据学社·住宿服务
十夫长1 个月前
Java poi 模板导出Word 带图片
java·word·poi·图片·模板
小鲨鱼-LFSJXY2 个月前
【数据分享】2024年我国省市县三级的生活服务设施数量(46类设施/Excel/Shp格式)
poi·数据分享·立方数据学社·生活服务设施
cyt涛2 个月前
Apache POI—读写Office格式文件
apache·excel·poi·office·单元格·xssfworkbook·sxssfworkbook
-南帝-2 个月前
EasyExcel填充模板导出excel.xlsx
java·excel·poi·easyexcel·模板导出
ziyue75753 个月前
java给word设置复选框
java·开发语言·word·poi·复选框·aspose-words
ziyue75753 个月前
POI从3.14升级为5.2.0
java·poi
拾光师3 个月前
POI操作EXCEL增加下拉框
excel·poi