本文将Base64编码形式的pdf文件加水印,并输出完成后的pdf的Base64编码,也可以根据情况自行改动,输出其他形式的内容。
首先引入两个包
<!-- PDF文件依赖包 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13.1</version>
</dependency>
<!-- PDF文件字体 防止中文乱码 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
完整代码如下:
package com;
import com.itextpdf.text.Element;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
public class testPdf {
public static void main(String[] args) {
try {
String pdfBase64 = "JVBERi0xLjQKJeLjz9MKMiAwIG9iago8PC9MZW5ndGggOTEvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KeJwr5HIK4TI2U7AwMFMISeEyUNA1tAAx9N0MFQyNFELSuDQ8UnNy8hXC84tyUhSpytYMyQJaaIBirwYN7Bm1c9DaiVVLQUoadRF9bCHbRiTFriFcgVwADhAu8AplbmRzdHJlYW0KZW5kb2JqCjQgMCBvYmoKPDwvVHlwZS9QYWdlL01lZGlhQm94WzAgMCA1OTUgODQyXS9SZXNvdXJjZXM8PC9Gb250PDwvRjEgMSAwIFI+Pj4+L0NvbnRlbnRzIDIgMCBSL1BhcmVudCAzIDAgUj4+CmVuZG9iagoxIDAgb2JqCjw8L1R5cGUvRm9udC9TdWJ0eXBlL1R5cGUxL0Jhc2VGb250L0hlbHZldGljYS9FbmNvZGluZy9XaW5BbnNpRW5jb2Rpbmc+PgplbmRvYmoKMyAwIG9iago8PC9UeXBlL1BhZ2VzL0NvdW50IDEvS2lkc1s0IDAgUl0+PgplbmRvYmoKNSAwIG9iago8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMyAwIFI+PgplbmRvYmoKNiAwIG9iago8PC9Qcm9kdWNlcihpVGV4dK4gNS41LjEzLjEgqTIwMDAtMjAxOSBpVGV4dCBHcm91cCBOViBcKEFHUEwtdmVyc2lvblwpKS9DcmVhdGlvbkRhdGUoRDoyMDIzMDcwNjE0NTUzOCswOCcwMCcpL01vZERhdGUoRDoyMDIzMDcwNjE0NTUzOCswOCcwMCcpPj4KZW5kb2JqCnhyZWYKMCA3CjAwMDAwMDAwMDAgNjU1MzUgZiAKMDAwMDAwMDI4NCAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAzNzIgMDAwMDAgbiAKMDAwMDAwMDE3MiAwMDAwMCBuIAowMDAwMDAwNDIzIDAwMDAwIG4gCjAwMDAwMDA0NjggMDAwMDAgbiAKdHJhaWxlcgo8PC9TaXplIDcvUm9vdCA1IDAgUi9JbmZvIDYgMCBSL0lEIFs8YTRkYTc4NmQ5MTllZDllMTg2NzQ0MDJlMDMwYWJhNzk+PGE0ZGE3ODZkOTE5ZWQ5ZTE4Njc0NDAyZTAzMGFiYTc5Pl0+PgolaVRleHQtNS41LjEzLjEKc3RhcnR4cmVmCjYyOAolJUVPRgo=";
String waterMarkName = "这是测试水印";
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] decodeBuffer = base64Decoder.decodeBuffer(pdfBase64);
String outBase64 = PDFAddWatermark(pdfBase64, waterMarkName);
// 为了验证结果方便,直接转为文件到本地
FileOutputStream fos = new FileOutputStream("C:\\Users\\zh\\Desktop\\a.pdf");
fos.write(base64Decoder.decodeBuffer(outBase64));
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 给PDF添加水印
* @param pdfBase64 原文件base64编码
* @param waterMarkName 添加水印的内容
*/
public static String PDFAddWatermark(String pdfBase64, String waterMarkName) throws Exception {
// 每页水印行数
int row = 7;
// 每行水印个数
int col = 3;
// 旋转角度
float rotation = 30f;
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] decodeBuffer = base64Decoder.decodeBuffer(pdfBase64);
// 读取PDF
PdfReader reader = new PdfReader(decodeBuffer);
//创建字节数组输出流
ByteArrayOutputStream ous =new ByteArrayOutputStream();
// 创建字节缓冲输出流
BufferedOutputStream bos = new BufferedOutputStream(ous);
// 输出的PDF文件内容
PdfStamper stamper = new PdfStamper(reader, bos);
BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
gs.setFillOpacity(0.2f);//改透明度
gs.setStrokeOpacity(0.4f);//设置笔触字体不透明度
PdfContentByte content;
int total = reader.getNumberOfPages();//pdf文件页数
for (int i=0; i<total; i++) {
// 在内容上方加水印
content = stamper.getOverContent(i+1);
// 在内容下方加水印
// content = stamper.getUnderContent(i+1);
//开始写入
content.beginText();
content.setGState(gs);
//字体大小
content.setFontAndSize(base, 20);
//每页7行,一行3个
for (int j=0; j<col; j++) {
for (int k=0; k<row; k++) {
float x = reader.getPageSize(i+1).getWidth() / col * j + 90;
float y = reader.getPageSize(i+1).getHeight() / row * k;
//showTextAligned 方法的参数(文字对齐方式,位置内容,输出水印X轴位置,Y轴位置,旋转角度)
content.showTextAligned(Element.ALIGN_CENTER, waterMarkName, x, y, rotation);
}
}
// 添加水印文字
content.endText();
}
stamper.close();
//添加水印后pdf的base64
byte[] bytes = ous.toByteArray();
ous.close();
bos.close();
reader.close();
return new BASE64Encoder().encode(bytes);
}
}
结果展示a.pdf:
文章来源:https://www.toymoban.com/news/detail-531744.html
文章来源地址https://www.toymoban.com/news/detail-531744.html
到了这里,关于java pdf加水印的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!