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

谁都会走 提交于 2019-12-04 13:12:25

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.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!