WinForm中的列印製作(無textBox)

來源:互聯網
上載者:User

最近做了一個winForm的列印功能。由於初次製作,對這個也不太瞭解。所以在網上找了很多資料。最後整理出處如下代碼

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace WindowsApplication1
{
    public partial class daochu : Form
    {
        public daochu()
        {
            InitializeComponent();
        }
        PrintDocument printDocument;
        private void daochu_Load(object sender, EventArgs e)
        {
            PrintDocuments();

        }

        private void PrintDocuments()//封裝事件
        {
            printDocument = new PrintDocument();
            printDocument.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);//列印頁面需指定相應的PrintDocument_PrintPrintPage事件委託 

        }

        int iWidth=800;
        int  iHeight = 600;
        int left=150;
        int right=150;
        int top=100;
        int bottom = 100;
        Graphics g;
        Brush b;
        Font titleFont;
        Font fntTxt;
        System.Drawing.Printing.PrintPageEventArgs x;
        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            PrintStyle(e);
        }

        private void PrintStyle(System.Drawing.Printing.PrintPageEventArgs e)//列印樣式的方法封裝
        {
            this.printDocument1.DefaultPageSettings.PaperSize = new
            System.Drawing.Printing.PaperSize("NewPrint", iWidth, iHeight); //設定列印紙的大小
            titleFont = new Font("宋體", 18, FontStyle.Bold);//標題字型           
            fntTxt = new Font("宋體", 16, FontStyle.Regular);//本文文字
            Margins margins = new Margins(left, right, top, bottom);//設定上下左右邊距
            printDocument.DefaultPageSettings.Margins = margins;
            x = e;
            float leftMargin = e.MarginBounds.Left;//左邊距
            float topMargin = e.MarginBounds.Top;//上邊距
            Brush brush = new SolidBrush(Color.Black);//畫刷         
            //Pen pen = new Pen(Color.Black);           //線條顏色
            g = e.Graphics;
            b = new SolidBrush(Color.Black);
            Point po = new Point(10, 10);
            try
            {
                e.Graphics.DrawString(GetPrintSW().ToString(), fntTxt, brush, po);   //DrawString方式進行列印。
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "列印出錯!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        ///GetPrintSw方法用來構造列印文本,內部StringBuilder.AppendLine在Drawstring時單獨佔有一行。

     
         // public StringBuilder GetPrintSW(string name, string IDcard, int Num) //外部傳入值時用

        public StringBuilder GetPrintSW() //直接在內部定義值
        {
            StringBuilder sb = new StringBuilder();
            string AdrName = txtNames.Text.Trim();
            if (AdrName == "")
            {
                AdrName = "中華人民共和國";
            }
            string title = "大中華" + AdrName + "動遷配套商品房";

            string titles = "順序號抽籤資格確認單";

            string IDcard = "622825198910223315";    //身份證      

            string name = "";
       
            string names="";
            if (name.Length == 2)
            {
                names= "\0\0"+name+"\0\0";
            }
            else if (name.Length == 3)
            {
                names ="\0"+ name+"\0" ;
            }
            else if (name.Length == 4)
            {
                names = name ;
            }
            else
            {
                names = "\0\0\0\0\0\0\0";
            }
            string  Num="25";              //號碼

            string item1 = "\0\0\0\0經本單位審核確認,抽籤人\0" + names + "\0有資格按《中華人民共";

            string item2 = "和國國務院動遷配套商品房抽籤公告》的規定參加順序號抽籤活動。";

            string item3 = "請公證處按抽籤公告的規定,為其辦理順序號電腦抽籤公證。";

            string item4 ="備忘:\0證件號碼為:\0" + IDcard + "\0抽籤順序號為:\0" + Num;

            string item5 = " \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + AdrName + "民委員會";
            //string item6 = "(抽籤人暫存並交相關單位簽約聯)";

            /*橫向列印文本控制,後面的x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2控制左右,20控制上下
            g.DrawString("\r\n\r\r\n\r\n\r\n" + title , titleFont, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 20));//標題

            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\r\n" + item1 + " \r\n\r\n\r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 4, 50));
             *
            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n" + item2 + " \r\n\r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 4, 120));

            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n" + item3 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 4, 190));
             *
            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n" + item4 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 220));
             *
            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\r\n \0\0\0\0\0\0\0\0 \0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0日期:" + DateTime.Now.ToShortDateString(), fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 250));
            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\r\n\r\n" + item5 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 3, 300));
             */

            for (int j = 1; j <= 2; j++)
            {
                int m = 0;
                if (j == 1)
                {
                    m = 0;
                }
                else
                {
                    m = 580;
                }        
                g.DrawString("\r\n\r\r\n\r\n\r\r" + title, titleFont, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 10+m));//標題1
                g.DrawString("\r\n\r\r\n\r\n\r\r\r\0\0\0\0\0\0\0\0\0\0\0\0" + titles, titleFont, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 3, 45 + m));//標題2
                g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\r\0" + item1 + " \r\n\r\n\r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 6, 50 + m));
                g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\0" + item2 + " \r\n\r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 6, 120 + m));
                g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\r\0" + item3 + " \r\n\r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 6, 170 + m));
                g.DrawString("\r\n\r\r\n\r\n\r\n\r\0" + item4 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 6, 240 + m));
                g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\0\0" + item5 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 4, 290 + m));
                g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\r\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0日期:" + DateTime.Now.ToShortDateString(), fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 300 + m));
               // g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\r\n\r\0" + item6 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 6, 310 + m));

            }

            /*以下方式屬於第二種列印方式
            sb.AppendLine("\r\n\r");//“\n”為分行符號;“\r”為斷行符號符
            sb.AppendLine("\r\n\r");
            sb.AppendLine("\r\n\r");
            sb.AppendLine("\r\n");
            //標題,主要是為了控制字型的樣式方便
            g.DrawString("\r\n\r\r\n\r\n\r\n" + title, titleFont, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 2, 15));
            //sb.AppendLine("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + title + " \r\n\r");
            sb.AppendLine("\0\0\0\0\0" + item1 + "\r\n\r");
            sb.AppendLine("\0\0\0\0\0" + item2 + "\r\n\r");
            sb.AppendLine("\0\0\0\0\0" + item3 + "\r\n");
            sb.AppendLine("\r\n\r");
           // sb.AppendLine("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0" + item4 + "\r\n\r");
            g.DrawString("\r\n\r\r\n\r\n\r\n\r\n\r\n\0\0\0\0\0\0" + item4 + " \r\n\r", fntTxt, b, new PointF((x.PageBounds.Width - g.MeasureString(title, titleFont).Width) / 4, 240));
            sb.AppendLine("\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0日期:" + DateTime.Now.ToShortDateString());
            sb.AppendLine("\r");
            sb.AppendLine("\0\0\0\0\0\0\0\0\0\0" + item5);
           // sb.AppendLine("-----------------------------------------");      
   */
            return sb;
        }
       
        private void btntxt_Click(object sender, EventArgs e)//列印
        {
            Printing();            
        }

        private void Printing()//列印的方法
        {
            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printDocument;
            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    printDocument.Print();
                }
                catch (Exception excep)
                {
                    MessageBox.Show(excep.Message, "列印出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    printDocument.PrintController.OnEndPrint(printDocument, new PrintEventArgs());
                }
            }
        }

        private void show_Click(object sender, EventArgs e)//預覽列印
        {
            PrintPreview();
        }

        private void PrintPreview()//預覽列印的方法
        {
            PageSetupDialog psd = new PageSetupDialog();
            psd.Document = printDocument;
            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();
            printPreviewDialog.Document = printDocument;

            /*橫向預覽列印設定  ps:預設為縱向          
            printDocument.PrinterSettings.PrintToFile = false;
            printDocument.DefaultPageSettings.Landscape = true;
            printDocument.PrinterSettings.DefaultPageSettings.Landscape = true;
            printDocument.DefaultPageSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
            PrintDialog pDialog = new PrintDialog();
            pDialog.Document = printDocument;
            pDialog.PrinterSettings.DefaultPageSettings.Landscape = true;
            pDialog.PrinterSettings.PrintToFile = false;
            pDialog.Document.DefaultPageSettings.Landscape = true;
           */

            try
            {
                printPreviewDialog.ShowDialog();
            }
            catch (Exception excep)
            {
                MessageBox.Show(excep.Message, "預覽出錯", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}

因為時間原因,很多代碼也不知道具體的意思。但是總上是實現了這個列印功能。今天在此貼出代碼,希望路過的高手指點代碼的不足。在此小弟先謝謝了~~~

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.