1

I have used BaseFont.GetWidthPoint to calculate the width of a string using a particular BaseFont.

    //FontInfo is my own invention
    public float GetTextWidth(string text, FontInfo fontInfo)
    {
        return fontInfo.BaseFont.GetWidthPoint(text, fontInfo.Size);
    }

But when I switch to a Bold font it calculates wrong.


I realize what my problem is. I work on BaseFonts it does not have a notion of boldd font. When I write using a bold font, I set following:

if (fontInfo.IsBold)
{
    pdfContentByte.SetCharacterSpacing(1);
    pdfContentByte.SetRGBColorFill(66, 00, 00);  

    pdfContentByte.SetLineWidth((float)0.5);
    pdfContentByte.SetTextRenderingMode(PdfContentByte.TEXT_RENDER_MODE_FILL_STROKE);
}

Bold is only an attribute on my own FontInfo.

Based on this starting point, is there a way of calculating how much space a text takes up when painted with a "bold" font? Now it gives the space it takes up using a none-bold font.

TIA

Best regards Soeren D.

sdalby
  • 163
  • 1
  • 13
  • 1
    This works correctly for me, so tell me: how do you switch to a bold font? – Bruno Lowagie Jul 08 '14 at 15:49
  • *when I switch to a Bold font it calculates wrong* - how does it calculate wrong? – mkl Jul 08 '14 at 20:54
  • Thanks for your responses. I have added to the original question in order to be able to post code. – sdalby Jul 09 '14 at 07:29
  • 2
    *Based on this starting point, is there a way of calculating how much space a text takes up when painted with a "bold" font?* - As the spacing does not care about the rendering mode, the font drawn in bold has the same width as in non-bold. I see you setting the character spacing here, though. If you use a different character spacing value for the font drawn non-bold, you'll have to take that into account. [This answer](http://stackoverflow.com/a/16483429/1729265) quotes the PDF specification section defining how the character spacing influences the calculation of the character displacement. – mkl Jul 09 '14 at 12:29
  • Thanks, mkl (and bruno) :) – sdalby Jul 09 '14 at 14:06

0 Answers0