Wednesday, 24 June 2020

Merge PDF Documents in Java

In order to better manage and store documents, it is inevitable to combine multiple PDF documents into one document. Here I’ll introduce two ways to merge PDF documents using Free Spire.PDF for Java.

Before beginning, you need to download the required tool Free Spire.PDF for Java from the link, unzip it and import Spire.Pdf.jar located in the “lib” folder into your IDEA. Of course, if you use maven, you need to add the following code to your project’s pom.xml file.

<repositories> 

        <repository> 

            <id>com.e-iceblue</id> 

            <name>e-iceblue</name> 

            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url> 

        </repository> 

</repositories> 

<dependencies> 

    <dependency> 

        <groupId>e-iceblue</groupId> 

        <artifactId>spire.pdf.free</artifactId> 

        <version>2.6.3</version> 

    </dependency> 

</dependencies> 

Using the code

Method 1

Load three PDF documents and select the first PdfDocument for the purpose of merging the second and third PDF file to it.

import com.spire.pdf.PdfDocument;

public class MergePDF1 {
   
public static void main(String[] args) {
       
//Load PDF files
       
String[] files = new String[]
                {
                       
"C:\\Users\\Test1\\Desktop\\Sample01.pdf",
                       
"C:\\Users\\Test1\\Desktop\\Sample02.pdf",
                       
"C:\\Users\\Test1\\Desktop\\Sample03.pdf",
                };

       
//Open pdf documents
       
PdfDocument[] docs = new PdfDocument[files.length];
        PdfDocument doc =
new PdfDocument();
       
for (int i = 0; i < files.length; i++) {
            docs[i] =
new PdfDocument();
            docs[i].loadFromFile(files[i]);
        }
       
//Append document
       
docs[0].appendPage(docs[1]);

       
//import pages
       
for (int i = 0; i < docs[2].getPages().getCount(); i = i + 2) {
            docs[
0].insertPage(docs[2], i);
        }

       
// Save pdf file.
       
docs[0].saveToFile("output/MergeDocument.pdf");
        doc.close();
    }
}

Method 2

Input the three PDF documents by stream and then use mergeFiles(streams)methods to merge the PDF documents into one PDF document.

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

public class MergePDF2 {
   
public static void main(String[] args) throws FileNotFoundException {
       
//Load PDF files
       
FileInputStream stream1 = new FileInputStream(new File("C:\\Users\\Test1\\Desktop\\Sample01.pdf"));
        FileInputStream stream2 =
new FileInputStream(new File("C:\\Users\\Test1\\Desktop\\Sample02.pdf"));
        FileInputStream stream3 =
new FileInputStream(new File("C:\\Users\\Test1\\Desktop\\Sample03.pdf"));
     
               //Input PDF files by stream
       
InputStream[] streams = new FileInputStream[]{stream1, stream2, stream3};

       
//Merge files by stream
       
PdfDocumentBase doc = PdfDocument.mergeFiles(streams);
       
//Save the file
       
doc.save("output/mergeFilesByStream.pdf");
        doc.close();
    }
}

Output


No comments:

Post a Comment

Change PDF Versions in Java

In daily work, you might need to change the version of a PDF document you have in order to ensure compatibility with another version which a...