java aspose 加水印_给pdf文件加水印(aspose)

这是一个Java代码示例,演示如何使用Aspose库为PDF文件添加水印。代码创建了三个不同位置的文本水印,设置字体、大小、颜色等属性,并保存处理后的PDF。
摘要由CSDN通过智能技术生成

packagecom.tythin.tyboot.core.util;importcom.aspose.pdf.Color;importcom.aspose.pdf.Document;importcom.aspose.pdf.FontRepository;importcom.aspose.pdf.FontStyles;importcom.aspose.pdf.PageCollection;importcom.aspose.pdf.Rotation;importcom.aspose.pdf.TextStamp;importjava.io.InputStream;/***@authorkuangQingLin

*@version1.0

* @date 2019/10/21/021 15:39

**/

public classWatermarkPdfUtil {public static voidmain(String[] args) {

Document pdfDocument= new Document("C:\\Users\\Administrator\\Desktop\\采购合同.pdf");

TextStamp textStamp= new TextStamp("合同水印");

textStamp.setBackground(true);

textStamp.setXIndent(200);

textStamp.setYIndent(100);

textStamp.setRotate(Rotation.None);

textStamp.getTextState().setFont( FontRepository.findFont("Arial"));

textStamp.getTextState().setFontSize(34.0F);

textStamp.getTextState().setFontStyle(FontStyles.Bold);

textStamp.getTextState().setFontStyle(FontStyles.Italic);

textStamp.getTextState().setForegroundColor(Color.getPink());

TextStamp textStamp1= new TextStamp("合同水印");

textStamp1.setBackground(true);

textStamp1.setXIndent(200);

textStamp1.setYIndent(400);

textStamp1.setRotate(Rotation.None);

textStamp1.getTextState().setFont( FontRepository.findFont("Arial"));

textStamp1.getTextState().setFontSize(34.0F);

textStamp1.getTextState().setFontStyle(FontStyles.Bold);

textStamp1.getTextState().setFontStyle(FontStyles.Italic);

textStamp1.getTextState().setForegroundColor(Color.getPink());

TextStamp textStamp2= new TextStamp("合同水印");

textStamp2.setBackground(true);

textStamp2.setXIndent(200);

textStamp2.setYIndent(700);

textStamp2.setRotate(Rotation.None);

textStamp2.getTextState().setFont( FontRepository.findFont("Arial"));

textStamp2.getTextState().setFontSize(34.0F);

textStamp2.getTextState().setFontStyle(FontStyles.Bold);

textStamp2.getTextState().setFontStyle(FontStyles.Italic);

textStamp2.getTextState().setForegroundColor(Color.getPink());

PageCollection pages=pdfDocument.getPages();int size =pages.size();for (int i = 0; i < size; i++) {

pages.get_Item(i+ 1).addStamp(textStamp);

pages.get_Item(i+ 1).addStamp(textStamp1);

pages.get_Item(i+ 1).addStamp(textStamp2);

}

pdfDocument.save("C:\\Users\\Administrator\\Desktop\\采购合同11.pdf");

}/*** 给pdf文件加水印

* 水印字体不支持宋体,不要乱改字体 !!!

*@paraminputStream 输入流

*@parampath 输出路径*/

public static voidaddWater(InputStream inputStream, String path) {

Document pdfDocument= newDocument(inputStream);

TextStamp textStamp= new TextStamp("合同水印");

textStamp.setBackground(true);

textStamp.setXIndent(200);

textStamp.setYIndent(100);

textStamp.setRotate(Rotation.None);

textStamp.getTextState().setFont( FontRepository.findFont("Arial"));

textStamp.getTextState().setFontSize(34.0F);

textStamp.getTextState().setFontStyle(FontStyles.Bold);

textStamp.getTextState().setFontStyle(FontStyles.Italic);

textStamp.getTextState().setForegroundColor(Color.getPink());

TextStamp textStamp1= new TextStamp("合同水印");

textStamp1.setBackground(true);

textStamp1.setXIndent(200);

textStamp1.setYIndent(400);

textStamp1.setRotate(Rotation.None);

textStamp1.getTextState().setFont( FontRepository.findFont("Arial"));

textStamp1.getTextState().setFontSize(34.0F);

textStamp1.getTextState().setFontStyle(FontStyles.Bold);

textStamp1.getTextState().setFontStyle(FontStyles.Italic);

textStamp1.getTextState().setForegroundColor(Color.getPink());

TextStamp textStamp2= new TextStamp("合同水印");

textStamp2.setBackground(true);

textStamp2.setXIndent(200);

textStamp2.setYIndent(700);

textStamp2.setRotate(Rotation.None);

textStamp2.getTextState().setFont( FontRepository.findFont("Arial"));

textStamp2.getTextState().setFontSize(34.0F);

textStamp2.getTextState().setFontStyle(FontStyles.Bold);

textStamp2.getTextState().setFontStyle(FontStyles.Italic);

textStamp2.getTextState().setForegroundColor(Color.getPink());

PageCollection pages=pdfDocument.getPages();int size =pages.size();//这里下标从1 开始,切记

for (int i = 0; i < size; i++) {

pages.get_Item(i+ 1).addStamp(textStamp);

pages.get_Item(i+ 1).addStamp(textStamp1);

pages.get_Item(i+ 1).addStamp(textStamp2);

}

pdfDocument.save(path);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值