Wil*_*ler 3 pdf layout drawstring pdfsharp
无论我是否使用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);
Run Code Online (Sandbox Code Playgroud)
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));
Run Code Online (Sandbox Code Playgroud)
虽然yPos变量值是完全相同的值!
*yPos = Page.Height*.4093,占页面高度的40,93%.*
这是我尝试做的一个例子:
"你好,世界!" "你好,世界!"
这就是我得到的:
Run Code Online (Sandbox Code Playgroud)"Hello World!""你好,世界!"
由于不同的打印区域限制和字体大小以及不同的字体样式,我不能将这些写入一个简单的句子,包括正确的空格数.
引用错误消息可以帮助其他人帮助您.
错误消息显示:
DrawString:使用XLineAlignment.BaseLine时,布局矩形的高度必须为0.
文本将在一行对齐,因此高度必须为0.是的,这是一条线.如果指定矩形,请使用其他对齐方式.
该TextLayout示例显示了如何设置文本格式.
Graphics示例还显示了如何布局文本(单行文本,没有自动换行符; TextLayout示例中显示的技术使用XTextFormatter类自动处理换行符).