0

i am trying to merge 2 pdf in one. Merging is working fine but contents overflow from pdf page. A shown in attachment. Original Document pdf is as Follows.

Original Doc before merge

After Merge Document is coming like this enter image description here

Java code as follows :

BaseFont bf = BaseFont.createFont(BaseFont.TIMES_BOLD, BaseFont.CP1252, BaseFont.EMBEDDED);
        //BaseFont bf= BaseFont.createFont();
        PdfContentByte cb = writer.getDirectContent(); // Holds the PDF
        // data
        PdfImportedPage page;
        int currentPageNumber = 0;
        int pageOfCurrentReaderPDF = 0;
        Iterator<PdfReader> iteratorPDFReader = readers.iterator();

        // Loop through the PDF files and add to the output.
        while (iteratorPDFReader.hasNext()) {
            PdfReader pdfReader = iteratorPDFReader.next();

            // Create a new page in the target for each source page.
            while (pageOfCurrentReaderPDF < pdfReader.getNumberOfPages()) {
                document.newPage();
                pageOfCurrentReaderPDF++;
                currentPageNumber++;
                page = writer.getImportedPage(pdfReader,
                        pageOfCurrentReaderPDF);
                cb.addTemplate(page, 0, 0);

                // Code for pagination.
                if (paginate) {
                    cb.beginText();
                    cb.setFontAndSize(bf, 9);
                    cb.showTextAligned(PdfContentByte.ALIGN_CENTER, ""
                            + currentPageNumber + " of " + totalPages, 520,
                            5, 0);
                    cb.endText();
                }
            }
            pageOfCurrentReaderPDF = 0;
        }

please Help.

zytham
  • 613
  • 1
  • 12
  • 27
  • Some zoom property of the original one or Target PDf ?? – zytham Feb 08 '13 at 11:21
  • You use `PdfWriter` instead of `PdfCopy` and you ignore the dimensions of the pages you import. Thus, either follow Bruno's advice in his answer (which copies the pages as they are) or (if the former is not possible due to unappropriate design of the software calling your method) take **all** page dimension data of the imported page into account, either by creating pages of the required size or by scaling the imported page. – mkl Feb 08 '13 at 12:11

2 Answers2

2

Please download chapter 6 of my book and take a look at table 6.1. You're making the mistake merging two documents using PdfWriter instead of using PdfCopy as documented. Take a look at listing 6.22 to find out how to add page numbers when using PdfCopy.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
1

i used "PdfCopyFields" Snippet as follows :

public static boolean concatPDFFiles(List<String> listOfFiles,
        String outputfilepath) throws FileNotFoundException, DocumentException {
    PdfCopyFields copy = null;
    try {
        copy = new PdfCopyFields(new FileOutputStream(outputfilepath));
    } catch (DocumentException ex) {
        Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
    }
    try {
        for (String fileName : listOfFiles) {
            PdfReader reader1 = new PdfReader(fileName);
            copy.addDocument(reader1);
        }
    } catch (IOException ex) {
        Logger.getLogger(MergerGoogleDocsToPDF.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        copy.close();
    }
    if (new File(outputfilepath).exists()) {
        double bytes = new File(outputfilepath).length();
        //double kilobytes = (bytes / 1024);
        if (bytes != 0) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}
zytham
  • 613
  • 1
  • 12
  • 27