我正在使用 PdfSharpCore 以创建PDF文件。
我需要在 XRect 。我在网上找到了以下代码,用于了解如何垂直放置文本。
XRect
double lineHeight = font.GetHeight(); double cyAscent = lineHeight * font.CellAscent / font.CellSpace; double y = rect.Y + cyAscent; graphics.DrawString(text, font, brush, rect.X, y);
这似乎正是我想要的。然而,我现在已经使我的代码更加复杂,可以在同一行上使用不同的字体。有些字体可能比其他字体高,并且它们都应该具有相同的基线。
我正在努力理解如何调整上面的逻辑来处理这个案例。
简单的方法是做这样的事情。
double y = rect.Y + heightOfTallestFont; graphics.DrawString(text, font, brush, rect.X, y);
但这似乎比它应该打印的文本略低。
所以,我发现,如果我对行中最大的字体进行原始计算,那么较小的字体在相同的值下看起来很好。
XFont font = line.LargestFont; double lineHeight = font.GetHeight(); double cyAscent = lineHeight * font.CellAscent / font.CellSpace; double y = rect.Y + cyAscent; graphics.DrawString(text, font, brush, rect.X, y);