使用itext框架填充pdf模板

所需jar的pom坐标

		<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.10</version>
        </dependency>
        <dependency>
            <groupId>com.sun.media</groupId>
            <artifactId>jai_codec</artifactId>
            <version>1.1-mr</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/jai_codec-1.1-mr.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>javax.media</groupId>
            <artifactId>jai_core</artifactId>
            <version>1.1-mr</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/jai_core-1.1-mr.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>com.sun.medialib</groupId>
            <artifactId>mlibwrapper_jar</artifactId>
            <version>1.1</version>
            <scope>system</scope>
            <systemPath>${project.basedir}/src/main/resources/jar/mlibwrapper_jar-1.1.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.0-RC2</version>
        </dependency>
  /**
     * pdf填充文字信息
     *
     * @param targetPath   新生成的pdf路径
     * @param templatePath pdf模板路径
     * @param dataMap      key--value
     */
    public static void fillPdfWord(String targetPath, String templatePath, Map<String, String> dataMap) {
        PdfReader reader = null;
        ByteArrayOutputStream bos = null;
        PdfStamper ps = null;
        FileOutputStream fos = null;
        try {
            //设置字体
            BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            //创建一个pdf读取对象,读取pdf模板
            reader = new PdfReader(templatePath);
            //创建一个输出流
            bos = new ByteArrayOutputStream();
            //创建pdf模板,参数reader  bos
            ps = new PdfStamper(reader, bos);
            //封装数据,取出模板中的所有字段数据,读取文本域
            AcroFields form = ps.getAcroFields();
            //必须要调用这个,否则文档不会生成的
            ps.setFormFlattening(true);
            form.addSubstitutionFont(bf);
            for (String key : dataMap.keySet()) {
                String value = dataMap.get(key);
                //为指定域,设置字体大小
                form.setFieldProperty(key, "textsize", 7f, null);
                //为指定域,填充数据
                form.setField(key, value);
            }
            ps.close();
            //指定生成的新路径
            File file = new File(targetPath);
            //创建文件
            file.createNewFile();
            //使生成的文件file生效,这个必须有
            //创建文件输出流
            fos = new FileOutputStream(file);
            fos.write(bos.toByteArray());//写入数据
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) fos.close();
                if (bos != null) bos.close();
                if (reader != null) reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * pdf填充图片信息
     *
     * @param targetPath   新生成的pdf路径
     * @param templatePath pdf模板路径
     * @param imgMap       key--value
     */
    public static void fillPdfImage(String targetPath, String templatePath, Map<String, String> imgMap) {
        PdfReader reader = null;
        ByteArrayOutputStream bos = null;
        PdfStamper ps = null;
        FileOutputStream fos = null;
        try {
            //设置字体
            BaseFont bf = BaseFont.createFont("c://windows//fonts//simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            //创建一个pdf读取对象,读取pdf模板
            reader = new PdfReader(templatePath);
            //创建一个输出流
            bos = new ByteArrayOutputStream();
            //创建pdf模板,参数reader  bos
            ps = new PdfStamper(reader, bos);
            //封装数据,取出模板中的所有字段数据,读取文本域
            AcroFields form = ps.getAcroFields();
            //必须要调用这个,否则文档不会生成的
            ps.setFormFlattening(true);
            form.addSubstitutionFont(bf);
            for (String key : imgMap.keySet()) {
                String imgPath = imgMap.get(key);
                int pageNo = form.getFieldPositions(key).get(0).page;
                Rectangle signRect = form.getFieldPositions(key).get(0).position;
                float x = signRect.getLeft();
                float y = signRect.getBottom();
                // 根据路径读取图片
                Image image = null;
                image = Image.getInstance(imgPath);
                // 获取图片页面
                PdfContentByte under = ps.getOverContent(pageNo);
                // 图片大小自适应
                image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                // 添加图片
                image.setAbsolutePosition(x, y);
                under.addImage(image);
            }
            ps.close();
            //指定生成的新路径
            File file = new File(targetPath);
            //创建文件
            file.createNewFile();
            //使生成的文件file生效,这个必须有
            //创建文件输出流
            fos = new FileOutputStream(file);
            fos.write(bos.toByteArray());//写入数据
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) fos.close();
                if (bos != null) bos.close();
                if (reader != null) reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值