使用itextpdf在pdf模板中插入对应数据

2022-07-25,,,,

大家好今儿给大家带来的是使用itextpdfpdf模板插入对应数据

目录

  • 大家好今儿给大家带来的是使用itextpdf在pdf模板中插入对应数据
  • 1.需求描述
  • 2.操作Adobe Acrobat DC完成pdf域对象的添加
  • 3.代码部分(可直接用)
  • 4.测试
  • 5.总结

1.需求描述

在pdf模板中插入数据,大家都知道pdf是不可编辑的,在做这个需求的时候要借助到一个工具那就是 Adobe Acrobat DC 专门操作pdf的然后给对应的pdf加上对应的文本域对象和我们java bean中的属性名称一样,这样就可以再pdf中插入对应的数据了,这个软件可以再百度上搜一下下载一个就行。

2.操作Adobe Acrobat DC完成pdf域对象的添加

1.首先安装完成这个软件后我们打开软件点击准备表单,选择需要添加域对象的pdf

2.打开完成后我们可以看到上方有很的选项,直接点击文本域即可

3.名称中的key很重要必须和代码中的数据key一样,(表单域一定要选可见才能看到)向这样以此类推,把所有需要添加的文本域都加上后我们可以直接上代码了。

3.代码部分(可直接用)

注意:
字体
BaseFont.createFont(“C:/Windows/Fonts/simfang.ttf”, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); 字体的话可以把simfang.ttf复制一份放到项目里或者云存储上用的时候去拿。

如果放到项目里的话需要放在static里,代码如下
BaseFont.createFont(new ClassPathResource(“static/simfang.ttf”).getPath(), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

 		<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13.1</version>
        </dependency>
@RequestMapping("/pdf")
@RestController
public class PdfControllerTest {


    @Autowired
    private HttpServletRequest request;

    @Autowired
    private HttpServletResponse response;


    private void pdfExport(Map<String, String> inputMap) {
        // 模板路径
        String templatePath = "C:\\Users\\dxl\\Desktop\\测试.pdf";

        File file = new File(templatePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        PdfReader reader;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        OutputStream out = null;
        try {
            Map<String, String> datemap = inputMap;
            BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/simfang.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            // 输出流
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition",
                    "attachment;fileName=" + URLEncoder.encode(file.getName(), "UTF-8"));
            out = new BufferedOutputStream(response.getOutputStream());
            // 读取pdf模板
            reader = new PdfReader(templatePath);
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            AcroFields form = stamper.getAcroFields();
            //文字类的内容处理
            form.addSubstitutionFont(bf);
            for (String key : datemap.keySet()) {
                String value = datemap.get(key);
                form.setField(key, value);
            }
            stamper.setFormFlattening(false);
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, out);
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();

        } catch (IOException | DocumentException e) {
            System.out.println(e);
        } finally {
            try {
                assert out != null;
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


    @RequestMapping(value = "/getPdf", method = RequestMethod.GET)
    @ApiOperation("getPdf")
    public void getPdf() throws IOException {

        /**
         * map 中的key 要和 pdf中的域名称key 保持一样
         */
        Map<String, String> map = new HashMap<>();
        map.put("name", "dxl测试测试测试测试强啊!!!!");
        map.put("age", "100");
        map.put("adder", "测试测试测试测试");
        this.pdfExport(map);
    }

4.测试

访问接口

打开模板pdf

5.总结

操作pdf模板插入数据需要借助Adobe Acrobat DC工具完成文本域的添加在文本域中设置对应的key,在代码中的key保持一样。

本文地址:https://blog.csdn.net/weixin_41914928/article/details/112467458

《使用itextpdf在pdf模板中插入对应数据.doc》

下载本文的Word格式文档,以方便收藏与打印。