How to preserve CSS using iTextSharp?


Tags: c#,html,asp.net,css,itextsharp

Problem :

i have a header at the top of my front page like this: enter image description here

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:

enter image description here

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


Solution :

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: http://stackoverflow.com/a/9616429/604232


    CSS Howto..

    How to “clear” a CSS fake table row?

    CSS: how to have reposition relative to a centered background image

    How can I place two grid items above each other using Susy?

    How to link Body/Html Backgrounds with CSS in Ipad/Iphone (safari)

    How to read and show accelerometer data (server side) using JavaScript and Node.js

    How to output different images depend on user screen?

    How do I insert an image in HTML so that its fills up the entire screen regardless of screen size?

    How do I get the (element with the) highest CSS z-index in a document?

    Using CSS to style a Sencha Touch form - how do I find the elements?

    How to change the value depend on css animation?

    CSS - How to remove 2nd vertical scroll bar without changing anything else?

    How can I give a child element of one parent a higher z-index than another parent?

    How to pop a dialog box /iframe/Lightbox like one below above the webpage content [closed]

    How to show hidden part of an image which is inside a fixed height div, using just the CSS?

    how to set css width equal to length of longest text

    How to align div in body to stay with specific height and width

    How should you comment css? [closed]

    How to close popup

    How to push down second dropdown when open first ?

    CSS Border Color: How to set bottom border color?

    How do I make footer take 100% of the width of the webpage?

    How to use mapbox.js as a background so that html content and Navbar stay visible on top?

    In Wordpress how can I create a page which has 3 col-md-4 blocks in a container?

    how can we use and control Gradient

    How to create border around jquery mobile webpage?

    How to display clicked on/off SVG icon from admin panel page and display result on a different page

    How to maintain consistent text and image positioning using CSS

    How could I add two separate text styles to a button in a form in CSS?

    how to change css of n number of div element using jquery

    Vim: How would I recursively run “:sort” for everything between curly braces?