How to preserve CSS using iTextSharp? - c#

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

Related

Create a pdf file with textbox and CSS in 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();
}

Error in htmlparser cannot take string reader as input?

In the following code, Parse cannot take StringReader (sr) as input. How can I pass the string I have to Parse, then?
protected void LinkButton1_Click(object sender, EventArgs e)
{
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=UserDetails.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
string str = "<h1 title=’Header’ align=’Center’> Writing To PDF Using ASP.NET> <br><table align=’Center’><tr><td style=’width:100px;color:green’> <b>iTextSharp</b></td><td style=’width:100px;color:red’>mytestpdf</td></tr></table>";
StringReader sr = new StringReader(str);
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0.0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);//Parse cannot take input as string reader (sr) how to solve?
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
Why are you putting your html into a StringReader? You already have the html in a string, just use that.
htmlparser.Parse(str)
If you must use StringReader for some reason, use the ReadToEnd() method to get a data type HTMLWorker.Parse will accept:
htmlparser.Parse(sr.ReadToEnd())
I update the itextsharp 5.0.. ..
now it not showing errors..

Error in creating pdf file using iTextSharp

I have one Gridview in my asp .Net project (c#) and i want to export it to pdf
for this purpose i'm using iTextSharp as bellow:
public override void VerifyRenderingInServerForm(Control control)
{ /* Do nothing */ }
protected void Button2_Click1(object sender, EventArgs e)
{
Response.Clear(); //this clears the Response of any headers or previous output
Response.Buffer = true; //ma
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=DataTable.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
ASPxGridView1.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}
when i test it , i get this error:
The UNC path should be of the form \server\share
anybody pls help me?

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.

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