通过itext给pdf文件添加水印,并通过response返回

1.把原来从微软云download下的输出流转为输入流(因为PdfReader只接收输入流)

try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
	if (scontentType.equals("application/pdf")) {
		blobClient.download(outputStream);
		log.info("outputStream的大小:{}", outputStream.toByteArray().length);
		InputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
		addWaterMarkPdf(inputStream, response.getOutputStream());
	} else {
		blobClient.download(response.getOutputStream());
	}
} catch (Exception e) {
	log.error("[文档存储-下载文档]-下载文档失败");
	throw new UserException(MessageUtils.get(MessagesConst.DOCSTORAGE_OPERATION_DOWNLOAD_ERROR));
}

2.给需要返回的输出流添加水印(如果pdf总是无法打开,查看response的大小是否有设置,可能会大致水印无法加上,我就是在这被坑了好久)

3.用浏览器打开pdf无水印,用Adobe打开水印正常,这个跟字体有关

4.ttc字体文件后需要加  ,0/1   详情可参照

生成pdf设置中文字体出错 \simsun.ttc' with 'Identity-H' is not recognized或者type of font{0} is not recognized_万里飞鹏的博客-CSDN博客

5.itext-asian支持的字符集

iText-Asian jar可以使用的中文字符及iText——>html2pdf使用(附坑)_Chuck_le的博客-CSDN博客_itext-asian

6.给pdf加上文本水印后,文本可选中可编辑问题(暂没找到解决方式)

public void addWaterMarkPdf(InputStream inputStream, OutputStream outputStream) {
	try {
		// 原PDF文件
		PdfReader reader = new PdfReader(inputStream);

		// 输出的PDF文件内容
		PdfStamper stamper = new PdfStamper(reader, outputStream);

		// 字体 来源于 itext-asian JAR包
		BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
//            BaseFont baseFont = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
//            BaseFont baseFont = BaseFont.createFont("MHei-Medium", "UniCNS-UCS2-H", BaseFont.EMBEDDED);
//            BaseFont baseFont = BaseFont.createFont("MSung-Light", "UniCNS-UCS2-H", BaseFont.EMBEDDED);
//            BaseFont baseFont = BaseFont.createFont("MSungStd-Light", "UniCNS-UCS2-H", BaseFont.EMBEDDED);
//            BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\arial.ttf",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		//从本地读取字体文件,注意ttc文件后加 ,0
//            BaseFont baseFont = BaseFont.createFont("C:\\Windows\\Fonts\\simsun.ttc,0",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
		//从resources资源目录读取
//            BaseFont baseFont = BaseFont.createFont("/simsun.ttc,0",BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

		Rectangle pageRect = null;
		PdfGState gs = new PdfGState();
		// 设置透明度
		gs.setFillOpacity(0.3f);
		gs.setStrokeOpacity(0.4f);

		JLabel label = new JLabel();
		FontMetrics metrics;
		label.setText("void");
		metrics = label.getFontMetrics(label.getFont());
		int textH = metrics.getHeight();
		int textW = metrics.stringWidth(label.getText());

		int totalPage = reader.getNumberOfPages() + 1;
		for (int i = 1; i < totalPage; i++) {
			pageRect = reader.getPageSizeWithRotation(i);
			//内容上层
			PdfContentByte content = stamper.getOverContent(i);

			content.beginText();
			// 字体添加透明度
			content.setGState(gs);
			// 添加字体大小等
			content.setFontAndSize(baseFont, 20);
			//content.setRGBColorFill(255, 0, 0);

			for (int height = textH; height < pageRect.getHeight(); height = height + textH*3) {
				for (int width = textW; width < pageRect.getWidth() + textW; width = width + textW*2) {
					content.showTextAligned(Element.ALIGN_LEFT, "void", width - textW, height - textH, 30);
				}
			}

			content.endText();
		}
		// 关闭
		stamper.close();
		reader.close();
	} catch (Exception e) {
		e.printStackTrace();
	}

}

参考博客:

Java实现对PDF文件添加水印_天涯共明月的博客-CSDN博客_java中pdf加水印

铺满:

Java实现pdf文件添加水印,铺满全页。_duan1309的博客-CSDN博客_pdf水印怎么铺满

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值