无论我是否使用 XTextFormatter,我都会收到同样的错误,即 LayoutRectangle 的高度必须为 0 或类似的东西。
new PdfSharp.Drawing.Layout.XTextFormatter(_gfx).DrawString(text
, new PdfSharp.Drawing.XFont(fontName, fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle)
, new PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(foreColour))
, new PdfSharp.Drawing.XRect(new PdfSharp.Drawing.XPoint(xPos, yPos), new PdfSharp.Drawing.XPoint(xLimit, yLimit))
, PdfSharp.Drawing.XStringFormats.Default);
fontStyle的类型为 System.Drawing.FontStyle foreColour的类型为 System.Drawing.Color 我已经从 PdfPage 中预定义了_gfx ,其中Orientation = Landscape,Size = Letter xPos和yPos是 double 类型的参数,与xLimit和yLimit相同。
我收到 LayoutRectangle 的高度必须为零 (0) 的运行时错误...
根据定义,矩形意味着有一个高度,否则称它为一条线!没看懂!...
我直接尝试了XGraphics.DrawString()方法,我得到了同样的错误。看来我不能使用 LayoutRectangle 但必须手动管理文本适合所需区域。
var textFont = new PdfSharp.Drawing.XFont(fontName, fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle);
while (xPos + _gfx.MeasureString(text, textFont).Width > xLimit)
textFont = new PdfSharp.Drawing.XFont(fontName, --fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle);
while (yPos + _gfx.MeasureString(text, textFont).Height > yLimit && fontSize > 0)
textFont = new PdfSharp.Drawing.XFont(fontName, --fontSize, (PdfSharp.Drawing.XFontStyle)fontStyle);
_gfx.DrawString(text
, textFont
, new PdfSharp.Drawing.XSolidBrush(PdfSharp.Drawing.XColor.FromArgb(foreColour))
, new PdfSharp.Drawing.XPoint(xPos, yPos));
虽然 yPos 变量值是完全相同的值!
*yPos = Page.Height * .4093,页面高度的 40.93%。*
以下是我尝试做的一个例子:
“你好世界!” “你好世界!”
这就是我得到的:
"Hello World!"
“你好世界!”
而且由于不同的打印区域限制和字体大小以及不同的字体样式,我不能只将这些写成一个简单的句子,包括正确的空格数。