说明
1、使用Java批量给pdf增加页眉页脚功能,并在每页中间随机位置增加透明水印文字
2、输入文件夹会获取所有pdf文件包含子目录文件
3、输出pdf会保留原有目录结构
代码
1、引入itext7
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext7-core</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>kernel</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>layout</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>io</artifactId>
<version>7.2.0</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>forms</artifactId>
<version>7.2.0</version>
</dependency>
2、代码
package com.duan.demo.pdf;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class AddHeaderFooterIText76 {
public static void main(String[] args) throws IOException {
String srcDir = "D:\\小学PDF\\小学第5部分-全套资料高清无水印整理版(每日更新)"; // 输入文件夹路径
String destDir = "D:\\小学PDF\\小学第5部分-全套资料高清无水印整理版(每日更新)\\output"; // 输出文件夹路径
List<File> pdfFiles = new ArrayList<>();
getAllPDFFiles(new File(srcDir), pdfFiles);
for (File pdfFile : pdfFiles) {
String relativePath = pdfFile.getAbsolutePath().substring(srcDir.length() + 1);
String destPath = destDir + File.separator + relativePath;
createParentDirs(destPath);
addHeaderFooter(pdfFile.getAbsolutePath(), destPath);
System.out.println(destPath);
}
}
private static void getAllPDFFiles(File dir, List<File> pdfFiles) {
if (dir.isDirectory()) {
for (File file : dir.listFiles()) {
if (file.isDirectory()) {
getAllPDFFiles(file, pdfFiles);
} else if (file.getName().endsWith(".pdf")) {
pdfFiles.add(file);
}
}
}
}
private static void createParentDirs(String filePath) throws IOException {
File file = new File(filePath);
File parent = file.getParentFile();
if (!parent.exists()) {
Files.createDirectories(Paths.get(parent.getAbsolutePath()));
}
}
private static void addHeaderFooter(String src, String dest) throws IOException {
PdfReader pdfReader = new PdfReader(src);
pdfReader.setUnethicalReading(true);
PdfDocument pdfDoc = new PdfDocument(pdfReader, new PdfWriter(dest));
Document document = new Document(pdfDoc);
//该路径为中文字体路径
String fontZW = "C:\\Users\\机械革命\\Desktop\\NotoSansCJK-Regular\\NotoSansCJK-Regular.otf";
// 使用支持中文的字体
PdfFont font = PdfFontFactory.createFont(fontZW, PdfEncodings.IDENTITY_H);
int numberOfPages = pdfDoc.getNumberOfPages();
Random random = new Random();
for (int i = 1; i <= numberOfPages; i++) {
addHeaderFooterAndRandomText(document, pdfDoc, i, font, random);
}
document.close();
}
private static void addHeaderFooterAndRandomText(Document document, PdfDocument pdfDoc, int pageNum, PdfFont font, Random random) {
PdfPage page = pdfDoc.getPage(pageNum);
float pageWidth = page.getPageSize().getWidth();
float pageHeight = page.getPageSize().getHeight();
// 包含中文的文本
String text = "+V: duanduan981201获取更多无水印资料";
// 添加页眉
Paragraph header = new Paragraph(text)
.setFont(font)
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(12);
document.showTextAligned(header, pageWidth / 2, pageHeight - 30, pageNum, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
// 添加页脚
Paragraph footer = new Paragraph(text)
.setFont(font)
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(12);
document.showTextAligned(footer, pageWidth / 2, 30, pageNum, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
// 添加随机位置的文本
float randomHeight = 30 + random.nextFloat() * (pageHeight - 60); // 避免与页眉页脚重叠
float opacity = 0.1f + random.nextFloat() * 0.9f; // 随机透明度,范围为0.1到1.0
Paragraph randomText = new Paragraph(text)
.setFont(font)
.setTextAlignment(TextAlignment.CENTER)
.setFontSize(12)
.setFontColor(new DeviceRgb(0, 0, 0), opacity);
document.showTextAligned(randomText, pageWidth / 2, randomHeight, pageNum, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
}
}