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

前端之家收集整理的这篇文章主要介绍了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;
        }
    }
}

猜你在找的C#相关文章