在C#问题中使用打印文档进行打印 [英] Print using print document in C# problem

查看:110
本文介绍了在C#问题中使用打印文档进行打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题1 =>
我正在打印客户收据,但是当我添加超过20种产品时,它会生成新页面,并且第一页的标题也会显示在新页面中,但是我希望它在新页面中仅显示产品详细信息,标题仅显示第一页,请告诉我该怎么办.

问题2 =>

当添加多于1页时,我使用热敏打印机进行打印,问题是它仅打印第一页并跳过其他页.但是我希望它在仅打印的一页中打印所有大于1页的页.通过热敏打印机.

问题编号=>
当我添加更多20产品时,它会不断生成新页面,而不会停止生成新页面

我尝试过的事情:

它是我的代码,用于打印客户收据

Problem No 1=>
I''m printing customer receipt but when i add more then 20 product then it generate new page and the header of 1st page also show in new page but i want it show only product details in new page header is show only first page please tell me how can i do this.

problem No 2=>

when more then 1 page are added then i take print using Thermal printer the problem is that it print only first page and skip the other pages .but i want it print all the pages that is more then 1 page in only one page that is printed by thermal printer.

Problem No =>
when i added more the 20 product then it continuously generate new page and not stop to generating new page

What I have tried:

Its my code that is use to print customer Receipt

private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //this method get data from database and fill in list
            FillListforprintingItems();

            e.Graphics.DrawString("Date :" +  DateTime.Now, new Font("Arial", 12, FontStyle.Bold),
            Brushes.Black, 200,50);
            e.Graphics.DrawString("------------------------------------------------------------------------------------",
            new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(9, 85));
            e.Graphics.DrawString("Product", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(10,100));
            //e.Graphics.DrawString("Qty", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(340, 100));
            e.Graphics.DrawString("Amount", new Font("Arial", 12, FontStyle.Bold), Brushes.Black, new Point(180, 100));
            e.Graphics.DrawString
            ("------------------------------------------------------------------------------------"
            ,new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(9,  115));
            //int ypos = 250;
            int startX = 10;
            int startY = 140;
            int offset = 40;
            for (int i = NumberOfItemPrintedSofar; i < _List.Count; i++)
            {
                NumberofExpensperPage++;
                if (NumberofExpensperPage <= 20)
                {
                    NumberOfItemPrintedSofar++;
                    if (NumberOfItemPrintedSofar <= _List.Count)
                    {
                        e.Graphics.DrawString(_List[i].Qty+" X " +_List[i].Name, new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX, startY);
                        e.Graphics.DrawString(Convert.ToString("Vat % :"+_List[i].vateparecent), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX, startY + 20);
                        string colorsize = _List[i].sizcolor;
                        if (colorsize != "")
                        {
                            e.Graphics.DrawString(Convert.ToString(colorsize), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX, startY + 40);
                        }
                        e.Graphics.DrawString(_List[i].amountwithvat.ToString(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX + 170, startY);                    
                        // e.Graphics.DrawString(Convert.ToString( _List[i].Qty), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, startX + 330, startY);
                        if (colorsize != "")
                        {
                            e.Graphics.DrawString
                          ("----------------------------------------------------------------------------------",                     
                         new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX, startY = startY + 55));
                        }
                        if (colorsize == "")
                        {
                            e.Graphics.DrawString
                       ("----------------------------------------------------------------------------------",
                      new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX, startY = startY + 35));

                        }
                        startY += 15;
                    }
                    else
                    {
                        e.HasMorePages = false;
                    }
                }
                else
                {
                    NumberofExpensperPage = 0;
                    e.HasMorePages = true;
                    return;
                }
            } 
            //e.Graphics.DrawString("----------------------------------------------------------------------------------",
            //new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX, startY));
            e.Graphics.DrawString("Total Amount :" + TotalAmounttextBox.Text.Trim(), new Font("Arial", 12, FontStyle.Regular), Brushes.Black, new Point(startX + 10 , startY + 30));
            //rest thhe veriable
            NumberofExpensperPage = 0;
            NumberOfItemPrintedSofar = 0;
            _List.Clear();           
        }

推荐答案

因此,请检查您的页码(我认为该页码基于NumberOfItemPrintedSofar),并且仅在第一页上打印页眉.

并且帮自己一个忙:不要轻易创建Font对象-它们每个都使用Handles,这些Handle是稀有资源,只有在处理Font对象时才释放它们.而且,只要一直创建新实例,就会在很长时间内耗尽您的Handles,而内存耗尽之前,GC会自动处理它们.

为您使用的每种字体创建一个样本-从我的看来,您只需要两个-并将它们保留在基于类的私有变量中-您的代码将更快,并且不会因内存不足"错误而崩溃句柄已耗尽.
So check your page number (which I assume is based on NumberOfItemPrintedSofar) and only print the header on the first page.

And do yourself a favour: don''t create Font objects willy nilly - they each use Handles which are scarce resources that are only released when the Font object is Disposed. And just creating new instances all the time will run you out of Handles long, long before your run out of memory and the GC kicks in to automatically dispose them.

Create one sample of each font you use - and from what I can see you only need two - and keep them in class based private variables - your code will be faster, and won''t crash with "out of memory" errors when the Handles are exhausted.


这篇关于在C#问题中使用打印文档进行打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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