Java使用aspose.pdf给PDF文件添加文字水印

1:提供一个aspose.pdf的jar包下载路径:

     链接:https://pan.baidu.com/s/118BvmZMwt0k0CjiJgdOEFg 
     提取码:tflv

2:代码如下:直接使用

// 添加水印
//filepath:文件路径
//data:水印文字内容
	public static void addWatermark(String filepath,String data) {
		
		Document pdfDocument = new Document(filepath);
		TextStamp textStamp = new TextStamp(data);
		textStamp.setBackground(true);
		textStamp.setXIndent(100);
		textStamp.setYIndent(100);
		textStamp.setRotateAngle(50);
		
		textStamp.getTextState().setFont(FontRepository.findFont("Arial"));
		textStamp.getTextState().setFontSize(34.0F);
		textStamp.getTextState().setFontStyle(FontStyles.Italic);
		textStamp.getTextState().setForegroundColor(Color.getLightGray());

		TextStamp textStamp1 = new TextStamp(data);
		textStamp1.setBackground(true);
		textStamp1.setXIndent(300);//设置位置
		textStamp1.setYIndent(300);
		textStamp1.setRotateAngle(50);//设置旋转角度
		textStamp1.getTextState().setFont(FontRepository.findFont("Arial"));
		textStamp1.getTextState().setFontSize(34.0F);//设置字体大小
		textStamp1.getTextState().setFontStyle(FontStyles.Italic);
		textStamp1.getTextState().setForegroundColor(Color.getLightGray());//设置字体颜色

		TextStamp textStamp2 = new TextStamp(data);
		textStamp2.setBackground(true);
		textStamp2.setXIndent(500);
		textStamp2.setYIndent(500);
		textStamp2.setRotateAngle(50);
		textStamp2.getTextState().setFont(FontRepository.findFont("Arial"));
		textStamp2.getTextState().setFontSize(34.0F);
		textStamp2.getTextState().setFontStyle(FontStyles.Italic);
		textStamp2.getTextState().setForegroundColor(Color.getLightGray());
		
		
		PageCollection pages = pdfDocument.getPages();
		int size = pages.size();
		for (int i = 1; i <= size; i++) {
			pages.get_Item(i).addStamp(textStamp);
			pages.get_Item(i).addStamp(textStamp1);
			pages.get_Item(i).addStamp(textStamp2);
		}
		pdfDocument.save(filepath);
	}

3:效果如下:

 

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叮当LIU

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值