简体   繁体   English

如何使用itextsharp合并两个Document对象

[英]How to Merge two Document objects using itextsharp

I have two Document objects 我有两个Document对象

How does one Merge these two Document objects using itextsharp? 如何使用itextsharp合并这两个Document对象?

As per Mr. Haas (with the help of his code somewhere on SO), 根据Haas先生(在SO上某处的代码的帮助下),

"Unfortunately, to the best of my knowledge, there is no way to merge two Document objects. These objects are helper classes that abstract away the intricacies of the PDF file format. One of the "costs" of these abstractions is that you are limited to a single document. However, as others have pointed out, you can create separate PDFs (even in-memory) and then merge them." “不幸的是,据我所知,无法合并两个Document对象。这些对象是帮助程序类,可以抽象出PDF文件格式的复杂性。这些抽象的“成本”之一是您的局限性到一个文档。但是,正如其他人指出的那样,您可以创建单独的PDF(甚至是内存中的PDF),然后将它们合并。”

So i did just that: 所以我就是这样做的:

I used the PdfCopyFields object. 我使用了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())

I did that because the MemoryString was closed. 我这样做是因为MemoryString已关闭。

A little more easier on the palate: 口感更加轻松:

You have to take the PDF Document memory streams and merge them together! 您必须获取PDF Document内存流并将它们合并在一起!

Here is a simple function that will accomplish this! 这是一个可以完成此操作的简单功能!

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

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM