温馨提示×

如何解决Java PDFStamper的常见问题

小樊
101
2024-09-04 12:40:48
栏目: 编程语言

Java PDFStamper 是一个用于处理 PDF 文档的库,但它已经被废弃,现在推荐使用 iText 或 Apache PDFBox 等其他库。这里我将为您提供一些关于使用 iText 库的建议来解决常见问题。

  1. 添加 iText 依赖项:

    对于 Maven 项目,请在 pom.xml 文件中添加以下依赖项:

com.itextpdf itext7-core 7.1.16 ```

对于 Gradle 项目,请在 build.gradle 文件中添加以下依赖项:

implementation 'com.itextpdf:itext7-core:7.1.16'
  1. 合并 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();
    }
    
  2. 分割 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();
    }
    
  3. 添加水印:

    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();
    }
    
  4. 添加页眉和页脚:

    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

0