Hi Guys,
Here I am showing you the merging of two PDF files using iText API.
You are required to add iText-x.x.x.jar (i.e. iText-2.1.7.jar) file in your project build path.
After adding the Jar file in build path, try out this code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class CombineTwoPDFs {
public static void main(String args[]) throws DocumentException, IOException {
List<InputStream> list = new ArrayList<InputStream>();
list.add(new FileInputStream(new File("<PATH_TO_PDF_FILE1>")));
list.add(new FileInputStream(new File("PATH_TO_PDF_FILE2>")));
OutputStream out = new FileOutputStream(new File("<TARGET_FILE_NAME_WITH_PATH>"));
mergePDF(list, out);
}
public static void mergePDF(List<InputStream> list, OutputStream outputStream)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (InputStream in : list) {
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.newPage();
// import the page from source pdf
PdfImportedPage page = writer.getImportedPage(reader, i);
// add the page to the destination pdf
cb.addTemplate(page, 0, 0);
}
}
outputStream.flush();
document.close();
outputStream.close();
}
}
Here I am showing you the merging of two PDF files using iText API.
You are required to add iText-x.x.x.jar (i.e. iText-2.1.7.jar) file in your project build path.
After adding the Jar file in build path, try out this code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfImportedPage;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;
public class CombineTwoPDFs {
public static void main(String args[]) throws DocumentException, IOException {
List<InputStream> list = new ArrayList<InputStream>();
list.add(new FileInputStream(new File("<PATH_TO_PDF_FILE1>")));
list.add(new FileInputStream(new File("PATH_TO_PDF_FILE2>")));
OutputStream out = new FileOutputStream(new File("<TARGET_FILE_NAME_WITH_PATH>"));
mergePDF(list, out);
}
public static void mergePDF(List<InputStream> list, OutputStream outputStream)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (InputStream in : list) {
PdfReader reader = new PdfReader(in);
for (int i = 1; i <= reader.getNumberOfPages(); i++) {
document.newPage();
// import the page from source pdf
PdfImportedPage page = writer.getImportedPage(reader, i);
// add the page to the destination pdf
cb.addTemplate(page, 0, 0);
}
}
outputStream.flush();
document.close();
outputStream.close();
}
}
No comments:
Post a Comment