Merging pdf documents with java

A Java 8 code snippet to merge to pdf documents into a single file:

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.*;

public class PdfMerger {

    public static void main(String[] args) throws Exception {

        File[] inFiles = new File[] {
            new File("1.pdf"),
            new File("2.pdf")
        };

        File outFile = new File("out.pdf");

        Document document = new Document();

        try (OutputStream outputStream = new FileOutputStream(outFile)) {
            PdfWriter writer = PdfWriter.getInstance(document, outputStream);
            document.open();
            PdfContentByte cb = writer.getDirectContent();

            for (File inFile : inFiles) {
                PdfReader reader = new PdfReader(new FileInputStream(inFile));
                for (int i = 1; i <= reader.getNumberOfPages(); i++) {
                    document.newPage();
                    PdfImportedPage page = writer.getImportedPage(reader, i);
                    cb.addTemplate(page, 0, 0);
                }
            }
            document.close();
        }
    }

}