java itextpdf5.5.6 删除水印
时间: 2023-09-28 07:05:27 浏览: 183
在 iText PDF 中删除水印有两种方法:
1. 通过遍历页面内容并删除水印。
```
PdfReader reader = new PdfReader(inputFilePath);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFilePath));
int pageCount = reader.getNumberOfPages();
for (int i = 1; i <= pageCount; i++) {
PdfContentByte cb = stamper.getOverContent(i);
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
cleanUpLocations.add(new PdfCleanUpLocation(i, new Rectangle(0, 0, 500, 500), BaseColor.WHITE));
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
cleaner.cleanUp();
cb.setLiteral("Q\nq");
}
stamper.close();
reader.close();
```
2. 通过使用 PdfCleanUpProcessor 删除水印。
```
PdfReader reader = new PdfReader(inputFilePath);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFilePath));
int pageCount = reader.getNumberOfPages();
List<PdfCleanUpLocation> cleanUpLocations = new ArrayList<>();
for (int i = 1; i <= pageCount; i++) {
cleanUpLocations.add(new PdfCleanUpLocation(i, new Rectangle(0, 0, 500, 500), BaseColor.WHITE));
}
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
cleaner.cleanUp();
stamper.close();
reader.close();
```
其中,`inputFilePath` 和 `outputFilePath` 分别是输入文件和输出文件的路径。需要注意的是,iText PDF 5.5.6 已经过时,建议使用最新版本的 iText。
阅读全文