Merge and split PDF documents in Java

When dealing with PDF documents, we can merge any group of several different PDF files or split a file into multiple sub files. This is convenient for document storage and management. The following will introduce the specific methods of PDF merging and splitting through Java program code.

tool

Note: compared with the previous version 1.1.0, version 2.0.0 has greatly improved its functions. It supports all the functions of the charging version. It is very practical for processing PDF documents through Java programming.

jar file import:

Step 1: create a new folder in the Java program to be named lib. After downloading the installation package, unzip it, and copy the Spire.Pdf.jar and Spire.Common.jar files in the subfolder lib under the unzipped folder to the new folder, as shown below:

Step 2: after creating the folder, reference two files: select the two jar files, right-click, and select "Build Path" – "Add to Build Path".

[example 1] merge PDF documents

import com.spire.pdf.*;
import java.io.*;

public class Merge2 {
    public static void main(String[] args) throws Exception {
        
        String outputFile = "output/mergeFilesByStream.pdf";
        FileInputStream stream1 = new FileInputStream(new File("sample1.pdf"));
        FileInputStream stream2 = new FileInputStream(new File("sample2.pdf"));
        FileInputStream stream3 = new FileInputStream(new File("sample3.pdf"));
        //Load PDF Example document
        InputStream[] streams = new FileInputStream[]{stream1, stream2, stream3};

        //merge PDF File
        PdfDocumentBase doc = PdfDocument.mergeFiles(streams);

        //Save document
        doc.save(outputFile);
        doc.close();
    }
}

Before merger:

After the merger:

[example 2] split PDF document

There are two situations.

Test documentation:

1. Separate by each page

import com.spire.pdf.*;

public class SplitPDF1 {
    public static void main(String[] args)
    {
    //Load the PDF File
    PdfDocument doc = new PdfDocument();
    doc.loadFromFile("test.pdf");

    //Calling method split()take PDF Documents are split into separate documents by page
    doc.split("output/splitDocument-{0}.pdf", 0);
    doc.close();
    }
}

Split result:

2. Split by specified page range

import com.spire.pdf.*;
import com.spire.pdf.graphics.PdfMargins;

import java.awt.geom.Point2D;

public class SplitPDF2 {
    public static void main(String[] args)
    {

        //Load the PDF File
        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("test.pdf");

        //New 1st PDF Document 1
        PdfDocument newpdf1 = new PdfDocument();
        PdfPageBase page;

        //Will the original PDF Split pages 1 and 2 of the document and save to newpdf1
        for(int i = 0;i<2;i++)
        {
            page = newpdf1.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }
        newpdf1.saveToFile("split/result1.pdf");

        //New 2nd PDF File
        PdfDocument newpdf2 = new PdfDocument();

        //Will the original PDF Page 3 and 4 of the document are split and saved to newpdf2
        for(int i = 2;i<4;i++)
        {
            page = newpdf2.getPages().add(doc.getPages().get(i).getSize(), new PdfMargins(0));
            doc.getPages().get(i).createTemplate().draw(page, new Point2D.Float(0,0));
        }
        newpdf2.saveToFile("split/result2.pdf");
    }
}

Split result:

(end of this paper)

Reprint please indicate the source!

Keywords: Java Programming

Added by poppy on Sun, 08 Dec 2019 21:02:11 +0200