【问题标题】:ITextSharp using multiple font styles together. ie Bold, Underlined, italic... etcITextSharp 一起使用多种字体样式。即粗体、下划线、斜体...等
【发布时间】:2016-07-25 17:00:35
【问题描述】:

我正在尝试使用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】:

    如果您查看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();
            }
        }
    }
    

    此代码产生以下文本:

    【讨论】:

    • 我更新了上面的内容以显示可能的值及其结果。您发布的图表似乎不正确。
    • 优秀。谢谢。我不确定您描述的方法为什么不起作用。但有耐心我会再试一次
    猜你喜欢
    • 2012-07-20
    • 1970-01-01
    • 2010-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多