3

无论我是否使用 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 xPosyPos是 double 类型的参数,与xLimityLimit相同。


我收到 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!" 

“你好世界!”

而且由于不同的打印区域限制和字体大小以及不同的字体样式,我不能只将这些写成一个简单的句子,包括正确的空格数。

4

2 回答 2

9

引用错误消息完全可以帮助其他人帮助您。

错误消息如下:

DrawString:使用 XLineAlignment.BaseLine 布局矩形的高度必须为 0。

文本将在一条线上对齐,因此高度必须为 0。是的,那是一条线。如果指定矩形,请使用不同的对齐方式。

TextLayout示例显示了如何格式化文本。

Graphics 示例还展示了如何布局文本(单行文本,没有自动换行符;TextLayout示例中显示的技术使用类自动处理换行符XTextFormatter)。

于 2009-08-31T08:35:09.783 回答
4

在试图弄清楚文本定位如何与 PdfSharp 一起工作时,我注意到 DrawString() 方法写在我们指定的 Y 坐标之上。

如果我想写在 (0, 100)(x, y),它指向左下角,而我认为这是左上角坐标。结果,我应该指定的文本字符串 Y 坐标是 100 + string.Height * .6。

PdfDocument pdfDoc = new PdfDocument();
PdfPage pdfPage = new pdfPage();
pdfPage.Size = PageSize.Letter;
pdfPage.Orientation = Orientation.Landscape;
pdfDoc.Pages.Add(pdfPage);

double posX = 0;
double posY = pdfPage.Height * .4093;
string helloString = "Hello"
string worldString = "World!"

XFont helloFont = new XFont("Helvetica", 25, XFontStyle.Regular);
XFont worldFont = new XFont("Helvetica", 270, XFontStyle.Bold);

using(var pdfGfx = XGraphics.FromPdfPage(pdfPage)) { // assuming the default Point UOM
    XSize helloStringSize = pdfGfx.MeasureString(helloString, helloFont);
    XSize worldStringSize = pdfGfx.MeasureString(worldString, worldFont);
pdfGfx.DrawString(helloString
    , helloFont
    , XBrushes.Black
    , posX
    , posY + helloStringSize.Height * .6
    , XStringFormats.Default);
pdfGfx.DrawString(worldString
    , worldFont
    , XBrushes.Black
    , pdfPage.Width * .3978
    , posY + (worldStringSize.Height + helloStringSize.Height) * .6
    , XStringFormats.Default);

}

您可能想知道为什么当我想将字符串写在 Y 坐标下方时,我只添加 60% 的字符串大小?那是因为字体的整个高度包括顶部的某种飞跃。因此,计算结果不会是预期的。另一方面,如果你需要一个飞跃,你不必关心一个飞跃。在我的特殊情况下,我不需要跳跃,所以我必须把它从弦的高度上取下来。

如果您觉得我的解释需要更准确的细节,请随时将它们添加为评论或随时通知我,以便我可以将它们包括在内。

谢谢!

于 2009-08-31T16:35:07.223 回答