我有两个Document
个对象
如何使用itextsharp合并这两个Document
个对象?
答案 0 :(得分:1)
根据哈斯先生(在他的代码的帮助下,在某处),
“不幸的是,据我所知,没有办法合并两个Document对象。这些对象是帮助类,它们抽象出PDF文件格式的复杂性。这些抽象的”成本“之一是您只能使用单个文档。但是,正如其他人所指出的那样,您可以创建单独的PDF(甚至是内存中),然后合并它们。“
所以我做到了这一点:
我使用了PdfCopyFields对象。
MemoryStream realfinalStream = new MemoryStream();
MemoryStream[] realstreams = { stream,new MemoryStream(finalStream.ToArray()) };
using (realfinalStream)
{
//Create our copy object
PdfCopyFields copy = new PdfCopyFields(realfinalStream);
//Loop through each MemoryStream
foreach (MemoryStream ms in realstreams)
{
//Reset the position back to zero
ms.Position = 0;
//Add it to the copy object
copy.AddDocument(new PdfReader(ms));
//Clean up
ms.Dispose();
}
//Close the copy object
copy.Close();
}
return File(new MemoryStream(realfinalStream.ToArray()), "application/pdf","hello.pdf");
FYI
new MemoryStream(realfinalStream.ToArray())
我这样做是因为MemoryString已关闭。
答案 1 :(得分:0)
口感更轻松一点:
您必须获取PDF文档内存流并将它们合并在一起!
这是一个简单的功能,可以实现这一目标!
public MemoryStream MemoryStreamMerger(List<MemoryStream> streams)
{
MemoryStream OurFinalReturnedMemoryStream;
using (OurFinalReturnedMemoryStream = new MemoryStream())
{
//Create our copy object
PdfCopyFields copy = new PdfCopyFields(OurFinalReturnedMemoryStream);
//Loop through each MemoryStream
foreach (MemoryStream ms in streams)
{
//Reset the position back to zero
ms.Position = 0;
//Add it to the copy object
copy.AddDocument(new PdfReader(ms));
//Clean up
ms.Dispose();
}
//Close the copy object
copy.Close();
//Get the raw bytes to save to disk
//bytes = finalStream.ToArray();
}
return new MemoryStream(OurFinalReturnedMemoryStream.ToArray());
}