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

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

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保持一样。
在这里插入图片描述

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
使用iText库可以轻松实现使用模板生成PDF插入图片的功能。首先,你需要准备好一个样板PDF文件作为模板。然后,你可以使用iText的相关类和方法来加载这个样板PDF文件,并在指定位置插入图片。 首先,你需要导入iText库的相关依赖。然后,通过创建一个PdfReader对象来加载样板PDF文件。接下来,你可以通过创建一个PdfStamper对象,并使用该对象的getOverContent方法来获取PDF页面的内容,以便在指定位置插入图片。 在插入图片之前,你需要通过创建一个Image对象来加载你要插入的图片文件。然后,你可以使用PdfContentByte类的addImage方法将图片插入PDF文件。 以下是一个简单的示例代码,演示了如何使用iText使用模板生成PDF插入图片的过程: ```java import com.itextpdf.text.DocumentException; import com.itextpdf.text.Image; import com.itextpdf.text.pdf.*; import java.io.FileOutputStream; import java.io.IOException; public class PdfGenerator { public static void main(String[] args) { try { // 加载样板PDF文件 PdfReader reader = new PdfReader("template.pdf"); // 创建PdfStamper对象,并指定输出文件 PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("output.pdf")); // 获取第一页内容 PdfContentByte content = stamper.getOverContent(1); // 加载要插入的图片 Image image = Image.getInstance("image.jpg"); // 设置图片位置和大小 image.setAbsolutePosition(100, 100); image.scaleAbsolute(200, 200); // 将图片插入PDF文件 content.addImage(image); // 关闭PdfStamper对象 stamper.close(); // 关闭PdfReader对象 reader.close(); System.out.println("PDF生成成功!"); } catch (IOException | DocumentException e) { e.printStackTrace(); } } } ``` 在上述示例代码,你需要将"template.pdf"替换为你的样板PDF文件的路径,"output.pdf"替换为生成的PDF文件的输出路径,"image.jpg"替换为你要插入的图片文件的路径。通过运行这个代码,你将会生成一个新的PDF文件,其包含了插入的图片。 希望这个例子对你有所帮助!如果你还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值