私は2つのDocument
オブジェクトを持っています
Document
itextsharp を使用してこれら 2 つのオブジェクトをマージするにはどうすればよいですか?
私は2つのDocument
オブジェクトを持っています
Document
itextsharp を使用してこれら 2 つのオブジェクトをマージするにはどうすればよいですか?
Haas氏によると(SOのどこかにある彼のコードの助けを借りて)、
「残念ながら、私の知る限りでは、2 つの Document オブジェクトをマージする方法はありません。これらのオブジェクトは、PDF ファイル形式の複雑さを抽象化するヘルパー クラスです。これらの抽象化の「コスト」の 1 つは、制限があることです。ただし、他の人が指摘しているように、個別の 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");
ご参考までに
new MemoryStream(realfinalStream.ToArray())
MemoryString が閉じられていたので、それを行いました。
口当たりがもう少し簡単:
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());
}