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 详情可参照
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加水印
铺满: