使用打印机对话框以Windows窗体打印文本文档 [英] Printing a text document in windows form using a printer dialog box

查看:73
本文介绍了使用打印机对话框以Windows窗体打印文本文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行以下代码,但它会一直打印一个空白页:

I am running the following code, but it keeps printing a blank page:

     ;    PrintDocument1.DocumentName =" f:\ policy.txt"

        "PrintDocument1.DocumentName =(" \\\\
x3200\shares\VSApps\StockTransfer\stock_transfer_cover_sheet.txt")

      &NBSP ; PrintDialog1.Document = PrintDocument1

        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings

        PrintDialog1.AllowSomePages = True

       如果PrintDialog1.ShowDialog = DialogResult.OK则为
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings

            PrintDocument1.Print()

       结束如果

        PrintDocument1.DocumentName = "f:\policy.txt"
        'PrintDocument1.DocumentName = ("\\nx3200\shares\VSApps\StockTransfer\stock_transfer_cover_sheet.txt")
        PrintDialog1.Document = PrintDocument1
        PrintDialog1.PrinterSettings = PrintDocument1.PrinterSettings
        PrintDialog1.AllowSomePages = True
        If PrintDialog1.ShowDialog = DialogResult.OK Then
            PrintDocument1.PrinterSettings = PrintDialog1.PrinterSettings
            PrintDocument1.Print()
        End If

推荐答案

以下是我写的发票程序中的一些代码。

Here is some code from an invoice program I wrote.

抱歉有点长但它确实显示了如何详细地做。

Sorry its a bit long but it does show how to do it in detail.

        private void button2_Click(object sender, EventArgs e)
        {


            PrintDocument pd = new PrintDocument();



            
            string res;
            System.Windows.Forms.PrintDialog printDialog1 = new System.Windows.Forms.PrintDialog();
            res = printDialog1.ShowDialog().ToString();
            if (res != "OK")
                return;

            pd.PrinterSettings = printDialog1.PrinterSettings; //send data to selected print device






            printpagenumber = 0;
            printdx = 0;
            pd.PrintPage += new PrintPageEventHandler
               (this.pd_PrintPage);

            pd.Print();

        }



        private void pd_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e)
        {

            string str1;
            int cx;

            printFont = new Font("Arial", 10);
            Pen pn = new Pen(Color.Black);


            

//////////////////////////////////////////////////////////////////////////////////////////////








            //calc last row
            int lastrow = 0;
            for (cx = 0; cx <= numberofitemsininvoice - 1; cx++)
            {
               
                if (Convert.ToString(dataGridView1.Rows[cx].Cells[0].Value) != "")
                    lastrow = cx;
            }
            

            if (printpagenumber > 0)
                goto page2;





            //logo only on page 1

            if (File.Exists(@"c:\invoice\logo.jpg") == false)
            {
                goto nologo;
            }


            Image i = Image.FromFile(@"C:\invoice\logo.jpg");
            Rectangle m = new Rectangle(300, 0, 200, 180);
            e.Graphics.DrawImage(i, m);


            nologo:;









            e.Graphics.DrawLine(pn, 0, 0, 820, 0); //hor
            e.Graphics.DrawLine(pn, 0, 180, 820, 180); //hor


            e.Graphics.DrawLine(pn, 0, 0, 0, 180); //1st vert
            e.Graphics.DrawLine(pn, 500, 0, 500, 180); //2nd vert
            e.Graphics.DrawLine(pn, 820, 0, 820, 180); //2nd vert






            //print vendor
            str1 = thisvendors[0].name;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 0, 820, 1000));
            str1 = thisvendors[0].address;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 20, 1000, 1000));
            str1 = thisvendors[0].town;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 40, 1000, 1000));
            str1 = thisvendors[0].county;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 60, 1000, 1000));
            str1 = thisvendors[0].postcode;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 80, 1000, 1000));
            str1 = thisvendors[0].country;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 100, 1000, 1000));
            str1 = thisvendors[0].telephonenumber;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 120, 1000, 1000));
            str1 = thisvendors[0].emailaddress;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 140, 1000, 1000));
            str1 = thisvendors[0].vatnumber;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(10, 160, 1000, 1000));



            //print customer info

            

                str1 = customers[currentcustomer].name;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 0, 1000, 1000));
                str1 = customers[currentcustomer].address;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 20, 1000, 1000));
                str1 = customers[currentcustomer].town;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 40, 1000, 1000));
                str1 = customers[currentcustomer].county;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 60, 1000, 1000));
                str1 = customers[currentcustomer].postcode;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 80, 1000, 1000));
                str1 = customers[currentcustomer].country;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 100, 1000, 1000));
                str1 = customers[currentcustomer].telephonenumber;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 120, 1000, 1000));
                str1 = customers[currentcustomer].emailaddress;
                e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500 + 10, 140, 1000, 1000));
            


            //print out invoice number and date
            cx = currentinvoice;
            str1 = "Invoice no.:";
            str1 = str1 + invoices[cx].invoicenumber;
            str1 = str1+"   Date:";
            str1 = str1 + invoices[cx].date;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(0, 180, 1000, 1000));






            
            str1 = "Description";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(0, 200, 1000, 1000));
            str1 = "Tax %";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(400, 200, 1000, 1000));
            str1 = "Quantity";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500, 200, 1000, 1000));
            str1 = "Price";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(600, 200, 1000, 1000));
            str1 = "Subtotal";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(700, 200, 1000, 1000));

            

            e.Graphics.DrawLine(pn, 0, 220, 820, 220); 



            cx = currentinvoice;

            
            int x, y;
            x = 10;
            y = 220;

            printdx = 0;
            printloop1:

            if (printdx > lastrow)
                goto printdone;

            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[0].Value);
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[1].Value);
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x+400, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[2].Value) ;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x+500, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[3].Value); 
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x+600, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[4].Value); ;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x+700, y, 1000, 1000));



            e.Graphics.DrawLine(pn, 0, y+20, 820, y+20); //horizontal


            e.Graphics.DrawLine(pn, 0, y, 0, y + 20);
            e.Graphics.DrawLine(pn, 400, y, 400, y + 20);
            e.Graphics.DrawLine(pn, 500, y, 500, y + 20);
            e.Graphics.DrawLine(pn, 600, y, 600, y + 20);
            e.Graphics.DrawLine(pn, 700, y, 700, y + 20);
            e.Graphics.DrawLine(pn, 800, y, 800, y + 20);

            e.Graphics.DrawLine(pn, 820, y, 820, y + 20); 



            y = y + 20;
            printdx++;

            if (printdx > lastrow)
                goto printdone;


            if (printdx < 45)
                goto printloop1;



            str1 = "Page " + Convert.ToString(printpagenumber+1); 
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(400, 1140, 1000, 1000));




            if (printdx < lastrow)
            {
                e.HasMorePages = true;
                printpagenumber++;
                return;
            }
            goto printdone;


            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
            page2:;

            str1 = "Description";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(0, 0, 1000, 1000));
            str1 = "Tax %";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(400, 0, 1000, 1000));
            str1 = "Quantity";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(500, 0, 1000, 1000));
            str1 = "Price";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(600, 0, 1000, 1000));
            str1 = "Subtotal";
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(700, 0, 1000, 1000));
            
            e.Graphics.DrawLine(pn, 0, 20, 820, 20);







           
            int rowcount = 0;
            cx = currentinvoice;
            x = 10;
            y = 20;
            

            printloop2:


            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[0].Value);
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[1].Value);
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x + 400, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[2].Value) ;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x + 500, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[3].Value);
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x + 600, y, 1000, 1000));
            str1 = Convert.ToString(dataGridView1.Rows[printdx].Cells[4].Value); 
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x + 700, y, 1000, 1000));

            e.Graphics.DrawLine(pn, 0, y, 820, y); //horizontal line seperators
            e.Graphics.DrawLine(pn, 0, y + 20, 820, y + 20); //horizontal line seperators


            e.Graphics.DrawLine(pn, 0, y, 0, y + 20);
            e.Graphics.DrawLine(pn, 400, y, 400, y + 20);
            e.Graphics.DrawLine(pn, 500, y, 500, y + 20);
            e.Graphics.DrawLine(pn, 600, y, 600, y + 20);
            e.Graphics.DrawLine(pn, 700, y, 700, y + 20);
            e.Graphics.DrawLine(pn, 800, y, 800, y + 20);

            e.Graphics.DrawLine(pn, 820, y, 820, y + 20); 

            y = y + 20;
            printdx++;
            rowcount++;

            str1 = "Page " + Convert.ToString(printpagenumber+1);
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(400, 1140, 1000, 1000));




            if (printdx > lastrow)
                goto printdone;

                if (rowcount < 45)
                goto printloop2;


 



            if (printdx < lastrow)
            {
                printpagenumber++;
                e.HasMorePages = true;
                return;
            }

            

            goto printdone;






            printdone:;





            //calc total VAT
            double vat2 = 0;
            //calc subtotals
            for (cx = 0; cx <= numberofitemsininvoice - 1; cx++)
            {


                if (Convert.ToString(dataGridView1.Rows[cx].Cells[1].Value) == "")
                    goto skip3;

                vat2 = vat2 + Convert.ToDouble(dataGridView1.Rows[cx].Cells[1].Value)/100 * Convert.ToDouble(dataGridView1.Rows[cx].Cells[2].Value);

                skip3:;
            }

           
           
            //total vat
            str1 = "Total Tax=" + textBox4.Text;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x + 400, y, 1000, 1000));






            str1 = "Total incl Tax=" + textBox3.Text;
            e.Graphics.DrawString(str1, printFont, new SolidBrush(Color.Black), new RectangleF(x + 600, y, 1000, 1000));




            e.HasMorePages = false;


        }


这篇关于使用打印机对话框以Windows窗体打印文本文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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