iText 7 从字节数组合并文档

iText 7 Merge Documents from Byte Array

我使用 iTextSharp 来合并字节数组中的两个文档,如下所示:

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();
}

我无法将它转换为 iText 7,因为一堆东西发生了变化。有人会给我正确的指示吗?非常感谢!

经过一番研究,我明白了。这是解决方案 (iText7),以防有人在转换代码时遇到问题:

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();
}