c# - 关闭文档时抛出“文档没有页面”错误

标签 c# asp.net itext

我在输出流上生成、保存和写入 PDF。

在输出流上写入时出现The document has no page错误。

问题是什么?

string contents;
string fileName = "aaa.pdf";
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
hw.AddStyleAttribute("font-size","11px");
abs.RenderControl(hw);
string path = Server.MapPath("../Images/a.png");
contents = sw.ToString();
contents = contents.Replace("../Images/a.png", path);
sw.Close();
hw.Close();       
StringReader sr = new StringReader(contents);

System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/pdf/") + fileName, FileMode.Create);
Document pdfDoc = new Document(PageSize.A4, 30,5,35,5);
Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);

pdfDoc.PageCount = 2;
cpdfDoc.PageCount = 2;
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc,fs);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
fs.Close();
HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);               
PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
cpdfDoc.Open();
htmlparser2.Parse(sr);

cpdfDoc.Close();    

Response.Write(cpdfDoc);
Response.Flush();
Response.End();

保存没有问题。我在这一行 cpdfDoc.Close(); 上遇到错误。

最佳答案

使用此代码并设置您的 pdf 位置。和图像路径。 它的工作。

string contents = "hi";
        string fileName = "aaa.pdf";
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        StringWriter sw = new StringWriter();
        HtmlTextWriter hw = new HtmlTextWriter(sw);
        hw.AddStyleAttribute("font-size", "11px");

        string path = Server.MapPath("~/Images/a.png");

        contents = contents.Replace("~/Images/a.png", path);
        sw.Close();
        hw.Close();
        StringReader sr = new StringReader(contents);

        System.IO.FileStream fs = new System.IO.FileStream(Server.MapPath("~/") + DateTime.Now.Ticks, FileMode.Create);
        Document pdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);
        Document cpdfDoc = new Document(PageSize.A4, 30, 5, 35, 5);

        pdfDoc.PageCount = 2;
        cpdfDoc.PageCount = 2;
        HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
        PdfWriter.GetInstance(pdfDoc, fs);
        pdfDoc.Open();
        htmlparser.Parse(sr);
        pdfDoc.Close();
        fs.Close();
        HTMLWorker htmlparser2 = new HTMLWorker(cpdfDoc);
        PdfWriter.GetInstance(cpdfDoc, Response.OutputStream);
        cpdfDoc.Open();
        htmlparser2.Parse(sr);

不要使用这条线

contents = sw.ToString();

关于c# - 关闭文档时抛出“文档没有页面”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17483772/

相关文章:

c# - 为什么我对 Runspace.Open() 的调用没有返回?

c# - 如何将文件写入内存流,压缩其中三个内存流,然后将其放入另一个内存流中?

jquery - 用于 jQuery Mobile 控件的 Asp.Net MVC Razor 标记

c# - iTextSharp 一切正常,但在打开 pdf 文档时出现奇怪的错误

c# - 使用 LINQ 做我需要做的事情的更好方法?

c# - MySQL 语法错误 ASP.NET

c# - 如何清除 System.Runtime.Caching.MemoryCache

asp.net - ModalPopupExtender 和验证问题

c# - iTextSharp : How to resize an image to fit a fix size?

java - 如何使用 iText PDF Java 库打开远程目录?