Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将iTextSharp文档加载到MemoryStream_C#_Asp.net_Itextsharp - Fatal编程技术网

C# 将iTextSharp文档加载到MemoryStream

C# 将iTextSharp文档加载到MemoryStream,c#,asp.net,itextsharp,C#,Asp.net,Itextsharp,我正在开发一个ASP.NET应用程序,在这个应用程序中,我必须根据在页面上以电子邮件附件形式创建的表格发送PDF。因此,我有一个函数,将PDF创建为iTextSharp文档并返回它。如果我只想保存这个文档,它可以正常工作,但我在尝试将它作为流时遇到了麻烦。我已经试过好几种方法了,但我总是在某个时候陷入困境 我试图序列化它,但该文档似乎不可序列化。然后我尝试使用PdfCopy,但我不知道如何使用它来解决具体的问题 现在的代码如下所示: //Table,string,string,Stream //

我正在开发一个ASP.NET应用程序,在这个应用程序中,我必须根据在页面上以电子邮件附件形式创建的表格发送PDF。因此,我有一个函数,将PDF创建为iTextSharp文档并返回它。如果我只想保存这个文档,它可以正常工作,但我在尝试将它作为流时遇到了麻烦。我已经试过好几种方法了,但我总是在某个时候陷入困境

我试图序列化它,但该文档似乎不可序列化。然后我尝试使用PdfCopy,但我不知道如何使用它来解决具体的问题

现在的代码如下所示:

//Table,string,string,Stream
//This document returns fine
Document document = Utils.GeneratePDF(table, lastBook, lastDate, Response.OutputStream);

using (MemoryStream ms = new MemoryStream())
{
    PdfCopy copy = new PdfCopy(document, ms);
    //Need something here to copy from one to another! OR to make document as Stream      
    ms.Position = 0;
    //Email, Subject, Stream
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}
byte[] Bytes;
using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    Bytes = ms.ToArray();
}

我过去这样做的方式如下:

        using (Document doc = new Document())
        {
            MemoryStream msPDFData = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(doc, msPDFData);
            doc.Open();
            doc.Add(new Paragraph("I'm a pdf!");
        }
如果您需要访问原始数据,也可以这样做

byte[] pdfData = msPDFData.ToArray();

我过去这样做的方式如下:

        using (Document doc = new Document())
        {
            MemoryStream msPDFData = new MemoryStream();
            PdfWriter writer = PdfWriter.GetInstance(doc, msPDFData);
            doc.Open();
            doc.Add(new Paragraph("I'm a pdf!");
        }
如果您需要访问原始数据,也可以这样做

byte[] pdfData = msPDFData.ToArray();

尽量避免传递本机iTextSharp对象。传递流、文件或字节。目前我面前没有IDE,但您应该能够执行以下操作:

//Table,string,string,Stream
//This document returns fine
Document document = Utils.GeneratePDF(table, lastBook, lastDate, Response.OutputStream);

using (MemoryStream ms = new MemoryStream())
{
    PdfCopy copy = new PdfCopy(document, ms);
    //Need something here to copy from one to another! OR to make document as Stream      
    ms.Position = 0;
    //Email, Subject, Stream
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}
byte[] Bytes;
using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    Bytes = ms.ToArray();
}
然后,您可以将
Utils.SendMail()
更改为接受字节数组,也可以只接受字节数组

编辑

您也可以在代码中执行类似的操作:

using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    ms.Position = 0;
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}

尽量避免传递本机iTextSharp对象。传递流、文件或字节。目前我面前没有IDE,但您应该能够执行以下操作:

//Table,string,string,Stream
//This document returns fine
Document document = Utils.GeneratePDF(table, lastBook, lastDate, Response.OutputStream);

using (MemoryStream ms = new MemoryStream())
{
    PdfCopy copy = new PdfCopy(document, ms);
    //Need something here to copy from one to another! OR to make document as Stream      
    ms.Position = 0;
    //Email, Subject, Stream
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}
byte[] Bytes;
using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    Bytes = ms.ToArray();
}
然后,您可以将
Utils.SendMail()
更改为接受字节数组,也可以只接受字节数组

编辑

您也可以在代码中执行类似的操作:

using(MemoryStream ms = new MemoryStream()){
    Utils.GeneratePDF(table, lastBook, lastDate, ms);
    ms.Position = 0;
    Utils.SendMail(email, lastBook + " - " + lastDate, ms);
}

确保使用
ToArray()
而不是
GetBuffer()
谢谢您的回答!但我在创建PDF时就这样做了,我想用发送电子邮件的方法重用文档@圣诞老人的回答很好!确保使用
ToArray()
而不是
GetBuffer()
谢谢您的回答!但我在创建PDF时就这样做了,我想用发送电子邮件的方法重用文档@圣诞老人的回答很好!根据您的iTextSharp版本,在PDF完全创建后,流将自动关闭
PdfWriter
和派生类(例如
PdfCopy
)具有停止此操作的属性。您可能需要切换此属性。根据您的iTextSharp版本,在PDF完全创建后,流将自动关闭
PdfWriter
和派生类(例如
PdfCopy
)具有停止此操作的属性。您可能需要切换此属性。谢谢@ChrisHaas,工作起来很有魅力!在我的第一次测试中,我尝试过类似的方法,但我不确定为什么在那里不起作用,一定是忘记了什么。再次感谢!谢谢@ChrisHaas,工作得很有魅力!在我的第一次测试中,我尝试过类似的方法,但我不确定为什么在那里不起作用,一定是忘记了什么。再次感谢!