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;}

完整的项目代码

itext7 对pdf文件添加表格水印相关推荐

  1. java实现给PDF文件添加图片水印,java实现给PDF文件添加文字水印

    接上一篇,pdf跟tif 是一起做的 java实现 1.给PDF文件添加图片水印: public static void waterMark1(String inputFile,String outp ...

  2. 如何批量给pdf文件添加文字水印?

    工作中我们会给重要的办公文件文件水印,给文件加上公司的名称等,这样可以有效防止文件内容被别人盗用抄袭,其中就包括word.Excel.PPT.图片.PDF等文件.PDF文件由于其特殊性,越来越成为最常 ...

  3. PDF文件JAVA去水印源码,给pdf文件添加防伪水印logo(附工程源码下载)

    pdf添加水印logo这种需求场景确实很少,有些时候一些销售单据生成pdf添加一个水印logo,做一个简单的防伪效果,虽然实际上并没有太大作用,但是产品经理说要,巴拉巴拉--省略一万字. 下面将源码分 ...

  4. PDF文件添加文字水印的方法

    今天给大家带来的是PDF修改软件介绍,介绍这个PDF修改软件是因为一件小事.我有一个朋友,前些天和我抱怨一件事,说他从网上下载下来的好多文件,都只能阅读无法编辑.我一听,就明白了,他下载的文件格式一定 ...

  5. 使用itext7在PDF文件中的指定文字位置添加电子签名图片技术记录

    使用itext7在PDF文件中的指定文字位置添加电子签名图片 文章目录 使用itext7在PDF文件中的指定文字位置添加电子签名图片 一.技术使用背景 二.使用步骤 1.引入依赖 2.具体代码 2.控 ...

  6. Android使用iText7生成PDF文件

    一:添加依赖 implementation 'com.itextpdf:itext7-core:7.1.13' 二:清单文件AndroidManifest.xml 添加权限 <uses-perm ...

  7. java 取pdf表格内容数据_Java 在PDF中添加表格

    本文将介绍通过Java编程在PDF文档中添加表格的方法.添加表格时,可设置表格边框.单元格对齐方式.单元格背景色.单元格合并.插入图片.设置行高.列宽.字体.字号等. 使用工具:Free Spire. ...

  8. pdf文件怎么去除水印,pdf去除水印方法介绍

    pdf文件怎么去除水印?pdf文件是目前被普遍使用的一种文件格式,我们在工作中也会时常需要下载pdf文件进行使用,当我们遇到被添加了水印的pdf文件时,该如何将水印去除呢?如果屏幕前的你不知道的话就看 ...

  9. PDF怎么添加文字水印

    有时候自己辛辛苦苦做的文件被别人直接拿去使用就会有点不舒服,如果我们把文件中添加上水印呢?今天就以PDF文件为例子来教大家如果给PDF文件添加水印,一起来看看吧!  方法一.迅捷PDF编辑器https ...

最新文章

  1. R语言ggplot2可视化更改轴上数字的格式(显示格式)实战
  2. EF架构~为IEnumerable接口添加增删查等操作,原因是IEnumerable导航属性更放心
  3. mc服务器村民交易修改,【原创】【教程】MCPE自定义村民交易内容
  4. 【转】C语言中DEFINE简介及多行宏定义
  5. 自然哲学的数学原理_物理起源点,牛顿《自然哲学的数学原理》
  6. Java设计模式之七大结构型模式
  7. 眼图 非差分线_TMDS181 的眼图振铃问题
  8. services.xml应该放在项目的哪里_新轮胎应该放在前轮还是后轮?
  9. java 网格包,求大神解答:JAVA网格包布局管理器小程序问题
  10. 37.伪造参数错误的ICMP数据包
  11. 7系列主板 规格对比
  12. 数据的预处理——平滑处理
  13. 英语语法---读音规则
  14. PL330 DMAC笔记(2) - DMAC接口,状态机,初始化,APB slave接口
  15. MapReduce各个执行阶段
  16. coc机器人苹果_警察机器人绳索英雄
  17. 优秀领导者必读的8本管理学书籍
  18. 247 中心对称数 II
  19. 添加友情链接获取CF币
  20. Lambda表达式和Stream流

热门文章

  1. c语言打擂法教学案例,错误案例教学法在C语言教学的应用论文
  2. django搭建 - windos 服务器配置与设置
  3. vue.js中获取当前日期的前n天或者后n天(亲测成功)
  4. java between_Java Period between()用法及代码示例
  5. 清华EMBA课程系列思考之三 -- 中国经济与金融
  6. 测试工作——如何区别一个 App 是 Native App, Web App 还是 Hybrid app?
  7. 为什么我的VS开发环境没有:解决方案资源管理器
  8. tensorflow建立模型失败The following previous layers were accessed without issue
  9. 环岛c语言程序,石器时代萨伊那斯环岛活动C路线任务攻略
  10. 大数据在发电厂的应用_大数据在智能火力发电厂的应用