balaji
balaji

Reputation: 1304

'The document has no page' error thrown when closing a document

I'm generating, saving and writing PDF on output stream.

I'm getting a The document has no page error while writing on output stream.

What is the problem?

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();

No problem in saving. I'm getting error on this line cpdfDoc.Close();.

Upvotes: 2

Views: 3106

Answers (1)

Manish Sharma
Manish Sharma

Reputation: 2426

use this code and set your pdf location. and image path. its working.

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);

don't use this line

contents = sw.ToString();

Upvotes: 1

Related Questions