我需要用word wrapp中间字符串 [英] I need to word wrapp middle string

查看:74
本文介绍了我需要用word wrapp中间字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何格式化此字符串



i在我的打印机EPSON上有70mm的纸张大小。

需要使用PrintDocument类

i只需要用e.Graphics打印这个字符串



如果字符串比纸张尺寸长,我需要像这样使用



 3 x cola + burger + somelong 12,00 
itemname with modifiers etc //





我有一个属性物品

数量= 3

itemName =可乐+汉堡+ somelongstring

价格= 12,00



i需要解决方案 - 这个解决方案无法正常工作。

我总是需要联盟第一行文本价格

然后如果我们向左走,我需要Quantity作为单独的字符串+x+ ItemName作为其他字符串,但如果该字符串太长则需要word wrapp。所以我需要解决方法如何将中间字符串包装成字符串并按照我在预览时显示...



我尝试过: < br $>


 public string FormatLineItem(int lineType,float linewidth,int quantity,string name,string amount)
{
float qtyWidth = 3;
float amtWidth = 5;
float nameWidth = linewidth - (qtyWidth + amtWidth);
float padWidth =(linewidth + qtyWidth + 3) - nameWidth;
string result = string.Empty;

switch(lineType)
{
case 1:// data
result = string.Format({0:#0} x {1} {2 :##。#0},quantity,name.PadRight((int)padWidth),amount);
休息;
case 2:// header
result = string.Format(QTY {0} AMOUNT,name.PadRight((int)padWidth));
休息;
case 3:// footer
result = string.Format({0:##。#0},amount).PadLeft((int)(linewidth - amtWidth));
休息;
默认值:
break;
}

返回结果;
}

解决方案

使用WPF创建收据的视觉,缩放到70mm(发送到打印机)。



将长字符串放在带有文本换行的TextBlock中。



你可以指定如何破解(单词)。



如果你的空间用完了它甚至可以打印......。

使用PrintDocument时,使用Graphics.DrawString对其进行格式化 - 如果没有图形上下文以及字体和笔刷数据,则无法格式化,因为它们是单个字符大小和字距的基础,这会影响文本必须包装的点。



DrawString有几个重载,包括一对接受RectangleF的重载: Graphics.DrawString方法(System.Drawing)| Microsoft Docs [ ^ ]当您使用它时,它将自动换行以适合矩形的水平宽度,并且只有在达到垂直限制时才会被截断。



除非你总是使用非比例字体和特定的线宽,否则在没有实际字体和画笔信息的情况下尝试做wordwrap是行不通的。


how do i format this string

i have 70mm paper size on my printer EPSON.
need to use PrintDocument class
i just need to print with e.Graphics this string

if string is longer then paper size i need to bee like this

3 x cola + burger + somelong        12,00
itemname with modifiers etc//   



I have one class Item with properties
quantity = 3
itemName = cola + burger + somelongstring
price = 12,00

i need solution for this - this solution doesn't work the job.
Always i need allign to right in first line of text the price
then if we go left i need Quantity as separate string + "x" + ItemName as some other string but need to word wrapp if that string is too long. So i need solution how to word wrapp middle string into string and get as i show on preview..

What I have tried:

public string FormatLineItem(int lineType, float linewidth, int quantity, string name, string amount)
        {
            float qtyWidth = 3;
            float amtWidth = 5;
            float nameWidth = linewidth - (qtyWidth + amtWidth);
            float padWidth = (linewidth + qtyWidth + 3) - nameWidth;
            string result = string.Empty;

            switch (lineType)
            {
                case 1: // data
                    result = string.Format("{0:#0}x {1} {2:##.#0}", quantity, name.PadRight((int)padWidth), amount);
                    break;
                case 2: // header
                    result = string.Format("QTY {0} AMOUNT", name.PadRight((int)padWidth));
                    break;
                case 3: // footer
                    result = string.Format("{0:##.#0}", amount).PadLeft((int)(linewidth - amtWidth));
                    break;
                default:
                    break;
            }

            return result;
        }

解决方案

Use WPF to create a "visual" of the receipt that scales to 70mm (that one sends to the printer).

Put the "long string" in a TextBlock with "text wrapping" on.

You can specify how to "break" (on words).

It can even print "..." if you run out of space altogether.


When you use PrintDocument, you use Graphics.DrawString to format it - you can't formatit without the Graphics Context and the font and brush data as they are fundamental to the size and kerning of individual characters, which affects the point at which text must wrap.

And DrawString has several overloads, including a couple which accept a RectangleF: Graphics.DrawString Method (System.Drawing) | Microsoft Docs[^] When you use this, it will be automatically wrapped to fit within the horizontal width of the rectangle and only truncated when it reaches the vertical limit.

Trying to "do wordwrap" without the actual font and brush information won't work, unless you always use a non-proportional font, and a specific line weight.


这篇关于我需要用word wrapp中间字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆