Monday, May 16, 2011

Concat Pdf files into one file Using iText

Required Jar file Name: itext-2.1.2.jar

Source code:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfCopyFields;
import com.lowagie.text.pdf.PdfReader;
public class ConcatPdf {
/**
* @param args
* the command line arguments
*/
public static void main( String[] args ) {
PdfCopyFields copy;
try {
//New File Name
copy =
new PdfCopyFields( new FileOutputStream(
"D:\\Ganga\\ganga_Test.pdf" ) );
//Existing file 1. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg5.pdf" ) );
//Existing file 2. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg6.pdf" ) );
//Existing file 3. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg7.pdf" ) );
//Existing file 4. Add this into New File Name
copy.addDocument( new PdfReader( "D:\\Ganga\\gg8.pdf" ) );
copy.close();
} catch ( FileNotFoundException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( DocumentException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch ( IOException e ) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

No comments:

Post a Comment