C# StringTrimming.EllipsisCharacter 在 PrintDocument 中未能正确截断文本。

huangapple go评论75阅读模式
英文:

C# StringTrimming.EllipsisCharacter not properly truncating text in PrintDocument

问题

我正在构建一个C# WinForms应用程序,并正在处理一个PrintDocument来生成订单票据。订单明细中的每一行包括SKU、商品描述、价格和订购数量,我需要确保如果商品描述不适合定义的RectangleF内,它会被截断。我已经设置了如下格式:

StringFormat fmtNearTrimmed = new StringFormat();
fmtNearTrimmed.Alignment = StringAlignment.Near;
fmtNearTrimmed.LineAlignment = StringAlignment.Center;
fmtNearTrimmed.Trimming = StringTrimming.EllipsisWord;

然后,在PrintDocument中(在PrintPage事件中)如下使用它:

// 商品描述
e.Graphics.DrawRectangle(Pens.Black, 200, curPos, 550, defaultBoldFont.Height + 4);
rc = new RectangleF(200, curPos, 550, defaultFont.Height + 4);
e.Graphics.DrawString(list[curItem].ProductName, defaultFont, Brushes.Black, rc, fmtNearTrimmed);

根据我所了解的一切,这应该起作用,当文本到达矩形的末端时,文本应该被截断。但实际情况并非如此。这是我看到的情况:

C# StringTrimming.EllipsisCharacter 在 PrintDocument 中未能正确截断文本。

如您所见,太长无法适应RectangleF对象的商品描述应该被截断,但实际上没有。我错过了什么吗?

英文:

I am building a C# WinForms app, and I am working on a PrintDocument to generate an order ticket. Each line in the order detail includes a SKU, item description, price and quantity ordered, and part of what I need to is to make sure the item description is truncated if it doesn't fit inside the RectangleF defined for it. I have set up formatting like so:

StringFormat fmtNearTrimmed = new StringFormat ( );
fmtNearTrimmed.Alignment = StringAlignment.Near;
fmtNearTrimmed.LineAlignment = StringAlignment.Center;
fmtNearTrimmed.Trimming = StringTrimming.EllipsisWord;

I am then using this in the PrintDocument (in the PrintPage event) as follows:

// Item Description
e.Graphics.DrawRectangle ( Pens.Black, 200, curPos, 550, defaultBoldFont.Height + 4 );
rc = new RectangleF ( 200, curPos, 550, defaultFont.Height + 4 );
e.Graphics.DrawString ( list[curItem].ProductName, defaultFont, Brushes.Black, rc, fmtNearTrimmed );

From everything I've read, this should work, and the text should be trimmed when it reaches the end of the rectangle. But it doesn't. Here's what I see:

C# StringTrimming.EllipsisCharacter 在 PrintDocument 中未能正确截断文本。

As you can see, the item description that is too long to fit the RectnagleF object should be truncated, but it isn't. What am I missing here?

答案1

得分: 0

要禁用文本换行,请在设置StringFormat.FormatFlags属性或将其传递给采用此类型的构造函数时添加StringFormatFlags.NoWrap标志。

var fmtNearTrimmed = new StringFormat(StringFormatFlags.NoWrap)
{
    Alignment = StringAlignment.Near,
    LineAlignment = StringAlignment.Center,
    Trimming = StringTrimming.EllipsisCharacter
};

此外,根据我在附加图像中看到的内容,如果需要在此或其他打印作业中启用文本换行,请添加StringFormatFlags.LineLimit标志,以跳过不能完全打印和显示在格式化矩形内的行,如果其高度不足够。

英文:

To disable text wrapping, add the StringFormatFlags.NoWrap flag when you set the StringFormat.FormatFlags property or pass it to the constructor that takes this type.

var fmtNearTrimmed = new StringFormat(StringFormatFlags.NoWrap)
{
    Alignment = StringAlignment.Near,
    LineAlignment = StringAlignment.Center,
    Trimming = StringTrimming.EllipsisCharacter
};

Also, and according to what I see in the attached image, if you need to enable text wrapping elsewhere in this or other printing job, add the StringFormatFlags.LineLimit flag to skip the lines that does not perfectly printed and displayed within the formatting rectangle if its height is not high enough.

huangapple
  • 本文由 发表于 2023年6月12日 02:37:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451980.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定