Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# itextsharp测量块的宽度/高度_C#_Itextsharp - Fatal编程技术网

C# itextsharp测量块的宽度/高度

C# itextsharp测量块的宽度/高度,c#,itextsharp,C#,Itextsharp,我正试图与iTextSharp进行精确对齐,但我一直未能做到,因为我无法找到一种方法来获得块或段落的宽度/高度值。如果我创建了一个具有特定字体、大小和文本的段落,那么它的尺寸应该是已知的,对吗 我知道默认的左/右/中心对齐对我来说最有用,但我有一些场景,知道尺寸最有用。有什么想法吗?您可以使用GetWidthPoint()获得块的宽度,块的高度通常是字体大小,除非您只使用小写字母。如果是这样,则可以使用BaseFont.GetCharBBox()手动测量字符 然而,段落是可流动的项目,它们取决于

我正试图与iTextSharp进行精确对齐,但我一直未能做到,因为我无法找到一种方法来获得块或段落的宽度/高度值。如果我创建了一个具有特定字体、大小和文本的段落,那么它的尺寸应该是已知的,对吗


我知道默认的左/右/中心对齐对我来说最有用,但我有一些场景,知道尺寸最有用。有什么想法吗?

您可以使用
GetWidthPoint()
获得块的宽度,块的高度通常是字体大小,除非您只使用小写字母。如果是这样,则可以使用
BaseFont.GetCharBBox()
手动测量字符

然而,段落是可流动的项目,它们取决于它们被写入的上下文,因此测量它们比较困难。(区块不会自动换行,但段落会自动换行。)衡量段落的最佳方法是将其写入
PdfCell
,然后衡量
PdfCell
。您不必实际将
PdfCell
添加到文档中。下面的链接对此做了更多解释


请使用下面的代码查看确切的尺寸

Font font = ...;
BaseFont baseFont = font.BaseFont;
float width = baseFont.GetWidthPoint(text, fontSize);
float height = baseFont.GetAscentPoint(text, fontSize) - baseFont.GetDescentPoint(text, fontSize);

谢谢,我还发现了如何获得正确的高度测量。BaseFont.GetAscentPoint()-BaseFont.GetDescentPoint()。也许你可以把这个添加到你的答案中,让它更接近你的答案。感谢您的帮助。您也可以使用
ColumnText
go(false)
,但我不确定这是否会得到宽度,只是高度。如果您只关心文本是否合适,ColumnText.go(false)真的更有用(如果这是您正在做的,它实际上比测量更容易)