Question

I have been trying out the following code on c# using itextsharp

Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35); 
    PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~/Test.pdf"), FileMode.Create));

    doc.Open();

    HTMLWorker html = new HTMLWorker(doc);
    StyleSheet css = new StyleSheet();

    css.LoadTagStyle("div", "color", "red");
    html.Parse(new StringReader("<div>Sample text</div>"));
    css.LoadTagStyle("div", "color", "red"); 
    html.SetStyleSheet(css);
    doc.Close();

The test is however displayed in simple plain black.

Was it helpful?

Solution

The first answer is the String should be in HTML format. And the second answer is HTMLWorker does not support CSS in this way.

You can use XMLWorker to achieve your goal.

    public static void pdfWithCSS()
    {
        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~/TestWithCSS.pdf"), FileMode.Create));
        HtmlPipelineContext htmlContext = new HtmlPipelineContext(null);

        htmlContext.SetTagFactory(Tags.GetHtmlTagProcessorFactory());

        //create a cssresolver to apply css
        ICSSResolver cssResolver = XMLWorkerHelper.GetInstance().GetDefaultCssResolver(false);

        cssResolver.AddCss("div{color: red;}", true);
        cssResolver.AddCss("h1{color: green;}", true);

        //Create and attach pipline, without pipline parser will not work on css
        IPipeline pipeline = new CssResolverPipeline(cssResolver, new HtmlPipeline(htmlContext, new PdfWriterPipeline(doc, writer)));

        //Create XMLWorker and attach a parser to it
        XMLWorker worker = new XMLWorker(pipeline, true);
        XMLParser xmlParser = new XMLParser(worker);

        //All is well open documnet and start writing.
        doc.Open();
        string htmltext = "<html><body><h1>Heading in Green</h1><div>This is a div content. It should look red.</div></body></html>";
        xmlParser.Parse(new StringReader(htmltext));

        //Done! close the documnet
        doc.Close();
    }

But even if you want to use HTMLWorker then you have to provide your CSS attribute in the element itself with style.

See the example bellow:

    public static void pdfInLineCSS()
    {
        Document doc = new Document(iTextSharp.text.PageSize.LETTER, 10, 10, 42, 35);
        PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~/Test.pdf"), FileMode.Create));

        doc.Open();

        HTMLWorker html = new HTMLWorker(doc);
        /*StyleSheet css = new StyleSheet();*/ //Not supported
        /*css.LoadTagStyle("div", "color", "red");*/
        //css.LoadStyle("div", "color", "green");
        string simple = "<html><body><h1 style='color: green;'>Heading in Green</h1><div style='color: red;'>Sample text in red color.</div></body></html>";
        html.Parse(new StringReader(simple));
        //css.LoadTagStyle("DIV", "color", "red");
        /*html.SetStyleSheet(css);*/
        doc.Close();

    }

Resouces:

    And many more can not list all but a BIG THANKS to ALL

Happy Coding :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top