如何调整字体大小以填充iTextSharp中的固定高度表格单元格 [英] How To Adjust Font Size To Fill A Fixed Height Table Cell In iTextSharp

查看:882
本文介绍了如何调整字体大小以填充iTextSharp中的固定高度表格单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从iTextSharp创建一个PDF进行打印。我有可变长度的文本,我希望始终使用最大字体大小来填充固定高度的表格单元格而不会看不见。如何做到这一点?

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 fixed height table cell without wrapping out of sight. How can this be done?

推荐答案

您需要首先能够以您所使用的字体测量文本的宽度已经选择。来自iTextSharp文档:

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:


测量文本

有时需要知道
某段文字的长度。如果你有
创建了一个BaseFont对象,你可以使用
方法:
public float getWidthPoint(String text,float
fontSize);因此,如果您使用一些尺寸为36的
条形码字体,如前面的
示例中所示,并且您想要知道
您需要多少空间才能打印此条形码,
打印此条形码,你只需:
getWidthPoint(0123456789,36f)。
结果是以磅为单位的宽度。
是1英寸72点。因此,如果您
的结果为252分(如
例8中所示),您可以将其转换为
英寸和厘米像这样:252
/ 72 = 3.5英寸* 2.54 = 8.89 cm

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.

这篇关于如何调整字体大小以填充iTextSharp中的固定高度表格单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆