Create a pdf file with textbox and CSS in c# - c#

I am attempting to convert a tag Label of my webpage to pdf.
While the pdf generation is working correctly, none of the css styles are being applied.
I've tried applying the styles one at a time, but that doesn't seem to work.
How can we create a pdf file from html with textbox controls with CSS styles ?
My code below.
Please help me, thank you in advance.
<asp:Label ID="UserID" runat="server" BorderStyle="Double"></asp:Label>
private void PdfFiles()
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + Decrypt(Request.QueryString["id"].ToString()) + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
panelView.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 40f, 10f, 10f, 10f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.End();
}

Related

Converting HTML to pdf and save it to given location without opening "Save As" or "Download" window

I am converting html into pdf. It is working fine. but when i try to save html into any location it gives me error Network path not found.
I am having some line of code
#region Trying to convert pdf
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
StringReader sr = new StringReader(htmlStringToConvert);
iTextSharp.text.Document pdfDoc = new iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
#endregion
I am getting error at htmlparser.Parse(sr).
Thanks in advance
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
DivMaster.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 20, 20, 10, 20);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter writer = PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
Add DivMaster (div) in design page and Add ur HTML code inside that DivMaster.
Note: Use % or px throughout HTML design.

How to preserve CSS using iTextSharp?

i have a header at the top of my front page like this:
i use iTextSharp code to generate its PDF ... but the resulting PDF do not contain this Header with black : instead some CSS is written in place of header like this:
How can i possibly fix this issue??
Code:
protected void BtnPDF_Click(object sender, EventArgs e)
{
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();
}
Have you tried to use inline css? Because HTML/CSS parser in iTextSharp is not complete. So it may not work as you want.
Also check out the LoadTagStyle property of StyleSheet in iTextSharp, see if it helps.
For more details about it, see this answer: https://stackoverflow.com/a/9616429/604232

Image in html page cannot be found by c# html parser

I am using itextsharp dll to print web page in PDF . I have a <img src="Images/flower.jpg" /> in my html file. The button click event below will render PDF fine when I don't have the image. With the image, I got this error:
Could not find a part of the path 'C:\Program Files (x86)\Common
Files\Microsoft Shared\DevServer\10.0\Images\flower.jpg'
Here is my code:
protected void Button1_Click(object sender, EventArgs e)
{
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();
}
The easiest solution should be to change:
<img src="Images/flower.jpg" />
to:
<img src="http://www.yourwebsite.com/Images/flower.jpg" />
Otherwise you can use Server.MapPath("Images/flower.jpg") to get the full URL by code. However that implies you having to know the relative path to the image (meaning access to the raw html) at compile-time, which makes your code less maintainable.

how to convert html page to pdf with css using itextsharp?

This is my code :
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);** //the exception here
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
the error is :
Unable to cast object of type 'iTextSharp.text.html.simpleparser.CellWrapper' to type
'iTextSharp.text.Paragraph'.
What is this exception ?
I don't know if this is the answer, but I found that iTextSharp is picky about having valid html. My tests had a table that was opened twice and never closed and that took about an hour for me to notice. The exception was very similar to the one you have there.
use can you A4 size width & height for html deigning .
use this set od code to create pdf from html.
String htmlText = "<div>Your HTML text here</div>";
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\outfile.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(htmlText));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile(Request.PhysicalApplicationPath + "\\outfile.pdf");
Response.Flush();
Response.Clear();

Applying Styles in PDF GridView using itextsharp

I'm trying to print a panel which contains data like labels and GridView to a PDF using itextsharp in my webpage. But if i'm not able to apply the styles to the GridView.
Can you help me??
I'm using both css and html styles to style my GridView.
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "inline;filename=" + filename + ".pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnl_print.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();
sr.Close();
hw.Close();
sw.Close();
How can I add styles to Gridview in Panel??
Panel ID is pnl_print
GridView ID is gv1
Can you Help me out ??
I am not sure if it possible to add an extranal css to the pdf with itextsharp, but you can always create a css with a functions frovided by itextsharp using something like this, rewriting the style in the itextsharp css class.
StyleSheet styles = new StyleSheet();
styles.LoadTagStyle("p", "size", "10pt");
styles.LoadTagStyle("p", "color", "#0000ff");

Categories