我使用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。有人会这样给我正确的方向吗?提前非常感谢!
答案 0 :(得分:1)
经过一些研究,我弄清楚了。这是解决方案(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();
}