[英]itextsharp merging pdfs an emailing
I am battling with some memorystream logic. 我正在与一些内存流逻辑作斗争。
I have a method that receives a list of Id. 我有一个接收ID列表的方法。 uses them to pull pdfs from a webserver, and merges them into one pdf.
使用它们从Web服务器提取pdf,然后将它们合并为一个pdf。
I want to then email this pdf (working in memory only) 然后,我想通过电子邮件发送此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;
}
but when I try use the resulting stream for an email var mem = GetWebReport(selected_id); 但是当我尝试将结果流用于电子邮件时var mem = GetWebReport(selected_id);
mem.Seek(0, SeekOrigin.Begin);
Attachment att = new Attachment(mem, "Report for you", "application/pdf");
I get told: 我被告知:
System.ObjectDisposedException: 'Cannot access a closed Stream.'
So I am sure that my itextsharp logic is good (When I use a filestream I get the correct results). 因此,我确定自己的itextsharp逻辑很好(当我使用文件流时,会得到正确的结果)。 I am sure that my logic in passing streams is what is faulty
我确信我在传递流中的逻辑是错误的
Use 采用
PdfCopy pdf = new PdfCopy(document, stream);
pdf.CloseStream = false;
This will keep the stream open after closing the pdf to be used elsewhere. 关闭要在其他地方使用的pdf后,这将使流保持打开状态。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.