我有一个程序,使用C#中的PDFsharp手动生成PDF。虽然这相当繁琐,但我必须使用它,并且接近完成任务。只剩下一个问题。
问题:我想知道在Arial字体大小为多少时,给定字符的宽度是多少。
我正在努力寻找更精确的文本换行方法。现在,一个像素框的宽度被定义为盒子的宽度,然后在该盒子中写出一个字符串。我猜测能够适合于盒子中的字符串的最大长度,并且有些视觉上的怪异会不时出现。
有什么帮助吗?
谢谢
TextRenderer
类的MeasureText
方法:TextRenderer.MeasureText("some text", new Font("Arial", 1.0f, FontStyle.Regular))
Size
实例,其中包含Width=12, Height=2
。gfx.MeasureString(token, this.font).Width
来测量字符串的宽度。XGraphics.MeasureString(string s, Font f)就可以搞定了。
//l_Page is of type PdfPage
var l_Graphics = XGraphics.FromPdfPage( l_Page );
var l_TitleFont = new Font( "Arial", 15f, GraphicsUnit.World )
var l_Title = "Hallo Welt";
//l_TitleSize will be of type XSize and has properties for Width and Height
var l_TitleSize = l_Graphics.MeasureString( l_Title, l_TitleFont );