700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Java使用itext生成pdf并添加水印 二维码字节流

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

时间:2020-06-21 07:50:29

相关推荐

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 = "";String month = "3";String day = "20";String number = "xxxxxxxxxxxxxxx";String expireDay = "7";String signPlace = "xxxxxxxxxx所";String signDate = "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.4fgs.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");}}

最终效果:

二维码插入代码:

// 生成二维码BufferedImagepublic 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);

最终效果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。