如何在使用 iTextSharp 版本 7 实际渲染之前模拟文本元素的宽度
How to simulate width of an text element before actual render with iTextSharp version 7
在我的一个要求中,我想在我的文本元素呈现在页面上之前就获得它的宽度。
在 iText 版本 5 中应用的方式是
(新块("Test Text", SomeFont)).GetWidthPoint()
但是在第 7 版中,我找不到与此匹配的任何内容。
我试过:
(new Paragraph("Some Text")).GetWidth(), 但是这个returns null
如能提供任何帮助,我们将不胜感激。
提前致谢!!
一个选项是像这样查询有问题的字体:
String text = ...;
float fontSize = ...;
PdfFont font = ...;
GlyphLine glyphLine = font.createGlyphLine(text);
int width = 0;
for (int i = 0; i < glyphLine.size(); i++)
{
Glyph glyph = glyphLine.get(i);
width += glyph.getWidth();
}
float userSpaceWidth = width * fontSize / 1000.0f;
System.out.printf("The width of '%s' is %s text space units.\n", text, width);
System.out.printf("For a font size %s, therefore, this is %s unscaled user space units.\n", fontSize, userSpaceWidth);
在我的一个要求中,我想在我的文本元素呈现在页面上之前就获得它的宽度。
在 iText 版本 5 中应用的方式是 (新块("Test Text", SomeFont)).GetWidthPoint()
但是在第 7 版中,我找不到与此匹配的任何内容。 我试过:
(new Paragraph("Some Text")).GetWidth(), 但是这个returns null
如能提供任何帮助,我们将不胜感激。
提前致谢!!
一个选项是像这样查询有问题的字体:
String text = ...;
float fontSize = ...;
PdfFont font = ...;
GlyphLine glyphLine = font.createGlyphLine(text);
int width = 0;
for (int i = 0; i < glyphLine.size(); i++)
{
Glyph glyph = glyphLine.get(i);
width += glyph.getWidth();
}
float userSpaceWidth = width * fontSize / 1000.0f;
System.out.printf("The width of '%s' is %s text space units.\n", text, width);
System.out.printf("For a font size %s, therefore, this is %s unscaled user space units.\n", fontSize, userSpaceWidth);