/*
 * pdfmerge - overlay two pdf pages
 *
 * Copyright 2002 by Bruno Lowagie
 * Copyright 2004 by Jochen Eisinger <jochen@penguin-breeder.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * The code is based on Bruno's code from the iText tutorial
 * http://www.lowagie.com/iText/tutorial/
 *
 */
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;

public class pdfmerge {

    public static void main(String[] args) {
        try {
            // we create a reader for a certain document
            PdfReader reader = new PdfReader(args[0]);
	    PdfReader overlay = new PdfReader(args[1]);
            // we retrieve the size of the first page
            Rectangle psize = reader.getPageSize(1);
            
            // step 1: creation of a document-object
            Document document = new Document(psize);
            // step 2: we create a writer that listens to the document
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(args[2]));
            // step 3: we open the document
            document.open();
            // step 4: we add content
            PdfContentByte cb = writer.getDirectContent();
	    // step 5: add pages
	    document.newPage();
	    PdfImportedPage page1 = writer.getImportedPage(reader, 1);
	    cb.addTemplate(page1, 0, 0);
	    PdfImportedPage page2 = writer.getImportedPage(overlay, 1);
	    cb.addTemplate(page2, 0, 0);
	    
            // step 6: we close the document
            document.close();
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
}

