I am trying to merge around 15 pdfs into a single PDF. It's working most of the time but sometimes getting OutofMemory Java Heap Space error. I want to avoid temp file creation. Below is my code .
public static byte[] mergePdf(List < InputStream > inputStreams) {
    Document document = new Document();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    PdfCopy copy = new PdfSmartCopy(document, byteArrayOutputStream);
    document.open();
    for (InputStream inputStream: inputStreams) {
        PdfReader pdfReader = new PdfReader(inputStream);
        copy.addDocument(pdfReader);
        copy.freeReader(pdfReader);
        pdfReader.close();
    }
    document.close();
    return byteArrayOutputStream.toByteArray();
}
 
     
    