Java使用itext生成pdf并添加水印、二维码字节流

记录一次使用itext生成pdf,并添加水印的过程

本地字体路径:C:\Windows\Fonts

Maven导入:

<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.4</version>
</dependency>
<dependency>
    <groupId>com.itextpdf</groupId>
    <artifactId>itext-asian</artifactId>
    <version>5.2.0</version>
</dependency>

代码:

    public void pdf() {
        System.out.println(System.getProperty("user.dir"));
        BaseFont bf;
        Font titleFont = null;
        Font font = null;
        Font underlineFont = null;
        Font noFont = null;
        Font signatureFont = null;

        try {
            bf = BaseFont.createFont("font/simfang.ttf", BaseFont.IDENTITY_H,
                    BaseFont.NOT_EMBEDDED);//创建字体
            titleFont = new Font(bf, 20, Font.BOLD);//使用字体
            font = new Font(bf, 15);//使用字体
            underlineFont = new Font(bf, 15, Font.UNDERLINE);
            noFont = new Font(bf, 14);
            signatureFont = new Font(bf, 14);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            String name = "xxx";
            String idCode = "xxxxxxxxxxxxxxxxxx";
            String year = "2020";
            String month = "3";
            String day = "20";
            String number = "xxxxxxxxxxxxxxx";
            String expireDay = "7";
            String signPlace = "xxxxxxxxxx所";
            String signDate = "2020年3月20日";

            //实现A4纸页面
            Document document = new Document();
            PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("F:/解除医学观察告知书.pdf"));
            //打开文档
            document.open();
            // 加入水印
            PdfContentByte waterMar = pdfWriter.getDirectContentUnder();
            // 开始设置水印
            waterMar.beginText();
            // 设置水印透明度
            PdfGState gs = new PdfGState();
            // 设置填充字体不透明度为0.4f
            gs.setFillOpacity(0.1f);

            try {
                // 设置水印字体参数及大小 (字体参数,字体编码格式,是否将字体信息嵌入到pdf中(一般不需要嵌入),字体大小)
                waterMar.setFontAndSize(BaseFont.createFont("font/simfang.ttf", BaseFont.IDENTITY_H,
                        BaseFont.NOT_EMBEDDED), 50);
                // 设置透明度
                waterMar.setGState(gs);
                // 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度
                waterMar.showTextAligned(Element.ALIGN_RIGHT, "数据修复平台", 450, 900, 20);
                waterMar.showTextAligned(Element.ALIGN_RIGHT, "数据修复平台", 450, 700, 20);
                waterMar.showTextAligned(Element.ALIGN_RIGHT, "数据修复平台", 450, 500, 20);
                waterMar.showTextAligned(Element.ALIGN_RIGHT, "数据修复平台", 450, 300, 20);
                waterMar.showTextAligned(Element.ALIGN_RIGHT, "数据修复平台", 450, 100, 20);
                // 设置水印颜色
                waterMar.setColorFill(BaseColor.GRAY);
                //结束设置
                waterMar.endText();
                waterMar.stroke();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                waterMar = null;
                gs = null;
            }

            //段落
            Paragraph p = new Paragraph("NO:" + number, noFont);
            p.setIndentationLeft(380);

            document.add(p);

            p = new Paragraph("解除医学观察告知书", titleFont);
            p.setLeading(30);
            p.setAlignment(Element.ALIGN_CENTER);
            document.add(p);

            p = new Paragraph();
            //短语
            Phrase ph = new Phrase();
            //块
            Chunk space = new Chunk("    ", font);
            Chunk c0 = new Chunk(" " + name + " ", underlineFont);
            Chunk c1 = new Chunk("先生/女士(身份证号码:", font);
            Chunk c2 = new Chunk(" " + idCode + " ", underlineFont);
            Chunk c3 = new Chunk("),按照《中华人民共和国传染病防治法》相关规定,经卫健部门评估后决定自", font);
            Chunk c4 = new Chunk(" " + year + " ", underlineFont);
            Chunk c5 = new Chunk("年", font);
            Chunk c6 = new Chunk(" " + month + " ", underlineFont);
            Chunk c7 = new Chunk("月", font);
            Chunk c8 = new Chunk(" " + day + " ", underlineFont);
            Chunk c9 = new Chunk("日(期满", font);
            Chunk c10 = new Chunk(" " + expireDay + "天 ", underlineFont);
            Chunk c11 = new Chunk(")起解除对您的医学观察,并对您给予我们工作的支持和配合表示衷心感谢。", font);

            ph.add(space);
            ph.add(c0);
            ph.add(c1);
            ph.add(c2);
            ph.add(c3);
            ph.add(c4);
            ph.add(c5);
            ph.add(c6);
            ph.add(c7);
            ph.add(c8);
            ph.add(c9);
            ph.add(c10);
            ph.add(c11);
            p.add(ph);
            p.setSpacingBefore(20);
            p.setLeading(40);
            document.add(p);

            p = new Paragraph();
            ph = new Phrase();
            ph.add(new Chunk(signPlace+"\n", signatureFont));
            p.add(ph);
            ph = new Phrase();
            ph.add(new Chunk(signDate+"\n", signatureFont));
            p.add(ph);
            ph = new Phrase();
            ph.add(new Chunk("医生签名:", signatureFont));
            p.add(ph);

            p.setSpacingBefore(50);
            p.setIndentationLeft(300);
            p.setLeading(30);
            document.add(p);

            document.close();
            pdfWriter.close();
        } catch (Exception e) {
            System.out.println("file create exception");
        }
    }

最终效果:

 

二维码插入代码:

// 生成二维码BufferedImage
public BufferedImage get() throws Exception {
        String text = "http://47.115.86.208:8080/";
        int width = 95;
        int height = 95;
        Hashtable hints = new Hashtable();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints);
        return MatrixToImageWriter.toBufferedImage(bitMatrix);
    }
// 水印部分加上二维码
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(get(), "png",out);
byte[] b = out.toByteArray();
Image itextimage = Image.getInstance(b);
// 水印图片位置
itextimage.setAbsolutePosition(50, 730);
// 附件加上水印图片
waterMar.addImage(itextimage);

最终效果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值