itext7 java转pdf并插入图片的简单模板

itext7 java转pdf并插入图片的简单模板

pom依赖

 		<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.0.2</version>
            <type>pom</type>
        </dependency>
public static void createTempPdf(String filePath,String image){
        try {
            PdfWriter writer = new PdfWriter(new FileOutputStream(new File(filePath)));
            PdfDocument pdf = new PdfDocument(writer);
            Document document = new Document(pdf);
            PdfFont font = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
            Paragraph title = new Paragraph("标题")
                    .setTextAlignment(TextAlignment.CENTER)
                    .setFontSize(16);
            title.setFont(font);
            document.add(title);
            document.add(new Paragraph("模板内容").setFont(font));
            Image codeQrImage = new Image(ImageDataFactory.create("F:/"+image),450,600);
            //图片大小
            codeQrImage.scaleAbsolute(70, 30);
            document.add(codeQrImage);
            document.close();
            writer.close();
            pdf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
                createTempPdf("F:/"+"名称.pdf","a.jpg");
            }

我是存在f盘了 ,根据个人需求存储到相应位置就好

参考博客:https://blog.csdn.net/weixin_30243533/article/details/95257015?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase
https://blog.csdn.net/weixin_38345306/article/details/85929589
希望能对大家有所帮助

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值