c#-4.0 – 将内存流合并到一个iText文档

我有四个要合并的MemoryStream数据,然后打开pdfDocument,而不创建单个文件.

可以将它们写入文件然后合并它们但这样做会很糟糕,这也会导致一些问题,所以我想避免这种情况.

但是,我找不到将MemoryStreams与iText5 for .NET合并的方法.

现在,这是我用文件做的方式:

private static void ConcatenateDocuments()
    {
        var stream = new MemoryStream();

        var readerFrontPage = new PdfReader(Folder + FrontPageName);
        var readerDocA = new PdfReader(Folder + docA);
        var readerDocB = new PdfReader(Folder + DocB);
        var readerAppendix = new PdfReader(Folder + Appendix);
        var pdfcopyFields = new PdfcopyFields(stream);

        pdfcopyFields.AddDocument(readerFrontPage);
        pdfcopyFields.AddDocument(readerDocA );
        pdfcopyFields.AddDocument(readerDocB);
        pdfcopyFields.AddDocument(readerAppendix);
        pdfcopyFields.Close();

        SavePdf(stream,FilenameReport);
    }

由于我需要删除文件的使用,因此我保留了MemoryStream,因为不同的部分是从不同的资源构建的.所以我引用了这些内存流.

如何才能做到这一点?

解决方法

在这种情况下,通过将流的位置设置回0,可以修复未找到的错误PDF标题签名.因为您没有收到错误无法访问已关闭的流我假设您已经正确地将PdfWriter的CloseStream设置为false .

下面是一个完整的C#2010 WinForm应用程序,目标是iTextSharp 5.1.1.0,它在MemoryStreams中创建三个PDF并将它们组合在一起.由于我没有方便的Web服务器,我正在将它们写入磁盘.

using System;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender,EventArgs e)
        {
            //Create three MemoryStreams
            MemoryStream[] streams = { CreateDoc("Page 1"),CreateDoc("Page 2"),CreateDoc("Page 3") };

            //I don't have a web server handy so I'm going to write my final MemoryStream to a byte array and then to disk
            byte[] bytes;

            //Create our final combined MemoryStream
            using (MemoryStream finalStream = new MemoryStream())
            {
                //Create our copy object
                PdfcopyFields copy = new PdfcopyFields(finalStream);

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

            //Write out the file to the desktop
            string outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"Combined.pdf");
            using (FileStream fs = new FileStream(outputFile,FileMode.Create,FileAccess.Write,FileShare.None))
            {
                fs.Write(bytes,bytes.Length);   
            }

            this.Close();
        }

        /// <summary>
        /// Helper method to create temporary documents
        /// </summary>
        private MemoryStream CreateDoc(string name)
        {
            MemoryStream ms = new MemoryStream();
            using (Document doc = new Document(PageSize.LETTER))
            {
                using (PdfWriter writer = PdfWriter.GetInstance(doc,ms))
                {
                    writer.CloseStream = false;
                    doc.open();
                    doc.Add(new Paragraph(name));
                    doc.Close();
                }
            }
            return ms;
        }
    }
}

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...