1

I am building a tool that generate pdf report based on input provided by users.

But the situation that I have it is like let’s say that user’s provided input and that input has to consolidate 20 items in a pdf report (or may be more items) and each item has some records stored in DB and also a pdf file stored in DB. Now I need to fetch each items records from DB and their pdf files as well for all 20 items and then consolidate all of them in single reports that will be downloaded in user’s system.

And how I am doing it: I am create a separate pdf file for each 20 items and then merged all of them in one pdf file but this seems to more time consuming and also affected page number as well. I also tried by reading pdfs content and by appending them in one StringBuffer and then write that StringBuffer in final pdf but after doing this I was not able to read that content of pdf as it was in unreadable format. I am using iText and Yahp jars to consolidate the reports. Could someone please suggest me if I can achieve same result in better way.

public static void doMerge(List<InputStream> list, OutputStream outputStream)
        throws DocumentException, IOException {

    Document document = new Document();

    com.lowagie.text.Rectangle pageSize1 = new com.lowagie.text.Rectangle(695.0F, 942.0F);

     document.setPageSize(pageSize1);

    PdfWriter writer = PdfWriter.getInstance(document, outputStream);

    document.open();

    PdfContentByte cb = writer.getDirectContent();

    writer.setPageEvent(null);

    PdfReader reader = null;

    for (InputStream in : list) {

        reader = new PdfReader(in);

        for (int i = 1; i <= reader.getNumberOfPages(); i++) {
            document.newPage();
            PdfImportedPage page = writer.getImportedPage(reader, i);
            cb.addTemplate(page, 0, 0);
        }
    }

    outputStream.flush();
    document.close();
    outputStream.close();
    writer.close();
    reader.close();
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • How are you merging the files? If you're doing it right, you're using `PdfCopy`, but as you're not showing any code, your problem may be that you're doing it wrong. It is also unclear which problem you have with page numbers. Finally, your attempt to append PDF files in StringBuffers tells us that you didn't read any of the documentation. – Bruno Lowagie Jul 04 '15 at 08:11
  • This is the code that I use to merge pdfs... and about page number each page has it's page number in down like page 1/10, 2/10 ets.. as I am merging pdfs so it shows same page number what they have individualy, not as per the final pdfs.. – gyanendra pratap singh Jul 04 '15 at 08:52
  • It is clear that you don't understand PDF. A page doesn't know anything about its page number. It's just some content added at some position on a page. If you think that content can automatically "adapt", you are making some assumptions about PDF that are completely wrong. Please read a book about PDF. – Bruno Lowagie Jul 04 '15 at 13:18
  • Please download [the official documentation](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) (it's free and it will be a great help). By reading this documentation, you'll learn that your code is wrong. See for instance [how to merge documents correctly](http://stackoverflow.com/questions/21731439/how-to-merge-documents-correctly). The documentation also explains [the problem of the page numbers](http://stackoverflow.com/questions/26673581/how-to-extract-page-number-from-pdf-file). – Bruno Lowagie Jul 04 '15 at 16:05
  • Thanks a lot @Bruno Lowagie.. yes you are right, I am new to use iText APIs... now I did it using PdfCopy and issue is resolved… Really happy to see your comments… you are really doing great job.... – gyanendra pratap singh Jul 06 '15 at 10:11
  • Unfortunately, not every one thinks I'm doing a great job. Recently, people have been consistently down-voting my answers. – Bruno Lowagie Jul 06 '15 at 10:17

0 Answers0