I am trying to merge pdf file by using iText API to merge two or more PDF documents into one.But in result i am getting merge pdf with 0 byte size.I post my code as shown below.I tried with iText.jar file also but give same 0 size pdf.
And got this :-" Could not find class 'com.itextpdf.text.pdf.PdfPrinterGraphics2D', referenced from method com.itextpdf.text.pdf.PdfContentByte.createPrinterGraphicsShapes ". Still i am not getting any success.
Code:
public class ItextMerge {
public static void main() {
List<InputStream> list = new ArrayList<InputStream>();
try {
// Source pdfs
list.add(new FileInputStream(new File("mnt/sdcard/nocturia.pdf")));
list.add(new FileInputStream(new File("mnt/sdcard/Professional Android Application Development.pdf")));
// Resulting pdf
OutputStream out = new FileOutputStream(new File("mnt/sdcard/newmerge.pdf"));
doMerge(list, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* Merge multiple pdf into one pdf
*
* @param list
* of pdf input stream
* @param outputStream
* output file output stream
* @throws DocumentException
* @throws IOException
*/
public static void doMerge(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);
// cb.addTemplate(page, 0, 0);
}
}
outputStream.flush();
document.close();
outputStream.close();
}
}
Any idea?
Thank you
I upvoted Michael's answer, because it's the correct answer to your question.
However, reading your code, you have another problem you're not aware of: you're using the wrong code to merge PDFs. You should use PdfCopy
or PdfSmartCopy
, NOT PdfWriter
!
This has been explained many times before:
The fact that you're using PdfWriter reveals that you didn't read the documentation .
Furthermore, your question sounds as if you don't know that Lowagie is a name of a person. Actually, it's my name, and it's very awkward when somebody says, Lowagie iText API not working . For starters, because I've been asking to stop using those old versions of iText, but also because it sounds like a personal accusation, confusing a product with a human being. See What is the difference between lowagie and iText?
Please use the Android port of iText:
http://repo.itextsupport.com//android_gae/com/itextpdf/itextgoogle/
You'll need a trial license to work with iText on Android; http://demo.itextsupport.com/newslicense/
Below is the Simple Code to merge the two pdf files.
try{
String doc1 = FOLDER_PATH + "Doc1.pdf";
String doc2 = FOLDER_PATH + "Doc2.pdf";
String resultDocFile = FOLDER_PATH + "ResultDoc.pdf";
PdfReader reader1 = new PdfReader(doc1);
Document resultDoc = new Document();
PdfCopy copy = new PdfCopy(resultDoc, new FileOutputStream(resultDocFile));
resultDoc.open();
//Copying First Document
for(int i = 1; i <= reader1.getNumberOfPages(); i++) {
copy.addPage(copy.getImportedPage(reader1, i));
}
PdfReader reader2 = new PdfReader(doc2);
//Copying Second Document
for(int i = 1; i <= reader2.getNumberOfPages(); i++) {
copy.addPage(copy.getImportedPage(reader2, i));
}
resultDoc.close();
} catch (Exception e){
e.printStackTrace();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.