簡體   English   中英

itextsharp合並pdf電子郵件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM