[英]itextsharp merging pdfs an emailing
我正在与一些内存流逻辑作斗争。
我有一个接收ID列表的方法。 使用它们从Web服务器提取pdf,然后将它们合并为一个pdf。
然后,我想通过电子邮件发送此pdf文件(仅在内存中工作)
private Stream GetWebReport(string selected_id)
{
var IdLst = selected_id.Split(',').ToList();
MemoryStream stream = new MemoryStream();
Document document = new Document();
PdfCopy pdf = new PdfCopy(document, stream);
PdfReader reader = null;
try
{
document.Open();
foreach (var id in IdLst)
{
int i = Convert.ToInt32(id);
string invoice_url = string.Concat("http://specialurl/", id);
var urlpdf = new System.Net.WebClient().OpenRead(invoice_url);
reader = new PdfReader(urlpdf);
pdf.AddDocument(reader);
reader.Close();
}
}
catch (Exception)
{
throw;
}
finally
{
if (document != null)
{
document.Close();
}
}
return stream;
}
但是当我尝试将结果流用于电子邮件时var mem = GetWebReport(selected_id);
mem.Seek(0, SeekOrigin.Begin);
Attachment att = new Attachment(mem, "Report for you", "application/pdf");
我被告知:
System.ObjectDisposedException: 'Cannot access a closed Stream.'
因此,我确定自己的itextsharp逻辑很好(当我使用文件流时,会得到正确的结果)。 我确信我在传递流中的逻辑是错误的
采用
PdfCopy pdf = new PdfCopy(document, stream);
pdf.CloseStream = false;
关闭要在其他地方使用的pdf后,这将使流保持打开状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.