Join Two or More PDFs

IronPDF can combine the contents of multiple PDF documents. A common use for this feature is attaching cover pages to new or existing PDFs. Other possible uses include bundling related documents together into one file for convenient printing and transport.

The code example above uses the PdfDocument.merge method on two PdfDocument objects that were both rendered from HTML markup. The result of the invocation is a new PdfDocument produced by appending the content of the latter PdfDocument to the end of the former one.

To combine more than three PDF documents, first create a list containing the required PdfDocument objects, and then pass the list as a single argument to the PdfDocument.merge method:

import java.util.ArrayList;
import java.util.List;
import com.yourlibrary.PdfDocument; // Replace with the actual package name

// Example code to merge multiple PDF documents using IronPDF
public class MergePDFsExample {

    public static void main(String[] args) {
        // Create a list to store PdfDocument objects
        List<PdfDocument> pdfs = new ArrayList<>();

        // Assume pdfA, pdfB, pdfC, pdfD are previously created or loaded PdfDocument objects
        PdfDocument pdfA = new PdfDocument("path/to/pdfA.pdf");
        PdfDocument pdfB = new PdfDocument("path/to/pdfB.pdf");
        PdfDocument pdfC = new PdfDocument("path/to/pdfC.pdf");
        PdfDocument pdfD = new PdfDocument("path/to/pdfD.pdf");

        // Add PdfDocument objects to the list
        pdfs.add(pdfA);
        pdfs.add(pdfB);
        pdfs.add(pdfC);
        pdfs.add(pdfD);

        // Merge the PdfDocuments in the list into a single PdfDocument
        PdfDocument merged = PdfDocument.merge(pdfs);

        // Save the merged PDF to a file
        merged.saveAs("path/to/merged.pdf");
    }
}
import java.util.ArrayList;
import java.util.List;
import com.yourlibrary.PdfDocument; // Replace with the actual package name

// Example code to merge multiple PDF documents using IronPDF
public class MergePDFsExample {

    public static void main(String[] args) {
        // Create a list to store PdfDocument objects
        List<PdfDocument> pdfs = new ArrayList<>();

        // Assume pdfA, pdfB, pdfC, pdfD are previously created or loaded PdfDocument objects
        PdfDocument pdfA = new PdfDocument("path/to/pdfA.pdf");
        PdfDocument pdfB = new PdfDocument("path/to/pdfB.pdf");
        PdfDocument pdfC = new PdfDocument("path/to/pdfC.pdf");
        PdfDocument pdfD = new PdfDocument("path/to/pdfD.pdf");

        // Add PdfDocument objects to the list
        pdfs.add(pdfA);
        pdfs.add(pdfB);
        pdfs.add(pdfC);
        pdfs.add(pdfD);

        // Merge the PdfDocuments in the list into a single PdfDocument
        PdfDocument merged = PdfDocument.merge(pdfs);

        // Save the merged PDF to a file
        merged.saveAs("path/to/merged.pdf");
    }
}
JAVA

For more information on how IronPDF works, explore the IronPDF Documentation.