-1

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

kyogs
  • 6,766
  • 1
  • 34
  • 50

3 Answers3

3

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?

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • got this errorcom.itextpdf.license.LicenseKeyException: License file not loaded. and generated new pdf file with 0size – kyogs May 03 '13 at 09:19
  • i removed your name from question and replace PdfSmartCopy but still not getting any success. – kyogs May 03 '13 at 09:20
  • Don't blame the error on PdfSmartCopy. The exception tells you that you need to get a license file: http://demo.itextsupport.com/newslicense/ and you need to load it into your code: LicenseKey.loadLicenseFile(path_to_licensekey); where path_to_licensekey is the path to where you've stored the license key. – Bruno Lowagie May 03 '13 at 09:34
2

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/

Michaël Demey
  • 1,567
  • 10
  • 18
  • when i useed itextgoogle-5.4.0.jar it give me error "Caused by: java.lang.NoClassDefFoundError: org.spongycastle.crypto.engines.AESFastEngine" – kyogs May 03 '13 at 07:55
  • got this error com.itextpdf.license.LicenseKeyException: License file not loaded. and generated new pdf file with 0size – kyogs May 03 '13 at 09:19
  • i have added sc-light-jdk15on-1.47.0.2-sources.jar in libs folder but still not getting any success – kyogs May 03 '13 at 09:21
  • You'll need to add the actual compiled spongycastle jars and not the source code jars, those won't do much for any application. And to load your license file you'll need to include, LicenseKey.loadLicenseFile(String path) – Michaël Demey May 03 '13 at 09:46
1

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(); 
    }
Jagadeesh
  • 239
  • 5
  • 9