gpt4 book ai didi

c# - iText 7 从字节数组合并文档

转载 作者:行者123 更新时间:2023-12-05 01:38:40 24 4
gpt4 key购买 nike

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

关于c# - iText 7 从字节数组合并文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59384481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com