0

I have a loop where I read a pdf file every time. And I want to add these pdf files into another final pdf. Basically merging all pdfs into one new pdf file.

I tried following way :

I tried to concatenate byteArrays inside the loop like

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (............){
  outputStream.write(myInfo.getByteArray());
}

pdfreader = new PdfReader(outputStream.toByteArray());
FileOutputStream fileout = new FileOutputStream(file);

PdfStamper pdfStamper = new PdfStamper(pdfreader, fileout);

pdfStamper.close();
pdfreader.close();

The problem the final pdf does not have all the pdfs. Instead it has only one pdf.

And I am not sure if this is the right way to do it. Or is there any other to merge pdfs one by one ?

Mukund S
  • 31
  • 2
  • 9
  • Have a look at [https://stackoverflow.com/questions/3585329/how-to-merge-two-pdf-files-into-one-in-java](https://stackoverflow.com/questions/3585329/how-to-merge-two-pdf-files-into-one-in-java) –  Aug 20 '18 at 18:36
  • @weizenkeimhugo Thanks – Mukund S Aug 20 '18 at 22:31

1 Answers1

0

Have a look at the documentation for PdfMerger: itextsupport.com/apidocs/iText7/7.0.3/com/itextpdf/kernel/utils/PdfMerger.html

I can prepare an example later if needed.

Ben Ingle
  • 575
  • 2
  • 12
  • I missed that you were on iText 5. PdfCopy is a good solution. Also look at PdfSmartCopy if your PDFs are coming from similar sources and might share images or other resources. – Ben Ingle Aug 20 '18 at 23:27
  • Okay Thanks. And I have a doubt. Is it possible to create a page and add it to pdfcopy. I tried using **copy.add(new Paragraph("hello world"))** but it is not working. Is there any other way ? – Mukund S Aug 22 '18 at 23:11