/*
 * Created on 26.09.2007
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package pdf;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;

import com.lowagie.text.Document;
import com.lowagie.text.pdf.BadPdfFormatException;
import com.lowagie.text.pdf.PRAcroForm;
import com.lowagie.text.pdf.PdfCopy;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;

/**
 * @author FC095051
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class PdfTest {

	public static void main(String[] args) throws Exception{
		String inFileName = null;
		PdfImportedPage page;
		String basePath="C:/Documents and Settings/Administrator/My Documents/rtc/leerTest";
		File pdfJournalFile = new File(basePath+"/TestResult.pdf");
		
		if (!pdfJournalFile.exists()) {
			pdfJournalFile.getParentFile().mkdirs();
		}

		

		Document pdfJournalDocument = new Document();

		// we create a writer that listens to the document
		PdfCopy pdfCopy = new PdfCopy(pdfJournalDocument, new FileOutputStream(pdfJournalFile));

		// open the journal document
		pdfJournalDocument.open();
		
		 
	
		
//		 get the first document
		PdfReader pdfReader = new PdfReader(basePath + "/" + "05-hldmTST-2-mod2.pdf");
		

		copyPDFintoPDF(pdfReader,pdfCopy);
//		pdfReader.close();
		inFileName = basePath + "/" + "05-hldmTST-2-mod2.pdf";
		


		// create a reader for a certain document
		pdfReader = new PdfReader(inFileName);
		copyPDFintoPDF(pdfReader,pdfCopy);

//		PRAcroForm form = pdfReader.getAcroForm();
//		if (form != null) {
//			pdfCopy.copyAcroForm(pdfReader);
//		}	
		pdfJournalDocument.close();
		System.out.println("ok");

	}
	
	private static void copyPDFintoPDF(PdfReader pdfReader, PdfCopy pdfCopy) throws IOException, BadPdfFormatException {

		PdfImportedPage page;
		int pages = pdfReader.getNumberOfPages();

		for (int pageNo = 0; pageNo < pages;) {
			++pageNo;
			page = pdfCopy.getImportedPage(pdfReader, pageNo);
			pdfCopy.addPage(page);
		}
	}
}
