Concatinating 2 PDF's
In my previous post you have seen how to generate jasper reports
In this post i will tell you how to concatenate two pdf's as one pdf
1.create a class PDFConcatenate
2. place 2 pdfs in a specific path and give that path below
In my previous post you have seen how to generate jasper reports
In this post i will tell you how to concatenate two pdf's as one pdf
1.create a class PDFConcatenate
2. place 2 pdfs in a specific path and give that path below
class PdfConcatenat {
static void main(args){
List list = new ArrayList();
try {
// Source pdfs
list.add(new FileInputStream(new File("D:/1.pdf")));
list.add(new FileInputStream(new File("D:/2.pdf")));
// Resulting pdf
OutputStream out = new FileOutputStream(new File("D:/result.pdf"));
doMerge(list, out);
Process p = Runtime
.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler D:/result.pdf");
p.waitFor();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void doMerge(List list, OutputStream outputStream)throws DocumentException, IOException {
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
document.open();
PdfContentByte cb = writer.getDirectContent();
for (InputStream ins : list) {
PdfReader reader = new PdfReader(ins);
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