SoFunction
Updated on 2025-03-08

Summary of three common ways of merging PDF documents in Java

Preface

Three common ways to merge pdfs in Java

Merge PDF

Merge pdf

apache pdfbox

 <dependency>
     <groupId></groupId>
     <artifactId>pdfbox</artifactId>
     <version>2.0.26</version>
 </dependency>

Code Example

// Add files to be mergedfor ( sourcePdf : fileRefers) {
   long time1 = ();
   ("Start adding{}, size:{}MB, path:{}", (++index),
   (()).length() / 1024 / 1024, ());
   try (PDDocument document = ((()), ());
        ) {
            // Page outline (level 1)            String newName = (()).getName();
            newName = (newName).replace("." + (newName), "");
            PdfBoxBookmark boxData = new PdfBoxBookmark(newName, totalPage);

            PDDocumentOutline outline = ().getDocumentOutline();
            if (outline != null) {
                buildBookMark(outline, boxData, totalPage);
            }
            /
            (boxData);

            (());

            // Update the total page number            totalPage += ();
        } catch (IOException e) {
            ();
            ("mergepdffail:{}",e);
            throw new BaseException("The file does not exist:" + ());
        }
        ("End Add,After adding,total{}Page:{},Expenditure:{}Second",totalPage,
                (), (() - time1) / 1000);
    }

    // Set the path of the merged pdf file    (destFilePath);

    // Merge pdf    try {
        (.OPTIMIZE_RESOURCES_MODE);
        (());
    } catch (IOException e) {
        ();
        throw new BaseException("Exception occurred in the merge");
    }

e-iceblue

<dependency>
    <groupId>e-iceblue</groupId>
    <artifactId></artifactId>
    <version>9.5.6</version>
</dependency>

Code example:

String[] files = new String[] {
                "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\",
                "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\"};

//Merge documents and return an object of PdfDocumentBase
PdfDocumentBase pdf = (files);

//Save the result to a PDF file
("C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\", );

itextpdf

<dependency>
    <groupId></groupId>
    <artifactId>itextpdf</artifactId>
    <version>5.5.13.4</version>
</dependency>

Code example:

String[] pdfs = new String[] {
                "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\",
                "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\"};

String outputPdf = "C:\\Users\\test\\Desktop\\tmp\\001\\20241014-001\\"; // The merged PDF file
try {
    Document document = new Document();
    PdfCopy copy = new PdfCopy(document, new FileOutputStream(outputPdf));
    ();

    for (String pdf : pdfs) {
        PdfReader reader = new PdfReader(pdf);
        for (int i = 1; i &lt;= (); i++) {
            ();
            ((reader, i));
        }
        ();
    }

    ();
    ("PDFs merged successfully.");
} catch (Exception e) {
    ();
}

Summarize

pdfbox and itextpdf are free, e-iceblue is charged and expensive, and there will be a watermark without authorization. Although e-iceblue is free, it has a page limit.

The pdfbox and e-iceblue have low tolerance to PDF documents. If the pdf document is generated or edited through three-party software (there may be some problems, but if the browser is opened or WPS is opened, the document will be displayed to the point of fault). However, itextpdf will perform fault-tolerant repair and merge successfully.

Therefore, I personally recommend using itextpdf.

This is the end of this article about the three common ways of merging PDF documents in Java. For more information about Java merging PDF documents, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!