iText - Read pdfs in a loop and merge one by one

旧时模样 提交于 2019-12-13 03:48:38

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/51936626/itext-read-pdfs-in-a-loop-and-merge-one-by-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!