2

我正在使用以下代码合并两个 pdf:

File firstPdfFile = new File("firstPdf.pdf"); 
File secondPdfFile = new File("secondPdf.pdf"); 
PDFMergerUtility merger = new PDFMergerUtility(); 
merger.addSource(firstPdfFile);     
merger.addSource(secondPdfFile); 
String pdfPath = "PdfFile.pdf"; 
OutputStream bout2 = new BufferedOutputStream(new FileOutputStream(pdfPath)); 
merger.setDestinationStream(bout2); 
merger.mergeDocuments();    
File pdfFile = new File(pdfPath);

我正在正确获取合并的 pdf,但我想在此 pdf 文件中添加页码。

4

2 回答 2

1

试试这个代码。

    File firstPdfFile = new File("firstPdf.pdf");
    File secondPdfFile = new File("firstPdf.pdf");
    PDFMergerUtility merger = new PDFMergerUtility();
    merger.addSource(firstPdfFile);
    merger.addSource(secondPdfFile);
    String pdfPath = "PdfFile.pdf";
    OutputStream bout2 = new BufferedOutputStream(new FileOutputStream(pdfPath));
    merger.setDestinationStream(bout2);
    merger.mergeDocuments();

    PDDocument doc = null;
    try {
        URL file = new URL("file:///PdfFile.pdf");
        doc = PDDocument.load(file);

        List<?> allPages = doc.getDocumentCatalog().getAllPages();
        PDFont font = PDType1Font.HELVETICA_BOLD;
        float fontSize = 36.0f;
        for (int i = 0; i < allPages.size(); i++) {
            PDPage page = (PDPage) allPages.get(i);
            PDPageContentStream footercontentStream = new PDPageContentStream(doc, page, true, true);
            footercontentStream.beginText();
            footercontentStream.setFont(font, fontSize);
            footercontentStream.moveTextPositionByAmount((PDPage.PAGE_SIZE_A4.getUpperRightX() / 2), (PDPage.PAGE_SIZE_A4.getLowerLeftY()));
            footercontentStream.drawString(String.valueOf(i + 1));
            footercontentStream.endText();
            footercontentStream.close();
        }
        doc.save("PdfFile.pdf");
    } finally {
        if (doc != null) {
            doc.close();
        }
    }
于 2014-01-16T09:40:07.097 回答
1

试试下面的 PDFBox 2.0 代码

public class PageNumberExample {

final boolean isCompress = false;
final boolean isContextReset = true;

public static void main(String[] args) throws IOException {
    new PageNumberExample().addPageNumber("merged PDF path");
}

public void addPageNumber(String pdfPath) throws IOException {
    File mergePpdfFile = new File(pdfPath);
    PDDocument document = PDDocument.load(mergePpdfFile);
    int totalPage = document.getNumberOfPages();
    for(int i=0; i<totalPage; i++) {
        PDPage page = document.getPage(i);

        PDPageContentStream stream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, isCompress, isContextReset);

        stream.setNonStrokingColor(Color.BLACK);
        stream.beginText();
        stream.setFont(PDType1Font.COURIER, 10);
        stream.newLineAtOffset(100, 100); //Set position where you want to print page number.
        stream.showText("Page " + (i+1) + " of " + totalPage);
        stream.endText();
        stream.close();
    }
    document.save(pdfPath);
    document.close();
}
}
于 2019-12-18T04:14:03.583 回答