c#打印源代码

aini1563 贡献于2012-11-20

作者 MC SYSTEM  创建于2010-10-21 08:16:00   修改者MC SYSTEM  修改于2010-10-21 08:16:00字数2549

文档摘要:c#打印源代码
关键词:

c#打印源代码 2007年07月11日 星期三 11:33 using System; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Drawing.Printing; using System.Data; using System.Windows.Forms; namespace WindowsApplication1 {      /**////      /// Summary description for DatagridPrint.      ///      public class DatagridPrint      {          private DataGrid dataGrid;          private PrintDocument printDocument;          private PageSetupDialog pageSetupDialog;          private PrintPreviewDialog printPreviewDialog;          public DatagridPrint(DataGrid dataGrid)          {              this.dataGrid = dataGrid;              printDocument = new PrintDocument();              printDocument.PrintPage += new PrintPageEventHandler(this.printDocument_PrintPage);          }          private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)          {              int rowCount = 0;              int colCount = 0;              int x = 0;              int y = 0;              int rowGap = 20;              int colGap = 5;              int leftMargin = 50;              Font font = new Font("Arial", 10);              Font headingFont = new Font("Arial", 11, FontStyle.Underline);              Font captionFont = new Font("Arial", 10, FontStyle.Bold);              Brush brush = new SolidBrush(Color.Black);              string cellValue = "";              if(dataGrid.DataSource.GetType().ToString() == "System.Data.DataTable")              {                  rowCount = ((DataTable)dataGrid.DataSource).Rows.Count;              }              else if(dataGrid.DataSource.GetType().ToString() == "System.Collections.ArrayList")              {                  rowCount = ((ArrayList)dataGrid.DataSource).Count;              }              colCount = dataGrid.TableStyles[0].GridColumnStyles.Count;              //print caption              if(dataGrid.CaptionVisible)              {                  y += rowGap;                  x = leftMargin;                  e.Graphics.DrawString(dataGrid.CaptionText, captionFont, brush, x, y);              }              //print headings              y += rowGap;              x = leftMargin;              for(int j = 0; j < colCount; j++)              {                  if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)                  {                      cellValue = dataGrid.TableStyles[0].GridColumnStyles[j].HeaderText;                      e.Graphics.DrawString(cellValue, headingFont, brush, x, y);                      x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;                  }              }              //print all rows              for(int i = 0; i < rowCount; i++)              {                  y += rowGap;                  x = leftMargin;                  for(int j = 0; j < colCount; j++)                  {                      if(dataGrid.TableStyles[0].GridColumnStyles[j].Width > 0)                      {                          cellValue = dataGrid[i,j].ToString();                          e.Graphics.DrawString(cellValue, font, brush, x, y);                          x += dataGrid.TableStyles[0].GridColumnStyles[j].Width + colGap;                          y = y + rowGap * (cellValue.Split(new char[] {' ', ' '}).Length - 1);                      }                  }              }              string s = cellValue;              string f3 = cellValue;          }          public PrintDocument GetPrintDocument()          {              return printDocument;          }          public void Print()          {              try              {                  pageSetupDialog = new PageSetupDialog();                  pageSetupDialog.Document = printDocument;                  pageSetupDialog.ShowDialog();                  printPreviewDialog = new PrintPreviewDialog();                  printPreviewDialog.Document = printDocument;                  printPreviewDialog.Height = 600;                  printPreviewDialog.Width = 800;                  printPreviewDialog.ShowDialog();              }              catch(Exception e)              {                  throw new Exception("Printer error." + e.Message);              }          }      } }

下载文档到电脑,查找使用更方便

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 2 金币 [ 分享文档获得金币 ] 0 人已下载

下载文档