开发者

How To Adjust Font Size To Fill A Fixed Height Table Cell In iTextSharp

I am creating a PDF from iTextSharp for printing. I have varible length text that I would like to always be in the maximum font size to fill a 开发者_如何学Pythonfixed height table cell without wrapping out of sight. How can this be done?


You need to start by being able to measure the width of your text in the font that you have chosen. From the iTextSharp documentation:

Measuring text

Sometimes it's necessary to know the length of a certain piece of text. If you have created a BaseFont object, you can use the method: public float getWidthPoint(String text, float fontSize); So if you are using some barcode font with size 36 as in the previous example and you want to know how much space you need on the line to print this barcode, you just do: getWidthPoint("0123456789", 36f). The result is the width in points. There are 72 points in 1 inch. So if you have a result of 252 points (as in example 8), you can convert this to inches and centimeters like this: 252 / 72 = 3.5 inch * 2.54 = 8.89 cm

This will get you the width of your string with whatever font you have chosen as a default. You can then use the fixed width of your table cell to find the proper font size to fill the width of your cell without wrapping.

textWidth = getWidthPoint("sample text", originalFontHeight)
newFontHeight = (cellWidth / textWidth ) * originalFontHeight

Then just make sure that the new font's height (which is already given in points by the font size) does not exceed the height of your table cell.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜