MergingTwoPDFToOne.java Source code

Java tutorial

Introduction

Here is the source code for MergingTwoPDFToOne.java

Source

import java.io.FileOutputStream;

import com.lowagie.text.Element;
import com.lowagie.text.PageSize;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class MergingTwoPDFToOne {
    public static void main(String[] args) {
        try {
            PdfReader reader = new PdfReader("YourOwnPDF.pdf");

            PdfStamper stamp = new PdfStamper(reader, new FileOutputStream("TwoPDF.pdf"));

            PdfContentByte under;

            PdfReader reader2 = new PdfReader("AnotherPDF.pdf");
            under = stamp.getUnderContent(1);
            under.addTemplate(stamp.getImportedPage(reader2, 1), 1, 0, 0, 1, 0, 0);

            stamp.close();
        } catch (Exception de) {
            de.printStackTrace();
        }
    }
}