itext7 对pdf文件添加表格水印

1、pom.xml中添加依赖

<dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext7-core</artifactId>
            <version>7.1.11</version>
            <type>pom</type>
</dependency>

2、表格水印的初始化

// 字体
PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Color blue = new DeviceRgb(0,0,0);
// 设置边框
Border border = new SolidBorder(ColorConstants.BLACK,2);
// 初始化表格列数
Table table = new Table(2);
// 设置字体
table.setFontSize(16);
// 设置表格的验收
table.setFontColor(blue);

table.addCell(new Cell(1,2).add(new Paragraph(String.valueOf("文字")).setFont(sysFont)).setBorder(border));
table.addCell(new Cell().add(new Paragraph(String.valueOf("图片")).setFont(sysFont)).setBorder(border));
// 添加图片
Image cellimg = new Image(ImageDataFactory.create("/Users/leixia/Desktop/图片.png")).setWidth(30).setHeight(30);
table.addCell(new Cell().add(cellimg).setBorder(border));

3、读取pdf文件并对文件的每页添加水印


// 原始PDF地址
String inputFilePath = "/Users/leixia/Desktop/原始版.pdf";
// 加水印后的文件地址
String outputFileUrl = "/Users/leixia/Desktop/测试版.pdf";
File inputFile = new File(inputFilePath);
File outputFile = new File(outputFileUrl);
pdfReader = new PdfReader(inputFile);
File parentFile = outputFile.getParentFile();
// 判断父文件夹是否存在
if (!parentFile.exists()) {
	parentFile.mkdirs();
}
if(!outputFile.exists()){
	outputFile.createNewFile();
}
pdfWriter = new PdfWriter(outputFile);
PdfDocument pdf = new PdfDocument(pdfReader, pdfWriter);
doc = new Document(pdf);
// 获取pdf的页数
int numberOfPages = pdf.getNumberOfPages();
// 每页都添加table水印
for (int i = 1; i <= numberOfPages; i++) {
	doc.add(table.setBold().setFixedPosition(i,100,250,210));
}



完整版代码如下:

public Map<String, String> addWaterMark() {
		Map<String,String> returnMap = new HashMap<String,String>();
		returnMap.put("status", "");
		returnMap.put("msg", "");
		Document doc = null;
		PdfWriter pdfWriter = null;
		PdfReader pdfReader = null;
		try {
			// 字体
			PdfFont sysFont = PdfFontFactory.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
			Color blue = new DeviceRgb(0,0,0);
			// 设置边框
			Border border = new SolidBorder(ColorConstants.BLACK,2);
			// 初始化表格列数
			Table table = new Table(2);
			// 设置字体
			table.setFontSize(16);
			// 设置表格的验收
			table.setFontColor(blue);

			table.addCell(new Cell(1,2).add(new Paragraph(String.valueOf("文字")).setFont(sysFont)).setBorder(border));
			table.addCell(new Cell().add(new Paragraph(String.valueOf("图片")).setFont(sysFont)).setBorder(border));
			// 添加图片
			Image cellimg = new Image(ImageDataFactory.create("/Users/leixia/Desktop/图片.png")).setWidth(30).setHeight(30);
			table.addCell(new Cell().add(cellimg).setBorder(border));
			// 原始PDF地址
			String inputFilePath = "/Users/leixia/Desktop/原始版.pdf";
			// 加水印后的文件地址
			String outputFileUrl = "/Users/leixia/Desktop/测试版.pdf";
			File inputFile = new File(inputFilePath);
			File outputFile = new File(outputFileUrl);
			pdfReader = new PdfReader(inputFile);
			File parentFile = outputFile.getParentFile();
			// 判断父文件夹是否存在
			if (!parentFile.exists()) {
				parentFile.mkdirs();
			}
			if(!outputFile.exists()){
				outputFile.createNewFile();
			}
			pdfWriter = new PdfWriter(outputFile);
			PdfDocument pdf = new PdfDocument(pdfReader, pdfWriter);
			doc = new Document(pdf);
			// 获取pdf的页数
			int numberOfPages = pdf.getNumberOfPages();
			// 每页都添加table水印
			for (int i = 1; i <= numberOfPages; i++) {
				doc.add(table.setBold().setFixedPosition(i,100,250,210));
			}
			returnMap.put("status", "success");
			returnMap.put("msg", "pdf添加水印完成");
		} catch (IOException e) {
			e.printStackTrace();
			returnMap.put("status", "error");
			returnMap.put("msg", "pdf添加水印出错");
			e.printStackTrace();
			returnMap.put("status", "error");
			returnMap.put("msg", e.getMessage());
		}finally{
			if(doc != null){
				doc.close();
			}
			try {
				if(pdfWriter != null){
					pdfWriter.close();
				}
				if(pdfReader != null){
					pdfReader.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return returnMap;
	}

完整的项目代码

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iText 是一个开源的 Java PDF 库,可以用来创建、操作和处理 PDF 文件。以下是基于 iText 生成 PDF 文件的简单示例: 1. 添加 iText 依赖 在项目的 pom.xml 文件添加如下依赖: ```xml <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> ``` 2. 创建 PDF 文件 使用 iText 创建 PDF 文件的步骤如下: ```java // 创建 PDF 文档对象 Document document = new Document(); // 创建 PDF 输出流 PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); // 打开文档 document.open(); // 添加内容 document.add(new Paragraph("Hello, World!")); // 关闭文档 document.close(); ``` 在上面的代码中,首先创建了一个 Document 对象,然后使用 PdfWriter 创建一个 PDF 输出流,将输出流与 Document 对象关联。接下来打开文档,添加内容,最后关闭文档。 3. 添加表格和图片 除了文本,iText 还支持添加表格和图片等元素。以下是添加表格和图片的示例代码: ```java // 创建 PDF 文档对象 Document document = new Document(); // 创建 PDF 输出流 PdfWriter.getInstance(document, new FileOutputStream("output.pdf")); // 打开文档 document.open(); // 添加表格 PdfPTable table = new PdfPTable(3); table.addCell("Name"); table.addCell("Age"); table.addCell("Gender"); table.addCell("John Doe"); table.addCell("30"); table.addCell("Male"); document.add(table); // 添加图片 Image image = Image.getInstance("image.png"); document.add(image); // 关闭文档 document.close(); ``` 在上面的代码中,首先创建了一个 PdfPTable 对象,添加了表头和数据,然后将表格添加PDF 文档中。接下来添加了一张图片,最后关闭文档。 以上是基于 iText 生成 PDF 文件的简单示例,iText 还支持更丰富的 PDF 操作,如添加书签、水印、表单等功能。详细的示例可以参考 iText 官方文档。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值