pdf文字水印,后添加

/**
* 中间或者两边水印
* @param bos 添加完水印的输出
* @param input 原PDF文件输入
* @param word 水印内容
* @param model 水印添加位置1中间,2两边
*/
public static setWatermark(BufferedOutputStream bos, InputStream input, String word, int model)
throws DocumentException, IOException {
PdfReader reader = new PdfReader(input);
PdfStamper stamper = new PdfStamper(reader, bos);
PdfContentByte content;
// 创建字体,第一个参数是字体路径,itext有一些默认的字体比如说:
//BaseFont base = BaseFont.createFont(“STSong-Light”, “UniGB-UCS2-H”, BaseFont.EMBEDDED);
BaseFont base = BaseFont.createFont("/msyh.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
// 获取PDF页数
int total = reader.getNumberOfPages();
// 遍历每一页
for (int i = 0; i < total; i++) {
float width = reader.getPageSize(i + 1).getWidth(); // 页宽度
float height = reader.getPageSize(i + 1).getHeight(); // 页高度
content = stamper.getOverContent(i + 1);// 内容
content.beginText();//开始写入文本
gs.setFillOpacity(0.3f);//水印透明度
content.setGState(gs);
content.setColorFill(BaseColor.LIGHT_GRAY);
content.setTextMatrix(70, 200);//设置字体的输出位置

		if (model == 1) { //平行居中的3条水印
			content.setFontAndSize(base, 50); //字体大小
			//showTextAligned 方法的参数分别是(文字对齐方式,位置内容,输出水印X轴位置,Y轴位置,旋转角度)
			content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 650, 30);
			content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 400, 30);
			content.showTextAligned(Element.ALIGN_CENTER, word, width / 2, 150, 30);
		} else { // 左右两边个从上到下4条水印
			float rotation = 30;// 水印旋转度数
		
			content.setFontAndSize(base, 20);
			content.showTextAligned(Element.ALIGN_LEFT, word, 20, height - 50, rotation);
			content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 4 * 3 - 50, rotation);
			content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 2 - 50, rotation);
			content.showTextAligned(Element.ALIGN_LEFT, word, 20, height / 4 - 50, rotation);

			content.setFontAndSize(base, 22);
			content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height - 50, rotation);
			content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 4 * 3 - 50, rotation);
			content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 2 - 50, rotation);
			content.showTextAligned(Element.ALIGN_RIGHT, word, width - 20, height / 4 - 50, rotation);
		}
		content.endText();//结束写入文本  
		//要打图片水印的话
		//Image image = Image.getInstance("c:/1.jpg");  
		//content.addImage(image);  
	}
	stamper.close();
}

public static void main(String[] args) throws FileNotFoundException, DocumentException, IOException {
setWatermark(new BufferedOutputStream(new FileOutputStream(new File("/Users/mimhope/Documents/abc.pdf"))),
new FileInputStream("/Users/mimhope/Documents/周一.pdf"), “测试打印”, 1);
}
————————————————
版权声明:本文为CSDN博主「面向顶风」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_41746337/article/details/97615150

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为了保护PDF文件中的内容不被不良用户盗用或复制,我们可以添加水印水印是一种特殊的图形或文字,可以在PDF文档的任何位置显示。在PHP开发中,我们可以使用phpoffice/phpword扩展来完成PDF添加水印的功能。 PHPoffice是一个开源的PHP办公套件,支持多种文档格式,包括doc、docx、xls、xlsx、ppt、pptx和PDF。使用PHPoffice中的PhpWord类,我们可以快速创建和编辑Word文档。而在创建PDF文档的时候,我们可以使用phpoffice/phpword扩展提供的TCPDF类(TCPDF是一个流行的PDF生成库)。 添加水印的步骤如下: 1. 首先,我们需要创建一个TCPDF实例。 2. 然后,我们可以使用Tcpdf类中提供的AddWatermarkImage(添加图片水印)或AddWatermarkText添加文字水印)方法来添加水印。 3. 最后,我们可以调用TCPDF实例的Output方法来输出PDF文件。 以下是一个简单的代码示例: <?php use PhpOffice\PhpWord\IOFactory; use PhpOffice\PhpWord\PhpWord; use TCPDF; // 创建TCPDF实例 $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); // 添加页面 $pdf->AddPage(); // 添加内容 $pdf->SetFont('stsongstdlight', '', 10); $pdf->Write(0, '这是一个PDF文件。'); // 添加水印 $pdf->AddWatermarkText('CONFIDENTIAL'); // 输出PDF文件 $pdf->Output('example.pdf', 'D'); ?> 在这个示例中,我们创建了一个TCPDF实例,并向其添加一个页面和一些内容。然后,我们使用AddWatermarkText方法添加了一个文本水印。最后,我们调用Output方法将PDF文件下载到用户计算机上。 总之,在PHP开发中,使用phpoffice/phpword扩展中的TCPDF类可以轻松完成PDF添加水印的功能。通过添加水印,我们可以有效地保护PDF文件的内容。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值