简体   繁体   English

在 iTextSharp 中重新使用字体 object

[英]Re-using a font object in iTextSharp

I am having trouble trying to define an iTextSharp Font object that I can re-use throughout a class.我在尝试定义可以在整个 class 中重复使用的 iTextSharp 字体 object 时遇到问题。

My first attempt was to use the FontFactory.GetFont method in 2 private methods that create PdfPCell objects:我的第一次尝试是在创建 PdfPCell 对象的 2 个私有方法中使用 FontFactory.GetFont 方法:

FontFactory.GetFont("helvetica", 8)

The problem with this is that only one of the methods prints the font correctly, the other method would not print the inner text of the cell.这样做的问题是只有一种方法可以正确打印字体,另一种方法不会打印单元格的内部文本。

Next I tried to create a static field in the class representing the font:接下来我尝试在代表字体的 class 中创建一个 static 字段:

private static Font _standardCellFont;

public static PdfPCell CreateTableHeaderCell(string cellText, int colspan = 1)
    {
        var innerCellText = new Phrase(cellText, PdfSupport._standardCellFont);
        innerCellText.Font.Color = BaseColor.WHITE;

        return new PdfPCell(innerCellText)
                       {
                           Colspan = colspan,
                           PaddingLeft = 5,
                           PaddingRight = 5,
                           FixedHeight = 10,
                           BackgroundColor = BaseColor.BLACK,
                           VerticalAlignment = Element.ALIGN_CENTER
                       };
    }

public static PdfPCell CreateTableCell(string cellText, int colspan = 1, int rowspan = 1, bool bold = false, float minHeight = 0)
    {
        var cellInnerText = 
            bold ? 
            new Phrase(cellText, FontFactory.GetFont("helvetica", 8, Font.BOLD)) :
            new Phrase(cellText, PdfSupport._standardCellFont);

        return new PdfPCell(cellInnerText)
                   {
                       Border = Rectangle.NO_BORDER,
                       Colspan = colspan,
                       Rowspan = rowspan,
                       PaddingTop = 1,
                       PaddingBottom = 1,
                       PaddingLeft = 5,
                       PaddingRight = 5,
                       MinimumHeight = minHeight,
                       VerticalAlignment = Element.ALIGN_CENTER
                   };
    }

The result of this is that neither of the methods print the cell text.这样做的结果是这两种方法都不打印单元格文本。

I don't think I really understand what is happening when I create a font object, my goal is to be able to define a few common fonts and basic properties (Size, Bold) that I can then re-use throughout my class, however from my usage it appears that a font object cannot be persisted in this way.我不认为我真的明白当我创建字体 object 时发生了什么,我的目标是能够定义一些常见的 fonts 和基本属性(大小、粗体),然后我可以在整个 ZA2F2ED4F8EBC016ABCBDCC2 中重新使用它们,从我的使用来看,字体 object 似乎不能以这种方式持久化。

How strange.多么奇怪。 They can, and SHOULD, be shared.它们可以而且应该被共享。 Are the characters you're trying to draw all in WInAnsiEncoding (mostly the first 256 characters of Unicode).您尝试在 WInAnsiEncoding 中绘制的所有字符(主要是 Unicode 的前 256 个字符)。

Ah.啊。 I suspect you may be running into an ALIGNMENT issue.我怀疑您可能遇到了 ALIGNMENT 问题。

Valid settings for VerticalAlignment: VerticalAlignment 的有效设置:

Element.ALIGN_TOP
Element.ALIGN_MIDDLE
Element.ALIGN_BOTTOM

Element.ALIGN_CENTER is strictly for horizontal alignment. Element.ALIGN_CENTER严格用于水平 alignment。 I'm not sure that would make your text vanish , but it's certainly worth looking into.我不确定这会让你的文字消失,但它当然值得研究。

PS: I believe you can even reuse Font and BaseFont objects across documents, though I haven't tried it myself. PS:我相信您甚至可以跨文档重用 Font 和 BaseFont 对象,尽管我自己没有尝试过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM