C#2.0 print winform 打印窗体数据试试

  1using System;
  2using System.Collections.Generic;
  3using System.ComponentModel;
  4using System.Data;
  5using System.Drawing;
  6using System.Drawing.Drawing2D;
  7using System.Text;
  8using System.Windows.Forms;
  9using System.Globalization; 
 10
 11namespace PrintTest
 12{
 13    /// <summary>
 14    /// 打印报告测试 
 15    /// 2009-02-06 涂聚文 geovindu@163.com
 16    /// </summary>

 17    public partial class PintUlrtDocument : Form
 18    {
 19        /// <summary>
 20        /// 打印B超报告测试
 21        /// </summary>

 22        public PintUlrtDocument()
 23        {
 24            InitializeComponent();
 25        }

 26        /// <summary>
 27        /// 打印
 28        /// </summary>
 29        /// <param name="sender"></param>
 30        /// <param name="e"></param>

 31        private void btnPrint_Click(object sender, EventArgs e)
 32        {
 33
 34        }

 35        /// <summary>
 36        /// 打印浏览
 37        /// </summary>
 38        /// <param name="sender"></param>
 39        /// <param name="e"></param>

 40        private void btnPrintView_Click(object sender, EventArgs e)
 41        {
 42            if (MessageBox.Show("是否打印预览?""打印预览", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
 43            {
 44                this.printPreviewDialog1.Document = this.printDocument1;
 45                printPreviewDialog1.ShowDialog();
 46            }

 47            else
 48            {
 49                this.printDocument1.Print();//直接打印
 50            }

 51        }

 52        /// <summary>
 53        /// 打印设置
 54        /// </summary>
 55        /// <param name="sender"></param>
 56        /// <param name="e"></param>

 57        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
 58        {
 59            //字体设置
 60            //this.dateTimePicker1.Format=dateTimePicker1.CustomFormat;
 61            dateTimePicker1.CustomFormat = "yyyy-MM-dd";
 62            //System.DateTime currentTime = new System.DateTime(); 
 63
 64            DateTime date = DateTime.Now;
 65            String format = "u";
 66            string year=DateTime.Now.Year.ToString();
 67            string month=date.ToString(format).Substring(5,2);
 68            string day = date.ToString(format).Substring(82);
 69
 70            Font tFont = new Font("宋体"20, FontStyle.Bold);
 71            Font mFont = new Font("宋体"15, FontStyle.Regular);
 72            Font bFont = new Font("宋体"10, FontStyle.Regular);
 73            Font lFont = new Font("宋体"10, FontStyle.Regular);
 74            e.Graphics.DrawString("缔友计算机信息技术有限公司", tFont, Brushes.Black, 18010);
 75            e.Graphics.DrawString("电脑超声诊断报告", mFont, Brushes.Black, 25040);
 76            e.Graphics.DrawString("超声号:", bFont, Brushes.Black, 2070);
 77            e.Graphics.DrawString(this.txtUrltID.Text.Trim(), bFont, Brushes.Black, 7070);
 78            e.Graphics.DrawString("检查医生:", bFont, Brushes.Black, 29070);
 79            e.Graphics.DrawString(this.combPhyName.Text.Trim(), bFont, Brushes.Black, 36070);
 80            e.Graphics.DrawString("检查部位:", bFont, Brushes.Black, 20,90);
 81            e.Graphics.DrawString(this.combCheckBody.Text.Trim(), bFont, Brushes.Black, 8090);
 82            //划直线
 83            Pen Lapiz = new Pen(Color.Black, 1);
 84            int PosY = e.MarginBounds.Top;
 85            int PosX = e.MarginBounds.Height;
 86            e.Graphics.DrawLine(Lapiz, 20, PosY+10810, PosY+10);
 87            
 88            e.Graphics.DrawString("图像特征:", bFont, Brushes.Black, 20120);
 89            e.Graphics.DrawString(this.txtPicImpress.Text.Trim(), bFont, Brushes.Black, 40140);
 90            e.Graphics.DrawString("身份证图像", bFont, Brushes.Black, 420120);
 91            e.Graphics.DrawImage(this.pictureBox2.Image, 450140350350);
 92            //
 93            e.Graphics.DrawString("超声提示:", bFont, Brushes.Black, 20300);
 94            e.Graphics.DrawString(this.combMessage.Text, bFont, Brushes.Black, 40320);
 95            string emty = "      ";
 96            e.Graphics.DrawString("医师签字:", bFont, Brushes.Black, 600500);
 97            e.Graphics.DrawString("___________________", bFont, Brushes.Black,680500);
 98            e.Graphics.DrawString(this.combPhyName.Text+emty, lFont, Brushes.Black, 680500);
 99            e.Graphics.DrawString("报告日期:", bFont, Brushes.Black, 600520);
100            e.Graphics.DrawString(year, bFont, Brushes.Black, 680520);
101            e.Graphics.DrawString("", bFont, Brushes.Black, 720520);
102            e.Graphics.DrawString(month, bFont, Brushes.Black, 740520);
103            e.Graphics.DrawString("", bFont, Brushes.Black, 760520);
104            e.Graphics.DrawString(day, bFont, Brushes.Black, 790520);
105            e.Graphics.DrawString("", bFont, Brushes.Black, 810520);
106            
107            
108            //Graphics G;
109            //G = this.CreateGraphics();
110            //G.DrawLine(new Pen(Color.Red, 10), 50, 10, 50, 100);
111
112            //Graphics grfx = e.Graphics;
113            //grfx.DrawString(Text, Font, Brushes.Black, 0, 0);
114            //SizeF sizef = grfx.MeasureString(Text, Font);
115            //grfx.DrawLine(Pens.Black, sizef.ToPointF(), grfx.VisibleClipBounds.Size.ToPointF());
116            //grfx.DrawLine(Pens.Red, sizef.ToPointF(),grfx.VisibleClipBounds.Size.ToPointF());
117
118            画斜线
135
136            画多边形 
150            页底部 
168}

169        /// <summary>
170        /// 创建图片
171        /// </summary>
172        /// <param name="e"></param>

173        public void DrawImageRectF(PaintEventArgs e)
174        {
175
176            // Create image.
177            Image newImage = Image.FromFile(Application.StartupPath + "\\Customers Pics\\CustPic.bmp");
178
179            // Create rectangle for displaying image.
180            RectangleF rect = new RectangleF(100.0F100.0F450.0F150.0F);
181
182            // Draw image to screen.
183            e.Graphics.DrawImage(newImage, rect);
184        }

185        /// <summary>
186        /// 加载窗体
187        /// </summary>
188        /// <param name="sender"></param>
189        /// <param name="e"></param>

190        private void PintUlrtDocument_Load(object sender, EventArgs e)
191        {
192            pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
193            pictureBox2.Image = Image.FromFile(Application.StartupPath + "\\Customers Pics\\CustPic.bmp");
194        }

195        /// <summary>
196        /// 画直线
197        /// </summary>
198        /// <param name="e"></param>

199        public void DrawLinePoint(PaintEventArgs e)
200        {
201            //   Create   pen.  
202            Pen blackPen = new Pen(Color.Black, 3);
203            //   Create   points   that   define   line.  
204            Point point1 = new Point(100100);
205            Point point2 = new Point(500100);
206            //   Draw   line   to   screen.  
207            e.Graphics.DrawLine(blackPen, point1, point2);
208        }
   
209        /// <summary>
210        /// 画直线
211        /// </summary>
212        /// <param name="e"></param>

213        protected override void OnPaint(PaintEventArgs e)
214        {
215            Pen myPen = new Pen(Color.Red); 
216            Graphics g = e.Graphics;
217            myPen.Width = 5;
218            g.DrawLine(myPen, 11100100);
219
220        }

221
222        身份证 
309        /// <summary>
310        /// 
311        /// </summary>
312        /// <param name="args"></param>

313        //public   static   void   date(string[]   args)    
314        //{
315        //        DateTime   dt   =   DateTime.Now;
316        //        String[]   format   =   {
317        //                "d ",   "D ",
318        //                "f ",   "F ",
319        //                "g ",   "G ",
320        //                "m ",
321        //                "r ",
322        //                "s ",
323        //                "t ",   "T ",
324        //                "u ",   "U ",
325        //                "y ",
326        //                "dddd,   MMMM   dd   yyyy ",
327        //                //"ddd,   MMM   d   \ " '\ "yy ",
328        //                "dddd,   MMMM   dd ",
329        //                "M/yy ",
330        //                "dd-MM-yy ",
331        //        };
332        //        String   date;
333        //        for   (int   i   =   0;   i   <   format.Length;   i++)   {
334        //                date   =   dt.ToString(format[i],   DateTimeFormatInfo.InvariantInfo);
335        //                Console.WriteLine(String.Concat(format[i],   "   : "   ,   date));
336        //        } 
337        //}
338
339    }

340}
原文地址:https://www.cnblogs.com/geovindu/p/1385984.html