Combine PDF Files Java Itext Tutorial - Part 2 - Continued

In our previous tutorial, we presented an example that explained how to concatenate multiple PDF documents in Java iText. We used PdfReader object to read an existing PDF file and used PdfCopy to merge the contents into a new PDF document. 

I got an interesting question from the previous blog. The requirement was to insert a blank page between two different PDF portions with some text. To make it simpler, we have to add a new page between the two documents and the combined PDF file should have as many blank pages less one depending on the number of files to be merged. In this post, we will explain how to add this additional content into the joined PDF document, while merging the inputs. Before you get ahead with this example, you may wish to refer to the initial post provided earlier, which was a driving factor for this post.


Between two documents that needs to be joined, we will add a page. To do this, we can use the same PdfCopy class addPage method. This method takes either an imported page or an empty rectangle object. This rectangle object can be passed to the addPage method with a a desired rotation angle. Ok, basics first.

Create a Rectangle object, following the code example below;
          float s=100;
          int rotationangle=0;
          Rectangle RectangleObject= new Rectangle(s,s,rotationangle);
Now, between the For loops that is used for combining PDF documents, we also include some code to add this blank Rectangle object. This is shown below;
                        if (i != (files.length-1)) {
                        copy.addPage(RectangleObject,rotationangle);
                        }
The complete code to merge PDF files and also include a blank page between individual documents is provided below;
import java.io.*;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
public class combinePDFDocuments{  
     public static void main(String[] args){
        try {
          String[] files = { "File1.pdf", "File2.pdf" };
          Document PDFJoinInJava = new Document();
          PdfCopy PDFCombiner = new PdfCopy(PDFJoinInJava, new FileOutputStream("MergedPDFFile.pdf"));
          PDFJoinInJava.open();
          PdfReader ReadInputPDF;
          float s=100;
          int rotationangle=0;
          Rectangle RectangleObject= new Rectangle(s,s,rotationangle);
          int number_of_pages;
          for (int i = 0; i < files.length; i++) {
                  ReadInputPDF = new PdfReader(files[i]);
                  number_of_pages = ReadInputPDF.getNumberOfPages();
                  for (int page = 0; page < number_of_pages; ) {
                          PDFCombiner.addPage(PDFCombiner.getImportedPage(ReadInputPDF, ++page));
                        }
                        /* This steps adds a small blank page between PDF documents that are combined */
                        /* add blank page */
                        if (i != (files.length-1)) {
                        PDFCombiner.addPage(RectangleObject,rotationangle);
                        }
                        /* add blank page */
          }
          PDFJoinInJava.close();
        }
        catch (Exception i)
        {
            System.out.println(i);
        }
    }
}
When you run this example, you will get a small square blank page between the two PDF documents. You can increase the size of that blank page by increasing the values passed to the constructor of the Rectangle object.

No comments:

Post a Comment