开发者

Re-using a font object in iTextSharp

开发者 https://www.devze.com 2023-03-02 09:19 出处:网络
I am having trouble trying to define an iTextSharp Font object that I can re-use throughout a class. My first attempt was to use the FontFactory.GetFont method in 2 private methods that create PdfPCe

I am having trouble trying to define an iTextSharp Font object that I can re-use throughout a class.

My first attempt was to use the FontFactory.GetFont method in 2 private methods that create PdfPCell objects:

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:

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 开发者_JAVA百科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.


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).

Ah! I suspect you may be running into an ALIGNMENT issue.

Valid settings for VerticalAlignment:

Element.ALIGN_TOP
Element.ALIGN_MIDDLE
Element.ALIGN_BOTTOM

Element.ALIGN_CENTER is strictly for horizontal 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消