I used iTextSharp in order to merge two documents from byte arrays like this:
using (MemoryStream ms = new MemoryStream())
using (Document doc = new Document())
using (PdfSmartCopy copy = new PdfSmartCopy(doc, ms))
{
    // Open document
    doc.Open();
    // Create reader from bytes
    using (PdfReader reader = new PdfReader(pdf1.DocumentBytes))
    {
        //Add the entire document instead of page-by-page
        copy.AddDocument(reader);
    }
    // Create reader from bytes
    using (PdfReader reader = new PdfReader(pdf2.DocumentBytes))
    {
        //Add the entire document instead of page-by-page
        copy.AddDocument(reader);
    }
    // Close document
    doc.Close();
    // Return array
    return ms.ToArray();
}
I am unable to convert this to iText 7 since a bunch of stuff changed. Would someone be that kind to give me the right directions? Thanks a lot in advance!
I figured it out after some research. This is the solution (iText7) in case anyone is also facing trouble converting the code:
using (MemoryStream ms = new MemoryStream())
using (PdfDocument pdf = new PdfDocument(new PdfWriter(ms).SetSmartMode(true)))
{
    // Create reader from bytes
    using (MemoryStream memoryStream = new MemoryStream(pdf1.DocumentBytes))
    {
        // Create reader from bytes
        using (PdfReader reader = new PdfReader(memoryStream))
        {
            PdfDocument srcDoc = new PdfDocument(reader);
            srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdf);
        }
    }
    // Create reader from bytes
    using (MemoryStream memoryStream = new MemoryStream(pdf2.DocumentBytes))
    {
        // Create reader from bytes
        using (PdfReader reader = new PdfReader(memoryStream))
        {
            PdfDocument srcDoc = new PdfDocument(reader);
            srcDoc.CopyPagesTo(1, srcDoc.GetNumberOfPages(), pdf);
        }
    }
    // Close pdf
    pdf.Close();
    // Return array
    return ms.ToArray();
}
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With