zoukankan      html  css  js  c++  java
  • 将图片和表单数据变为word 文档

    2 ,将图片和表单数据变为word 文档 (承接上一篇)  一开始采用的技术为poi  ,后来发现非常不好行不通 

    网上poi 将图片和表单数据变为word 文档  技术链接    https://blog.csdn.net/MatheoGao/article/details/79417190

    为什么不用,因为发现工具类中

    String blipId = getAllPictures().get(id).getPackageRelationship()
    
    .getId();  此.getPackageRelationship() 在相应的类中无发现,不能使用,也不知是版本问题还是去掉了,写错了
    
    

    于是用iText 技术实现 将图片和表单数据变为word 文档

    1,引入jar,下面有jar 包中央仓库没有,所以自己搞定

    <!--iText jar 包-->
                <dependency>
                    <groupId>com.lowagie</groupId>
                    <artifactId>itext</artifactId>
                    <version>2.1.7</version>
                </dependency>
               <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itextasian</artifactId>
                <version>1.0</version>
               </dependency>
            <dependency>
                <groupId>com.lowagie</groupId>
                <artifactId>itext-rtf</artifactId>
                <version>2.1.7</version>
            </dependency>

    2后台代码

    //利用iText  技术将图片写入word 文档
    
            //把文件放到桌面上
            File desktopDir = FileSystemView.getFileSystemView().getHomeDirectory();
            String desktopPath = desktopDir.getAbsolutePath();
            //格式化日期
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
            String d=sdf.format(new Date());
            String  wordFileName="食品分析"+d;
            Document document =new Document(PageSize.A4.rotate());
            RtfWriter2.getInstance(document, new FileOutputStream(desktopPath+"\"+wordFileName+".doc"));
            document.open();
    
            // 添加图片 Image.getInstance即可以放路径又可以放二进制字节流
            Image img = Image.getInstance(downloadFilePath);
            img.setAbsolutePosition(0, 0);
            img.setAlignment(Image.ALIGN_CENTER);// 设置图片显示位置
            img.scalePercent(30);//表示显示的大小为原尺寸的30%
            document.add(img);
            document.add(new Paragraph("
    "));


    //添加文字字段 // 设置字体,字号,加粗,颜色 Font font = new Font(Font.NORMAL, 20, Font.BOLD, new Color(255, 0, 0)); // 设置新的段落,使其字体为font String dstr="数据分析分:ndskncdsnvcjdsnjvndsjvbnjsndaijvnfds hvufesvfs"; Paragraph p = new Paragraph(dstr, font); // 设置段落居中,其中1为居中对齐,2为右对齐,3为左对齐 p.setAlignment(1); // 文档中加入该段落 document.add(p); document.close();

    运行完自动在卓面形成word 文档

  • 相关阅读:
    从零实现一个功能完善的迷你区块链
    Merkle Tree理解起来并不难
    微信、支付宝个人收款的一种实现思路
    PostgreSQL的登录、创建用户、数据库并赋权
    java list 按照多字段排序
    2019年最新全国省市区街道共46462条数据(统计局MySQL数据库)
    一份非常值得一看的Java面试题
    spring scope prototype与singleton
    http请求与响应,TCP三次握手&四次分手
    HTTP协议三次握手过程
  • 原文地址:https://www.cnblogs.com/jsbk/p/9483911.html
Copyright © 2011-2022 走看看