Note that there are some explanatory texts on larger screens.

plurals
  1. PORe-using a font object in iTextSharp
    primarykey
    data
    text
    <p>I am having trouble trying to define an iTextSharp Font object that I can re-use throughout a class.</p> <p>My first attempt was to use the FontFactory.GetFont method in 2 private methods that create PdfPCell objects:</p> <pre><code>FontFactory.GetFont("helvetica", 8) </code></pre> <p>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.</p> <p>Next I tried to create a static field in the class representing the font:</p> <pre><code>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 }; } </code></pre> <p>The result of this is that neither of the methods print the cell text.</p> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload