Java PDFStamper 是一個用于處理 PDF 文檔的庫,但它已經被廢棄,現在推薦使用 iText 或 Apache PDFBox 等其他庫。這里我將為您提供一些關于使用 iText 庫的建議來解決常見問題。
添加 iText 依賴項:
對于 Maven 項目,請在 pom.xml 文件中添加以下依賴項:
對于 Gradle 項目,請在 build.gradle 文件中添加以下依賴項:
implementation 'com.itextpdf:itext7-core:7.1.16'
合并 PDF 文件:
import com.itextpdf.kernel.pdf.*;
public void mergePdfFiles(List<String> inputFiles, String outputFile) {
PdfWriter writer = new PdfWriter(outputFile);
PdfDocument pdfDocument = new PdfDocument(writer);
for (String inputFile : inputFiles) {
PdfReader reader = new PdfReader(inputFile);
PdfDocument sourceDocument = new PdfDocument(reader);
sourceDocument.copyPagesTo(1, sourceDocument.getNumberOfPages(), pdfDocument);
sourceDocument.close();
}
pdfDocument.close();
}
分割 PDF 文件:
import com.itextpdf.kernel.pdf.*;
public void splitPdfFile(String inputFile, String outputPath, int startPage, int endPage) {
PdfReader reader = new PdfReader(inputFile);
PdfDocument pdfDocument = new PdfDocument(reader);
int numberOfPages = pdfDocument.getNumberOfPages();
for (int i = startPage; i <= endPage && i <= numberOfPages; i++) {
PdfWriter writer = new PdfWriter(outputPath + "/page_" + i + ".pdf");
PdfDocument splitDocument = new PdfDocument(writer);
pdfDocument.copyPagesTo(i, i, splitDocument);
splitDocument.close();
}
pdfDocument.close();
}
添加水印:
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
public void addWatermark(String inputFile, String outputFile, String watermarkText) {
PdfReader reader = new PdfReader(inputFile);
PdfWriter writer = new PdfWriter(outputFile);
PdfDocument pdfDocument = new PdfDocument(reader, writer);
Document document = new Document(pdfDocument);
for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {
Paragraph watermark = new Paragraph(watermarkText)
.setFontSize(48)
.setFontColor(ColorConstants.RED)
.setTextAlignment(TextAlignment.CENTER);
document.showTextAligned(watermark, 297.5f, 421, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 45);
}
document.close();
}
添加頁眉和頁腳:
import com.itextpdf.kernel.pdf.*;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
public void addHeaderAndFooter(String inputFile, String outputFile, String headerText, String footerText) {
PdfReader reader = new PdfReader(inputFile);
PdfWriter writer = new PdfWriter(outputFile);
PdfDocument pdfDocument = new PdfDocument(reader, writer);
Document document = new Document(pdfDocument);
for (int i = 1; i <= pdfDocument.getNumberOfPages(); i++) {
// Add header
Paragraph header = new Paragraph(headerText)
.setFontSize(12)
.setTextAlignment(TextAlignment.CENTER);
document.showTextAligned(header, 297.5f, 806, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
// Add footer
Paragraph footer = new Paragraph(footerText + " | Page " + i)
.setFontSize(12)
.setTextAlignment(TextAlignment.CENTER);
document.showTextAligned(footer, 297.5f, 55, i, TextAlignment.CENTER, VerticalAlignment.MIDDLE, 0);
}
document.close();
}
這些示例應該可以幫助您解決使用 Java PDFStamper 時遇到的常見問題。如果您需要更多功能,請查看 iText 官方文檔:https://itextpdf.com/en/resources/documentation