使用ItextSharp时无法访问关闭的流错误

时间:2018-03-02 18:42:00

标签: c# asp.net-mvc pdf stream itext

  

System.ObjectDisposedException:无法访问已关闭的Stream。          在System.IO .__ Error.StreamIsClosed()          在System.IO.MemoryStream.Write(Byte []缓冲区,Int32偏移量,Int32计数)          在iTextSharp.text.pdf.OutputStreamCounter.Write(Byte []缓冲区,Int32偏移量,Int32计数)          在iTextSharp.text.pdf.PdfIndirectObject.WriteTo(Stream os)          在iTextSharp.text.pdf.PdfWriter.PdfBody.Write(PdfIndirectObject indirect,Int32 refNumber,Int32 generation)          在iTextSharp.text.pdf.PdfWriter.PdfBody.Add(PdfObject objecta,Int32 refNumber,Int32 generation,Boolean inObjStm)          在iTextSharp.text.pdf.PdfWriter.AddToBody(PdfObject objecta,PdfIndirectReference refa)          在iTextSharp.text.pdf.Type1Font.WriteFont(PdfWriter writer,PdfIndirectReference piref,Object [] parms)          在iTextSharp.text.pdf.FontDetails.WriteFont(PdfWriter writer)          在iTextSharp.text.pdf.PdfWriter.AddSharedObjectsToBody()          在iTextSharp.text.pdf.PdfWriter.Close()          在iTextSharp.text.DocWriter.Dispose()

[HttpPost]
[ValidateAntiForgeryTokenAjax]
public ActionResult CreatePdf(string htmlData)
{
    string fileName = "BillInvoice.pdf";
    try
    {
        using (var ms = new MemoryStream())
        {
            using (var doc = new Document(PageSize.A4, 25, 25, 25, 25))
            {
                using (var writer = PdfWriter.GetInstance(doc, ms))
                {
                    doc.Open();
                    using (var srHtml = new StringReader(htmlData))
                    {
                        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                    }
                    CurrentHomeView = GetHomeView();
                    List<string> signBase64List = new List<string>()
                    {
                        CurrentHomeView.BillSig.GmSignature,
                        CurrentHomeView.BillSig.DMSignature
                    };
                    foreach (var sign in signBase64List)
                    {
                        if (!string.IsNullOrWhiteSpace(sign))
                        {
                            string certbase64 = sign.Substring(sign.IndexOf(',') + 1);
                            doc.Add(ConvertImage(certbase64));
                        }
                    }
                    doc.Close();
                }
            }
            TempData[fileName] = ms;
            //ms.Position = 0;
        }
        return Json(new { success = true, fileName }, JsonRequestBehavior.AllowGet);
    }
    catch (Exception e)
    {
        fileName = e.ToString();
        return Json(new { success = false, fileName }, JsonRequestBehavior.AllowGet);
    }
}

public iTextSharp.text.Image ConvertImage(string base64String)
{
    iTextSharp.text.Image png = null;

    Byte[] bytes = Convert.FromBase64String(base64String);
    png = iTextSharp.text.Image.GetInstance(bytes);

    return png;
}

public ActionResult DownloadCertPdf(string fileName)
{
    var ms = TempData[fileName] as MemoryStream;
    if (ms == null)
    {
        return new EmptyResult();
    }
    TempData[fileName] = null;
    return File(ms.ToArray(), "application/pdf", fileName);
}

我已经检查了同一主题的其他类似问题,但没有解决它们。

我正在使用ItextSharp从Html标签生成pdf。

在文档加载时,我的一个函数循环并为每个部分创建了Table标记,然后通过ajax传递给我的MVC Controller。

为了测试目的,当我硬编码写表标签并传递它时没有抛出任何异常但是当我通过循环通过dom创建表标签时,我得到了以下异常。

我的代码是:

0 个答案:

没有答案