将样式应用于具有类名iTextSharp的div [英] Applying style to div having a class name iTextSharp

查看:117
本文介绍了将样式应用于具有类名iTextSharp的div的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将我的html字符串定义为:

i have defined my html string as:

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

要应用样式,我这样做

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");

如何为特定类指定样式?我正在使用iTextSharp dll生成pdf。

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

推荐答案

您需要创建一个实现 IFontProvider 实例。 XMLWorker附带了一个实现该类的类,因此您只需使用 XMLWorkerFontProvider 类并通过它注册字体。 Register()方法的第二个参数是可选的,但我建议您使用它来明确地为您的字体添加别名。

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.

一旦你有了,你可以使用长格式的 ParseXHtml(),它为HTML和CSS提供流。如果你从磁盘加载这两个中的任何一个,你应该检查编码。

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.

下面的代码是针对iTextSharp和XMLWorker 5.2.4测试的完整工作示例。有关详细信息,请参阅注释。

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

这篇关于将样式应用于具有类名iTextSharp的div的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆