Click here to Skip to main content
16,016,204 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai all,

I used this below code for converting asp.net web page to pdf.

but i am getting error like this The given key was not present in the dictionary on this line htmlparser.Parse(sr);
C#
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

please help me out this.
Posted
Updated 18-Aug-12 18:27pm
v2

See these links..2nd and 3rd link contains code for conversion.Compare your code with those..
Convert ASP.NET web page to PDF[^]
http://forums.asp.net/t/1444214.aspx/1[^]
http://aspdotnetcodebook.blogspot.com/2009/04/how-to-convert-web-page-to-pdf.html[^]
 
Share this answer
 
Comments
Madhulika Satalkar 16-Jan-13 0:51am    
i am alos facing same issue with images getting error
also but getting error:

htmlparser.Parse(sr)

is not defining proper path for the images:

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Images\Layout\an-Logo.png'.

plz suggests
even when converted in pdf my css is not working ....plz help
Qasiar Shabbir Awan 22-Aug-13 3:27am    
I have tried below code i am also facing the error, so please help me to solve this error.
Thanks.

public MemoryStream CreatePdf(string html)
{
MemoryStream m = new MemoryStream();
try
{
Document document = new Document(PageSize.LETTER);
PdfWriter.GetInstance(document, new FileStream(@"E:\Traning Projects\ConvertDocFileToPDF\ExampleDoc.pdf", FileMode.OpenOrCreate));

//StringReader sr = new StringReader(html);
//XmlTextReader xtr = new XmlTextReader(sr);

document.Open();
StringBuilder sb = new StringBuilder(html);
using (TextReader sReader = new StringReader(sb.ToString()))
{

List<ielement> list = HTMLWorker.ParseToList(sReader, new StyleSheet());

foreach (IElement elm in list)
{

document.Add(elm);

}

}


// HtmlParser.Parse(document, xtr);

//xtr.Close();
document.Close();
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", ex.Message);
throw ex;
}
return m;
}

I got this error: "The given key was not present in the dictionary."

Kindly help me. Thanks
ridoy 16-Jan-13 5:25am    
the exception clearly say it can't find a directory or file name that you have used.So make sure you give the correct file path.
In Place of "htmlparser.Parse(sr);"
Use this

StringBuilder strB = new StringBuilder(html);

XML
using (TextReader sReader = new StringReader(strB.ToString()))
           {
               List<IElement> list = HTMLWorker.ParseToList(sReader, new StyleSheet());
               foreach (IElement elm in list)
               {
                   Doc.Add(elm);
               }
           }



Regards

Rajkumar P
 
Share this answer
 
Comments
Qasiar Shabbir Awan 22-Aug-13 3:25am    
I have tried below code i am also facing the error, so please help me to solve this error.
Thanks.

public MemoryStream CreatePdf(string html)
{
MemoryStream m = new MemoryStream();
try
{
Document document = new Document(PageSize.LETTER);
PdfWriter.GetInstance(document, new FileStream(@"E:\Traning Projects\ConvertDocFileToPDF\ExampleDoc.pdf", FileMode.OpenOrCreate));

//StringReader sr = new StringReader(html);
//XmlTextReader xtr = new XmlTextReader(sr);

document.Open();
StringBuilder sb = new StringBuilder(html);
using (TextReader sReader = new StringReader(sb.ToString()))
{

List<ielement> list = HTMLWorker.ParseToList(sReader, new StyleSheet());

foreach (IElement elm in list)
{

document.Add(elm);

}

}

// HtmlParser.Parse(document, xtr);

//xtr.Close();
document.Close();
}
catch (Exception ex)
{
System.Diagnostics.EventLog.WriteEntry("Application", ex.Message);
throw ex;
}
return m;
}

I got this error: "The given key was not present in the dictionary."

Kindly help me. Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900