How to convert HTML content to PDF? Once, this question was very intricate. But after getting the solution I found it like a piece of cake. Here, I have provided solution with C# code so that you don’t have to waste your time by looking here and there for the solutions.
I have used iTextSharp.dll (version 4.0.6.0) in my solution which is very useful indeed. You can get it from here.
Review the below code, where I have converted content of the GridView to HTML and then I have converted that HTML content into a PDF file.
I have put comments with the code for more specification.
//Include following name space to use iTextSharp library
using iTextSharp.text;
using iTextSharp.text.pdf;
//Document is inbuilt class, available in iTextSharp
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
StringBuilder strData = new StringBuilder(string.Empty);
//I have provided Path for the HTML which will be generated from GridView content
string strHTMLpath = Server.MapPath("MyHTML.html");
//I have provided Path for the PDF file which will be generated from HTML content
string strPDFpath = Server.MapPath("MyPDF.pdf");
try
{
StringWriter sw = new StringWriter();
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
HtmlTextWriter htw = new HtmlTextWriter(sw);
//gvSearchResult is a GridView, I have converted its content to HTML and will acquire final PDF file
/*Here i have set AllowPaging and AllowSorting property of GridView as false, As my aim is to get whole content of the GridView in a single table, by setting these properties and binding the GridView again will remove paging and sorting property from it. */
gvSerchResult.AllowPaging = false;
gvSerchResult.AllowSorting = false;
BindGridView();
//Rendering the HtmlTextWriter
gvSerchResult.RenderControl(htw);
/*Here i have set AllowPaging and AllowSorting property of GridView as true, As my aim is to get whole content of the GridView is now finished and I have acquire its content in HtmlTextWriter. Then by setting properties again I will get the original form of my GridView again*/
gvSerchResult.AllowPaging = true;
gvSerchResult.AllowSorting = true;
BindGridView();
StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
strWriter.Write("<html><head><link href=Style.css rel=stylesheet type=text/css /></head><body>" + htw.InnerWriter.ToString() + "</body></html>");
strWriter.Close();
strWriter.Dispose();
iTextSharp.text.html.simpleparser.
StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle("ol", "leading", "16,0");
PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, "Style.css"));
document.Open();
ArrayList objects;
styles.LoadTagStyle("li", "face", "garamond");
styles.LoadTagStyle("span", "size", "8px");
styles.LoadTagStyle("body", "font-family", "times new roman");
styles.LoadTagStyle("body", "font-size", "10px");
document.NewPage();
objects = iTextSharp.text.html.simpleparser.
HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
for (int k = 0; k < objects.Count; k++)
{
document.Add((IElement)objects[k]);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
document.Close();
Response.Write(Server.MapPath("~/" + strPDFpath));
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=" + strPDFpath);
Response.ContentType = "application/octet-stream";
Response.WriteFile(Server.MapPath("~/" + strPDFpath));
Response.Flush();
Response.Close();
if (File.Exists(Server.MapPath("~/" + strPDFpath)))
{
File.Delete(Server.MapPath("~/" + strPDFpath));
}
}
Whether you found this article useful or not? please provide your valuable comments.
Great Article, it helped me a lot. Thank you very much, keep mailing me about ur good work. Thank a lot once again
Fantastic Article !!!!,
I have checked so many articles on this topic & this certainly is the best.
Thanks a lot & keep up the good work….
Hi!
I used ur Code, didn’t change even 1 leter. Build succsess, but when i browse it i got a mistake:
“~/C:\Inetpub\wwwroot\FinfoBase\Admin\FINFOBASE_Form\MyPDF.pdf’ is not a valid virtual path” – C:\Inetpub\wwwroot\FinfoBase is where i placed my project.
Source Error at line : Respone.write(Server.Mapath(“~”+strPDFpath));
Help me plz!
Hi Nam,
You need to provide only virtual path for the Server.MapPath( ) method. For example “~/FinfoBase/Admin/…../MyPDF.pdf”.
There for use the code like
Server.MapPath(“~/FinfoBase/Admin/…../MyPDF.pdf”)
Hope this will help to solve your problem.
Also check on the web, how to provide virtual path for this method.
Thanks hspinfo for the response, but now i get another error, name “Could not find file ‘C:\Inetpub\wwwroot\FinfoBase\Admin\FINFOBASE_Form\MyPDF.pdf'”.
I guess that i failed to generate the “MyPDF.pdf” file or “MyHTML.html” file from the begining but i dont know how to fix it.
Could you mind send me the example you ‘ve done so that better for me and dont waste your time.
Thanks again .
Hi,
How do i do with standalone java. What is the jar file that i need to include to get the following classes to convert the html content to pdf.
string strHTMLpath = Server.MapPath(”MyHTML.html”);
Environment.NewLine
HtmlTextWriter htw = new HtmlTextWriter(sw);
Please share.
Regards,
Vijay.B
Hi Nam,
You can just copy/paste code that is given here… and it will generate the HTML and PDF files on the same folder where your project is…
Hi Vijay,
I am not aware with the Java, so I don’t know anything much on that…
sorry dear…
Hi hspinfo!
Here are those code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Text;
using System.IO;
using FinfoBase.Engine;
public partial class Admin_FINFOBASE_Form_HTMLToPDF : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGridView();
}
}
protected void BindGridView()
{
DataTable dt = FINFOBASE_FormDB.GetAll();
gvSerchResult.DataSource = dt;
gvSerchResult.DataBind();
}
protected void btnConvert_Click(object sender, EventArgs e)
{
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
StringBuilder strData = new StringBuilder(string.Empty);
//I have provided Path for the HTML which will be generated from GridView
string strHTMLpath = Server.MapPath(“MyHTML.html”);
//I have provided Path for the PDF file which will be generated from HTML content
string strPDFpath = Server.MapPath(“MyPDF.pdf”);
try
{
StringWriter sw = new StringWriter();
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
HtmlTextWriter htw = new HtmlTextWriter(sw);
//gvSearchResult is a GridView, I have converted its content to HTML and will acquire final PDF file
/*Here i have set AllowPaging and AllowSorting property of GridView as false, As my aim is to get
whole content of the GridView in a single table, by setting these properties and binding
the GridView again will remove paging and sorting property from it. */
gvSerchResult.AllowPaging = false;
gvSerchResult.AllowSorting = false;
BindGridView();
//Rendering the HtmlTextWriter
gvSerchResult.RenderControl(htw);
/*Here i have set AllowPaging and AllowSorting property of GridView as true, As my aim is to get
whole content of the GridView is now finished and I have acquire its content in
HtmlTextWriter. Then by setting properties again I will get the original form of my GridView again*/
gvSerchResult.AllowPaging = true;
gvSerchResult.AllowSorting = true;
BindGridView();
StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
strWriter.Write(“” + htw.InnerWriter.ToString() + “”);
strWriter.Close();
strWriter.Dispose();
iTextSharp.text.html.simpleparser.
StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle(“ol”, “leading”, “16,0”);
PdfWriter.GetInstance(document, new FileStream(@”C:\Inetpub\wwwroot\FinfoBase\Admin\FINFOBASE_Form\MyPDF.pdf”, FileMode.Create));
document.Add(new Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, “Style.css”));
document.Open();
ArrayList objects;
styles.LoadTagStyle(“li”, “face”, “garamond”);
styles.LoadTagStyle(“span”, “size”, “8px”);
styles.LoadTagStyle(“body”, “font-family”, “times new roman”);
styles.LoadTagStyle(“body”, “font-size”, “10px”);
document.NewPage();
objects = iTextSharp.text.html.simpleparser.
HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
for (int k = 0; k < objects.Count; k++)
{
document.Add((IElement)objects[k]);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
document.Close();
Response.Write(Server.MapPath(“~/Admin/FINFOBASE_Form/MyPDF.pdf “));
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader(“Content-Disposition”, “attachment; filename=” + strPDFpath);
Response.ContentType = “application/octet-stream”;
Response.WriteFile(Server.MapPath(“~/Admin/FINFOBASE_Form/MyPDF.pdf “));
Response.Flush();
Response.Close();
if (File.Exists(Server.MapPath(“~/Admin/FINFOBASE_Form/MyPDF.pdf “)))
{
File.Delete(Server.MapPath(Server.MapPath(“~/Admin/FINFOBASE_Form/MyPDF.pdf “)));
}
}
}
}
Hello Nam,
You have written wrong code for the below line:
PdfWriter.GetInstance(document, new FileStream(@”C:\Inetpub\wwwroot\FinfoBase\Admin\FINFOBASE_Form\MyPDF.pdf”, FileMode.Create));
pls change to :
PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
Pls assign path to the variable strPDFPath, and strHTMLPath while declaring, and use the same variable for all process it as below, and do not provide any static path during the process:
string strHTMLpath = Server.MapPath(”~/Admin/FINFOBASE_Form/MyHTML.htm”);
string strPDFpath = Server.MapPath(”~/Admin/FINFOBASE_Form/MyPDF.pdf”);
Hi… I have a problem with this code. It generates right the pdf file, but when I try to make another one, I get an error. It said that HTML file is still in use. What should I do to solve this?
Thanks in advance and sorry for my bad english.
Hi there Vijay,
To do this using Java you should check out iText which is the original Java implementation of the iTextSharp library.
Hope this Helps
Also to Lucía,
The reason the HTML file is still in use is because the HtmlTextWriter is never closed in the code. The easiest way to remedy this would be in the finally block add the line
htw.Close();
NOTE: you will also need to move the definition of htw out of the try block so that it is available inside the finaly block.
This should release any resources held by the HtmlTextWriter object and let you reuse the file.
🙂
How way can convert to multi languages.
Thanks.
Does anybody have an example of working project in C# if so please send it rared to my email kristofer8@o2.pl. Thx a lot in advance.
hi!
Thank you very much for your information about convert html to pdf but i can’t convert this especially another text for example can you help me how can ı convert in asp.net FCKEditor1.value convent to pdf with html tags like tables or msn smiles or normal text.
i need so much very urgently .
Thank for your interest
i am waiting for your return
Güncel Sarıman
”’ Html to pdf….
Dim document As Document
Dim strData As StringBuilder
Dim strHTMLpath As String = “”
Dim strPDFpath As String = “”
Dim sw As StringWriter
Dim strHtml As String = “”
Dim strWriter As StreamWriter
Dim styles As iTextSharp.text.html.simpleparser.StyleSheet
Dim HTMLWorker As iTextSharp.text.html.simpleparser.HTMLWorker
Dim objects As ArrayList
document = New Document(PageSize.A4, 80, 50, 30, 65)
strData = New StringBuilder(String.Empty)
strHTMLpath = Server.MapPath(“MyHTML.html”)
strPDFpath = Server.MapPath(“MyHTML.pdf”)
Try
sw = New StringWriter
sw.WriteLine(Environment.NewLine)
Dim i As Integer
strHtml += “”
For i = 0 To 70
strHtml += “”
strHtml += ” ภาษาไทย 111″
strHtml += ” ”
strHtml += ” ”
Next
strHtml += “”
strHtml += “”
strHtml += ” English 222″
strHtml += ” ”
strHtml += ” ”
strHtml += “”
strHtml += “”
strHtml += “”
strWriter = New StreamWriter(strHTMLpath, False, Encoding.UTF8)
strWriter.Write(“” + strHtml + “”)
strWriter.Close()
styles = New iTextSharp.text.html.simpleparser.StyleSheet
styles.LoadTagStyle(“ol”, “leading”, “16”)
PdfWriter.GetInstance(document, New FileStream(strPDFpath, FileMode.Create))
document.Add(New Header(iTextSharp.text.html.Markup.HTML_ATTR_STYLESHEET, “Style.css”))
”footer
Dim HeaderFooter As HeaderFooter
HeaderFooter = New HeaderFooter(New Phrase(“Page “), True)
HeaderFooter.Border = 1
document.Footer = HeaderFooter
document.Open()
document.NewPage()
”add picture
‘Dim gif1 = iTextSharp.text.Image.GetInstance(Server.MapPath(“../images/sponser/CIES_logo.gif”))
‘document.Add(gif1)
objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(New StreamReader(strHTMLpath, Encoding.Default), styles)
HTMLWorker.ParseToList(New StreamReader(strHTMLpath, Encoding.Default), styles)
Dim intK As Int32 = 0
For intK = 0 To objects.Count – 1
document.Add(objects(intK))
Next
Catch ex As Exception
Finally
document.Close()
Response.Write(strPDFpath)
Response.ClearContent()
Response.ClearHeaders()
Response.AddHeader(“Content-Disposition”, “attachment; filename=” + strPDFpath)
Response.ContentType = “application/octet-stream”
Response.WriteFile(strPDFpath)
Response.Flush()
Response.Close()
End Try
nothing was done by using this code….
Hi, I tried this example. However i couldn’t see style sheet getting applied. what could be the reason? I have checked the html file bring generated, it had style sheet applied.
oas’s code works well. I want to create a asp webform using vb.net, when click a button on the page, a table on the page will be saved to a pdf file, how can I change the code, like change strHTMLpath to table ID. Thanks.
[…] January 23, 2009 at 1:40 pm (Asp.Net, HTML) https://hspinfo.wordpress.com/2008/01/12/how-to-convert-html-content-to-pdf-file/#comment-193 […]
Hello, i’m looking to find a list of the attributes that you can use with the StyleSheet.LoadTagStyle and their definition.
Is that available anywhere? Specifically, other than controlling font and font size i want to control vertical whitespace and indentation. the “leading” tag almost does this, but does not quite do what i hoped for.
thanks,
Niall Little
Hi, cool site, good writing 😉
When i try to use the above code I get the following error:
Control ‘gvSerchResult’ of type ‘GridView’ must be placed inside a form tag with runat=server.
Can anybody suggest a solution for this.
Rakesh,
I had the same problem and you have to override the VerifyRenderingInServerForm Method. Like this:
public override void VerifyRenderingInServerForm(Control control)
{
}
Greetz,
Thijs
Hi hspinfo,
I tried your code, and it works fine for me .
Thanks a lot.
Excellent work done By You.
Hi, hspinfo
In PDF which has been generated from Html file, in page of PDF
“Untitled Page” is written and then the content is started.
any idea How To Solve this problem?
Thanks
hello everybody,
I used ur code to convert html file into pdf in some application,
but I notice I haven’t the class Markup in my iTextSharp, is this corrupt one? or should I consider something else?
Hi every body !!!
Thank’s a lot !!! but i can’t found “using FinfoBase.Engine;” Help me.
Now who can tell me how can convert .doc file tho .pdf in asp.net
I’m thank;s you so much !!!.
Hi, I tried this example. However i couldn’t see style sheet getting applied. what could be the reason?.
Hi, I tried this example. However i couldn’t see style sheet getting applied. what could be the reason? I have checked the html file bring generated, it had style sheet applied.
Hi,
Iam using the code given by you for converting my web page to pdf but iam having the problem of binding my gridview because iam using data list inside the datagrid for my data representation..and how to do this phenomenon…Please help me..My requirement is very urgent
i am using datalist..
i want to convert datalist to pdf format using itextsharp
My requirement is very urgent
hii , how i can convert pdf to html plz?
hi
when i m executing the code
Response.WriteFile(Server.MapPath(“~/” + strPDFpath));
i am getting error The process cannot access the file “path..\outputfiles\914532.pdf” because it is being used by another process.
please help
Hi,
Thank You for the Great Work.
I have report a where the asp.net tabled has rowspan in order to group the data., when i push this HTML to create PDF, the PDF is created in an unstructured manner., ie., no rowspan is applied.
Is there any way to work on this., (C#.net 2.0)
Thanks a Lot !!!
Very Gud Site. But i’ am getting a problem. I’ am not able to add image in my string builder. Or i closed the htw in the finally block but still it give me an error message “HTML file is used It is used by another Process”.
PLZ Help
Thanks a lot in Advance.
Hi Deepak.,
This might be because of the StreamReader is not closed.
‘In this Code might cause you the issue..’
objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
‘Rewrite the above as..’
StreamReader objStreamReader;
objStreamReader = new StreamReader(strHTMLpath, Encoding.Default);
objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(objStreamReader, styles);
objStreamReader.Close();
objStreamReader.Dispose();
Just give a try..
Happy Coding !!
Thanks Pradeep it’s working fine. But i’ am facing two more issues:
1. How we can Merge PDF Files
2. Can we add an image in our html string (Actually) it is not taking the path. It give me an exception “IT is not a virtual path”.
If you have any ideas send me the code.
Thanks a lot Buddy !!!..
Superb……..
Simply Superb….
This helps me a lot….
You done a marvelous work..
Thanks my friend….Thanks
In my output pdf the font sizes and other html formatting are coming through in the pdf but not font styles..
Registering a certain font for a tag like ariel for span tag wouldn’t make sense in my case since the user can enter different font families for style tags, for instance they have may the word Hello in ariel followed by the name in Courier font. Any ideas?
Thanks,
Hi, I am getting an error….
“Could not find a part of the path ‘C:\Registration_files\image001.gif’.”
I am converting Registration.html file which has its Registration_files folder in the same folder as the file is in. But still the code checks for files in “C:\Registration_files\..” a fixed location. I have never given this as default location.
Here is my code
ArrayList objects;
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
(It is giving exception on this line =>) objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(dir + ConvertFileName + “.html”), styles);
for (int k = 0; k < objects.Count; k++)
{
myDocument.Add((IElement)objects[k]);
}
hi
am am convert aspx page to PDf using your First code it generate html and pdf file successfully in Html format abd css is Ok but for Pdf formate are not Set alignment of table are not looking god 3 html page become 14 Pdf pages can you provide me any sugestion
panal in place of Grideview in panal there are multiple html table
please provide me solution
The code seems reasonable, though I have not seen output of this code, as I am getting error on below line of code, it says “illeagal characters in Path”
StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
any thoughts on that, why I am getting error?
I am able to save the HTML Grid to PDF, it is awesome. The html file generated “MyHTML” has the CSS applied, but the PDF does not. Any idea where I am making mistake?
why i am getting error
Control ‘grdCustomer’ of type ‘GridView’ must be placed inside a form tag with runat=server.
hi,
I convert HTML page to PDF by using your code and it helped me a lot. It works fine in Internet Explorer but give an error in Mozilla.
Error is showing in this line “objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);”
And the complete error is:
“An error occurred: System.FormatException: Input string was not in a correct format. at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal) at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info) at iTextSharp.text.html.simpleparser.HTMLWorker.LengthParse(String txt, Int32 c) at iTextSharp.text.html.simpleparser.HTMLWorker.StartElement(String tag, Hashtable h) at iTextSharp.text.xml.simpleparser.SimpleXMLParser.ProcessTag(Boolean start) at iTextSharp.text.xml.simpleparser.SimpleXMLParser.Go(TextReader reader) at iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style, Hashtable interfaceProps) at iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(TextReader reader, StyleSheet style)”
hi,
Now it is working also in Mozilla but there is alignment problem. All contents are writing left align forcefully in IE and right align forcefully in Mozilla but actual alignment in HTML is center.
Can u help me It is very argent .
Thank
Very nice posting. I found out what I was actually looking for.
i am getting an error in this code
“ArrayList objects =(ArrayList) iTextSharp.text.html.simpleparser.
HTMLWorker.ParseToList(new StringReader(strpath),styles);”
“cannot implicitly convert type ‘system.collections.generic.list to system.collections.arraylist “…
Pls Help , waiting for ur reply
i am getting an error in this code
“ArrayList objects =(ArrayList) iTextSharp.text.html.simpleparser.
HTMLWorker.ParseToList(new StringReader(strpath),styles);”
“cannot implicitly convert type ‘system.collections.generic.list to system.collections.arraylist “…
Pls Help , waiting for ur reply
I used ur code,i am getting error like ” Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.ArrayList'” at HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
Can some one help me,i need very urgent
As nice as this is written, it uses the same “coding tecnique” I found on several other sites.
objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
And it gets the same compile error for the above statement: Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.ArrayList’
I am using VS 2008 and iTextSharp.dll version 5.0.2.0. Any suggestions?
I got what i needed but i have image, styles in html file and spaces ( ) but when i convert to pdf the image size is not set and not aligned as html page.
i need solution for that.
for example my html file will be like
FAKTURA
can i use this in asp.net?
i have tried using this cod emany time but giving me mnay errors..
huhu
Great Post! You solved my months long problem. No need to buy those $300-500 libraries with this code in hand 🙂
nice article thanks for solution.
string htmltext =”Some text “;
Paragraph para = new Paragraph(htmltext,font);
document.Add(para);
do any one knows a solution? para do not show any html tag properties.
hello,
I am getting this error.
Error – Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.ArrayList’
plz help me..
its urgent
Nice article, but so much code!
As an alternative suggestion, the following example uses only ten lines of code to produce paged output using ABCpdf.NET, which fully supports HTML and CSS.
Doc theDoc = new Doc();
theDoc.Page = theDoc.AddPage();
int theID = theDoc.AddImageUrl(@”http://www.example.com/”);
while (true) {
if (!theDoc.Chainable(theID)) break;
theDoc.Page = theDoc.AddPage();
theID = theDoc.AddImageToChain(theID);
}
theDoc.Save(“output.pdf”);
theDoc.Clear();
The Error shows
objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
And it gets the same compile error for the above statement: Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.ArrayList’
Conversion HTML2pdf is right now my major case. I found some really good stuff down here. Thanks! Waiting for some more useful articles 🙂
Does anyone know how to translate this code to C# for windows applications? Please help out.
Thanks in advance.
nico
Hello,
Using the code above, I received the error “The number of columns in PdfPTable constructor must be greater than zero.” Please help out.
Thanks in advance.
your code is usefull,
plase let me know about below error in that line:
objects = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
Error :Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.ArrayList’
same error can not implicity
You have sent demo details i don’t understand when you get css in code.
you can sent demo with me.i need it.
please help me.
plz sent me this project.i need it for my work.
thanks.
Hi all! And thanks for this great code!
I have a problem… When I try to create a PDF file (using the code you posted), the PDF is created and saved. But, if I try to refresh page, and to re-run the code, this error appears:
System.IO.FileNotFoundException: Could not find file ‘D:\ ..PATH.. \FileName.pdf’.
How could I fix this problem?
THANKS!!
Daniel
//Document is inbuilt class, available in iTextSharp
Document document = new Document(PageSize.A4, 80, 50, 30, 65);
System.Text.StringBuilder strData = new System.Text.StringBuilder(string.Empty);
//I have provided Path for the HTML which will be generated from GridView content
string strHTMLpath = Server.MapPath(“MyHTML.html”);
//I have provided Path for the PDF file which will be generated from HTML content
string strPDFpath = Server.MapPath(“MyPDF.pdf”);
try
{
StringWriter sw = new StringWriter();
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
sw.WriteLine(Environment.NewLine);
HtmlTextWriter htw = new HtmlTextWriter(sw);
//gvSearchResult is a GridView, I have converted its content to HTML and will acquire final PDF file
/*Here i have set AllowPaging and AllowSorting property of GridView as false, As my aim is to get whole content of the GridView in a single table, by setting these properties and binding the GridView again will remove paging and sorting property from it. */
FormView1.AllowPaging = false;
fillFromView(Asso_code);
//Rendering the HtmlTextWriter
FormView1.RenderControl(htw);
/*Here i have set AllowPaging and AllowSorting property of GridView as true, As my aim is to get whole content of the GridView is now finished and I have acquire its content in HtmlTextWriter. Then by setting properties again I will get the original form of my GridView again*/
FormView1.AllowPaging = false;
fillFromView(Asso_code);
StreamWriter strWriter = new StreamWriter(strHTMLpath, false, Encoding.UTF8);
strWriter.Write(“” + htw.InnerWriter.ToString() + “”);
strWriter.Close();
strWriter.Dispose();
iTextSharp.text.html.simpleparser.
StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
styles.LoadTagStyle(“ol”, “leading”, “16,0”);
PdfWriter.GetInstance(document, new FileStream(strPDFpath, FileMode.Create));
document.Add(new Header(iTextSharp.text.html, “Style.css”));
document.Open();
ArrayList objects;
styles.LoadTagStyle(“li”, “face”, “garamond”);
styles.LoadTagStyle(“span”, “size”, “8px”);
styles.LoadTagStyle(“body”, “font-family”, “times new roman”);
styles.LoadTagStyle(“body”, “font-size”, “10px”);
document.NewPage();
objects = iTextSharp.text.html.simpleparser.
HTMLWorker.ParseToList(new StreamReader(strHTMLpath, Encoding.Default), styles);
for (int k = 0; k < objects.Count; k++)
{
document.Add((IElement)objects[k]);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
document.Close();
Response.Write(Server.MapPath("~/" + strPDFpath));
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=" + strPDFpath);
Response.ContentType = "application/octet-stream";
Response.WriteFile(Server.MapPath("~/" + strPDFpath));
Response.Flush();
Response.Close();
if (File.Exists(Server.MapPath("~/" + strPDFpath)))
{
File.Delete(Server.MapPath("~/" + strPDFpath));
}
}
The type or namespace name ‘Markup’ does not exist in the namespace ‘iTextSharp.text.html’ (are you missing an assembly reference?)
i am using iTextSharp.dll (version 5.1.3)
Hi hpsinfo am getting tow errors:
Error 1:The type or namespace name ‘Markup’ does not exist in the namespace ‘iTextSharp.text.html’
Error 2: Cannot implicitly convert type ‘System.Collections.Generic.List’ to ‘System.Collections.ArrayList’
how to solve these 2 errors thanks in advance….
You should supply the source code. I find that copying and pasting does not do what you suggest it does. But I have my own working version. I was really just trying to figure out how to incorrperate css in the pdf. I tried to link the page to a css page, did’t work, tried inline tags, didn’t work. tried styles.LoadTagStyle(“span”, “size”, “8px”);, and still nothing. I see that you were doing it. and seen several good comments but I could not get your project to run. How ever my does just does not take the css.
I like the valuable info you provide in your articles.
I’ll bookmark your weblog and take a look at once more here frequently. I am quite certain I’ll be told a lot of new stuff proper here!
Good luck for the next!