iTextSharp的使用多种字体样式在一起。即粗体,下划线,斜体...等

问题描述 投票:2回答:1

我试图用Itextsharp.dll(不知道哪个版本)写一个动态的PDF。直到我需要写一个短语与大胆,并强调一切都会很大。然而似乎iTextSharp的的字体类不允许此。它允许粗体/斜体,但不是粗体/下划线,斜体/下划线,或所有三个。你不能用任何其他样式结合下划线。这似乎是相当愚蠢的,不允许字体既强调了和别的东西。我已经到处看,看什么,提到它。有谁知道解决的办法还是我缺少明显的东西吗?

我通常会建立自己的字体,像这样。

iTextSharp.text.Font myFont = new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 9, iTextSharp.text.Font.BOLDITALIC, BaseColor.BLACK);

你可以看到第三个参数是一个整数标志着字体的FontStyle,但是没有可用的,使一些枚举下划线和粗体,下划线和斜体或全部三个。必须有一个办法做到这一点。我觉得很难相信,iTextSharp的不占下划线和粗体文字。有任何想法吗?

c# itext
1个回答
4
投票

如果你看一下定义BOLDITALIC你会看到:

public const int BOLDITALIC    = BOLD | ITALIC;

这表明你如何使用按位|or)算这些样式结合起来。你当然可以自由但要重新定义这些,但你通常都会看到他们使用的是这样的:

var myFont = new Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 9, Font.BOLD | Font.UNDERLINE, BaseColor.BLACK);

编辑

望着源,BOLD1UNDERLINE4,当你|在一起你5这是您发布相同的值。您可以测试使用下面的代码的所有5种模式的每一个组合。

//Create a test file on our desktop
var testFile = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf");

//Possible styles
var styles = new Dictionary<string, int>() {
    { "NORMAL" , iTextSharp.text.Font.NORMAL },
    { "BOLD" , iTextSharp.text.Font.BOLD },
    { "ITALIC" , iTextSharp.text.Font.ITALIC },
    { "UNDERLINE" , iTextSharp.text.Font.UNDERLINE },
    { "STRIKETHRU",  iTextSharp.text.Font.STRIKETHRU }
};

//Standard iText bootstrap
using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
    using(var doc = new Document()) {
        using (var writer = PdfWriter.GetInstance(doc, fs)) {
            doc.Open();

            //We're going to try every possible unique combination of constants, store the
            //previously used ones in this dictionary
            var used = new Dictionary<int, string>();

            //Fixed-number combination hack, just create 5 nested loops.
            foreach (var a in styles) {
                foreach (var b in styles) {
                    foreach (var c in styles) {
                        foreach (var d in styles) {
                            foreach (var g in styles) {

                                //Bitwise OR the values together
                                var k = a.Value | b.Value | c.Value | d.Value | g.Value;

                                //If we didn't previously use this OR'd value
                                if (!used.ContainsKey(k)) {

                                    //Get all of the unique names exclude duplicates
                                    var names = new string[] { a.Key, b.Key, c.Key, d.Key, g.Key }.Distinct().OrderBy(s => s).ToList();

                                    //NORMAL is the "default" and although NORMAL | BOLD is totally valid it just
                                    //messes with your brain when you see it. So remove NORMAL from the description
                                    //when it is used with anything else. This part is optional
                                    if (names.Count() > 1 && names.Contains("NORMAL")) {
                                        names = names.Where(n => n != "NORMAL").ToList();
                                    }

                                    //Merge our names into a comma-separated string
                                    var v = String.Join(", ", names);

                                    //Store it so we don't use it again
                                    used.Add(k, v);

                                    //Create a font using this loop's value
                                    var myFont = new iTextSharp.text.Font(iTextSharp.text.Font.FontFamily.TIMES_ROMAN, 12, k, BaseColor.BLACK);

                                    //Add it to our document
                                    doc.Add(new Paragraph(k.ToString() + "=" + v, myFont));
                                }
                            }
                        }
                    }
                }
            }

            doc.Close();
        }
    }
}

这段代码产生这样的文字:

enter image description here


推荐问答