PdfDocument to byte[] using PdfMerger iText7

后端 未结 1 562
半阙折子戏
半阙折子戏 2021-01-27 15:32

I\'ve a requirement, where I am generating different pdf\' using iText 7.1.11. I am using PdfMerger to merge all pdf\'s on the fly. I am able to genera

相关标签:
1条回答
  • 2021-01-27 16:33

    This is how you initialize your PdfMerger:

    PdfWriter writer = new PdfWriter(dest); // 'dest' is local file system path 
    PdfDocument pdf = new PdfDocument(writer);
    PdfMerger merger = new PdfMerger(pdf);
    

    I.e. you explicitly write to the local file system and even stress that fact in the comment.

    If you want to have the merged PDF in a byte[] at the end, why don't you simply use a ByteArrayOutputStream here (as you claim you have tried a few lines earlier):

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfWriter writer = new PdfWriter(byteArrayOutputStream );
    PdfDocument pdf = new PdfDocument(writer);
    PdfMerger merger = new PdfMerger(pdf);
    ...
    pdf.close();
    byte[] bytes = byteArrayOutputStream.toByteArray();
    
    0 讨论(0)
提交回复
热议问题