收据在打印文档中有重叠文本
Posted
技术标签:
【中文标题】收据在打印文档中有重叠文本【英文标题】:Receipt has overlapping text in Print Document 【发布时间】:2020-09-21 05:25:39 【问题描述】:我正在我的 POS 项目中创建打印收据并附上输出图片
我的问题是描述、数量、价格、金额的重叠。 如何在此代码的下一行显示数量和价格和金额?
e.Graphics.DrawString("Description" , new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(0, 260));
e.Graphics.DrawString("Qty" , new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(120, 260));
//e.Graphics.DrawString("UM" , new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(190, 190));
e.Graphics.DrawString("Price", new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(155, 260));
e.Graphics.DrawString("Amount" , new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(205, 260));
e.Graphics.DrawString("-------------------------------------------", new Font("trebuchet ms", 12, FontStyle.Regular), Brushes.Black, new Point(0, 275));
int yPos = 300;
foreach (var i in TempVal)
e.Graphics.DrawString(i.Particular + " (" + i.UM + ")", new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(0, yPos));
e.Graphics.DrawString(i.Qty, new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(120, yPos));
//e.Graphics.DrawString(i.UM, new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(190, yPos));
e.Graphics.DrawString(i.Price +".00", new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(155, yPos));
e.Graphics.DrawString(i.Total + ".00", new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(205, yPos));
yPos = yPos + 20;
e.Graphics.DrawString("-------------------------------------------", new Font("trebuchet ms", 12, FontStyle.Regular), Brushes.Black, new Point(0, yPos));
e.Graphics.DrawString("Total Amount: php " + label3.Text.Trim(), new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(0, yPos+20));
e.Graphics.DrawString("-------------------------------------------", new Font("trebuchet ms", 12, FontStyle.Regular), Brushes.Black, new Point(0, yPos+35));
e.Graphics.DrawString("Cash Tendered: Php " + label4.Text.Trim(), new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(0, yPos + 55));
e.Graphics.DrawString("Change: Php " + lblTotal.Text.Trim(), new Font("trebuchet ms", 10, FontStyle.Regular), Brushes.Black, new Point(0, yPos + 75));
e.Graphics.DrawString("-------------------------------------------", new Font("trebuchet ms", 12, FontStyle.Regular), Brushes.Black, new Point(0, yPos+95));
【问题讨论】:
您好 ZniperpH,您是否厌倦了使用换行符 (\n) 或制表符 (\t) 您的代码打印正常。问题出在其他地方。您的文本似乎太长并且与其他“列”重叠。 不相关,但为了更好的可读性,您可以考虑使用var font = new Font("trebuchet ms", 10, FontStyle.Regular);
之类的东西,然后仅将此变量用作许多 DrawString
调用的参数。
@rizu,尝试换行和测量字符串,但没有任何反应
@LarsTech,是的,这是我的问题,我想知道文本的长度是否很长,其他列将发送到下一行,我卡了将近一个小时的搜索解决方案
【参考方案1】:
这将按照您的要求“在下一行显示数量、价格和金额”:
using (var f = new Font("trebuchet ms", 10, FontStyle.Regular))
int yPos = 300;
foreach (var v in TempVal)
e.Graphics.DrawString(v.Particular + " (" + v.UM + ")", f, Brushes.Black, new Point(0, yPos));
yPos += 20;
e.Graphics.DrawString(v.Qty, f, Brushes.Black, new Point(120, yPos));
e.Graphics.DrawString(v.Price +".00", f, Brushes.Black, new Point(155, yPos));
e.Graphics.DrawString(v.Total + ".00", f, Brushes.Black, new Point(205, yPos));
yPos += 20;
请注意,这仅适用于TempVal
的记录太少以至于它们都可以放在一页上的情况。如果有足够的记录需要更多页,则必须计算一页可以容纳多少行和split your output onto multiple pages。
【讨论】:
以上是关于收据在打印文档中有重叠文本的主要内容,如果未能解决你的问题,请参考以下文章