我正在开发一个 ASP.NET 应用程序,我必须根据在页面上动态创建的表格作为电子邮件附件发送 PDF。所以我有一个函数可以将 PDF 创建为 iTextSharp Document 并返回它。如果我只是尝试保存此文档,它可以正常工作,但我在尝试将其作为 Stream 时遇到了麻烦。我已经尝试了几件事,但我总是在某个时候卡住。
我试图序列化它,但似乎 Document 不可序列化。然后我尝试使用 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);
}