javapdfbox

Migration to Apache PDFBox 3.0. PDFMergerUtility addSource() does not accept ByteArrayInputStream, what is its replacement?


Previously, to merge multiple PDF files, I did it this way:

Map<String, byte[]> documentList = new TreeMap<>();
....
PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
pdfMergerUtility.setDestinationStream(outputStream);
documentList.forEach((key, value) -> 
    pdfMergerUtility.addSource(new ByteArrayInputStream(value)));

In version 3.0.0, the addSource method no longer accepts InputStream. I found the migration guide, but it doesn't explain how to convert this method.

I expect a similar method to have the same behavior.


Solution

  • I solved the problem, thanks to @M.Deinum

    Updated code:

    PDFMergerUtility pdfMergerUtility = new PDFMergerUtility();
    pdfMergerUtility.setDestinationStream(outputStream);
    documentList.forEach((key, value) -> {
        pdfMergerUtility.addSource(new RandomAccessReadBuffer(value));
    });