gpt4 book ai didi

itextsharp - 如何调整字体大小以填充 iTextSharp 中固定高度的表格单元格

转载 作者:行者123 更新时间:2023-12-01 11:09:07 33 4
gpt4 key购买 nike

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

最佳答案

首先,您需要能够测量所选字体的文本宽度。来自 iTextSharp 文档:

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

这将为您提供您选择的默认字体的字符串宽度。然后,您可以使用表格单元格的固定宽度来找到合适的字体大小,以在不换行的情况下填充单元格的宽度。

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

然后只需确保新字体的高度(已由字体大小以磅为单位给出)不超过表格单元格的高度。

关于itextsharp - 如何调整字体大小以填充 iTextSharp 中固定高度的表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2087966/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com