2

i have defined my html string as:

            string html = @"
                <html><body>    
                    <div class='class1'>My Text</div>    
                    </body></html>   
                    ";

To apply style, I am doing this

StyleSheet style = new StyleSheet();

            style.LoadTagStyle("class1", HtmlTags.FACE , "PATH" + "CustomFont.ttf");

This does not work. However, using this applies the font:

       style.LoadTagStyle(HtmlTags.DIV, HtmlTags.FACE , "PATH"+"CustomFont.ttf");

How to specify style to a particular class? I am generating pdf using iTextSharp dll.

user2877090
  • 73
  • 2
  • 7

1 Answers1

2

You need to create an instance of a class that implements the IFontProvider instance. XMLWorker ships with a class that implements that already so you can just use the XMLWorkerFontProvider class and register your fonts through that. The second parameter to the Register() method is optional but I recommend that you use it to explicitly give your font an alias.

Once you have that you can use the long form of ParseXHtml() which takes streams for both the HTML and CSS. If you're loading either of these two from disk you should check the encodings.

The below code is a full working example tested against iTextSharp and XMLWorker 5.2.4. See the comments for further details.

//File to output
var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

//Standard PDF setup, nothing special here
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using (var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {

            //Open our document for writing
            doc.Open();

            //Our basic HTML
            var html = @"<html><body><div class=""class1"">My Text</div></body></html>";

            //Fully qualified path to our font
            var myFont = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ALGER.TTF");

            //Register our font and give it an alias to reference in CSS
            var fontProv = new XMLWorkerFontProvider();
            fontProv.Register(myFont, "AlgerianRegular");

            //Create our CSS
            var css = @".class1{font-family: AlgerianRegular; color: #f00; font-size: 60pt;}";

            //Create a stream to read our HTML
            using (var htmlMS = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html))) {
                //Create a stream to read our CSS
                using (var cssMS = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css))) {
                    //Get an instance of the generic XMLWorker
                    var xmlWorker = XMLWorkerHelper.GetInstance();

                    //Parse our HTML using everything setup above
                    xmlWorker.ParseXHtml(writer, doc, htmlMS, cssMS, System.Text.Encoding.UTF8, fontProv);
                }
            }

            //Close and cleanup
            doc.Close();
        }
    }
}
Chris Haas
  • 53,986
  • 12
  • 141
  • 274
  • For future reference: not that there are no single quotes around the font-family name. I you add them, the xmlworker will not work. – KyorCode Oct 28 '14 at 13:11
  • I just tried the above code with and without quotes around `AlgerianRegular` against version 5.5.2 and both produced the correct output. – Chris Haas Oct 28 '14 at 13:15