Applying style to div having a class name iTextSharp

元气小坏坏 提交于 2019-11-27 08:48:35

问题


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.


回答1:


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


来源:https://stackoverflow.com/questions/21106710/applying-style-to-div-having-a-class-name-itextsharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!