Advertisement
Guest User

Untitled

a guest
Dec 12th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 10.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. using System.Drawing.Printing;
  9. using System.Collections;
  10.  
  11. namespace Okna
  12. {
  13.     class ClsPrint
  14.     {
  15.         #region Variables
  16.  
  17.         int iCellHeight = 0; //Used to get/set the datagridview cell height
  18.         int iTotalWidth = 0; //
  19.         int iRow = 0;//Used as counter
  20.         bool bFirstPage = false; //Used to check whether we are printing first page
  21.         bool bNewPage = false;// Used to check whether we are printing a new page
  22.         int iHeaderHeight = 0; //Used for the header height
  23.         StringFormat strFormat; //Used to format the grid rows.
  24.         ArrayList arrColumnLefts = new ArrayList();//Used to save left coordinates of columns
  25.         ArrayList arrColumnWidths = new ArrayList();//Used to save column widths
  26.         private PrintDocument _printDocument = new PrintDocument();
  27.         private DataGridView gw = new DataGridView();
  28.         private string _ReportHeader;
  29.         private string _ReportFooter;
  30.        
  31.  
  32.         #endregion
  33.  
  34.         public ClsPrint(DataGridView gridview, string ReportHeader, string ReportFooter)
  35.         {
  36.             _printDocument.PrintPage += new PrintPageEventHandler(_printDocument_PrintPage);
  37.             _printDocument.BeginPrint += new PrintEventHandler(_printDocument_BeginPrint);
  38.             gw = gridview;
  39.             _ReportHeader = ReportHeader;
  40.             _ReportFooter = ReportFooter;
  41.         }
  42.  
  43.         public void PrintForm()
  44.         {
  45.             ////Open the print dialog
  46.             //PrintDialog printDialog = new PrintDialog();
  47.             //printDialog.Document = _printDocument;
  48.             //printDialog.UseEXDialog = true;
  49.  
  50.             ////Get the document
  51.             //if (DialogResult.OK == printDialog.ShowDialog())
  52.             //{
  53.             //    _printDocument.DocumentName = "Test Page Print";
  54.             //    _printDocument.Print();
  55.             //}
  56.            
  57.             //Open the print preview dialog
  58.             PrintPreviewDialog objPPdialog = new PrintPreviewDialog();
  59.             objPPdialog.WindowState = FormWindowState.Maximized;
  60.             objPPdialog.PrintPreviewControl.Zoom = 1.0;
  61.             objPPdialog.Document = _printDocument;
  62.             objPPdialog.ShowDialog();
  63.         }
  64.  
  65.         private void _printDocument_PrintPage(object sender, PrintPageEventArgs e)
  66.         {
  67.             //try
  68.             //{
  69.             //Set the left margin
  70.             int iLeftMargin = e.MarginBounds.Left;
  71.             //Set the top margin
  72.             int iTopMargin = e.MarginBounds.Top;
  73.             //Whether more pages have to print or not
  74.             bool bMorePagesToPrint = false;
  75.             int iTmpWidth = 0;
  76.  
  77.             //For the first page to print set the cell width and header height
  78.             if (bFirstPage)
  79.             {
  80.                 foreach (DataGridViewColumn GridCol in gw.Columns)
  81.                 {
  82.                     iTmpWidth = (int)(Math.Floor(GridCol.Width /
  83.                         (double)iTotalWidth * iTotalWidth *
  84.                         (e.MarginBounds.Width / (double)iTotalWidth)));
  85.  
  86.                     iHeaderHeight = (int)(e.Graphics.MeasureString(GridCol.HeaderText,
  87.                         GridCol.InheritedStyle.Font, iTmpWidth).Height) + 11;
  88.  
  89.                     // Save width and height of headers
  90.                     arrColumnLefts.Add(iLeftMargin);
  91.                     arrColumnWidths.Add(iTmpWidth);
  92.                     iLeftMargin += iTmpWidth;
  93.                 }
  94.             }
  95.             //Loop till all the grid rows not get printed
  96.             while (iRow <= gw.Rows.Count - 1)
  97.             {
  98.                 DataGridViewRow GridRow = gw.Rows[iRow];
  99.                 //Set the cell height
  100.                 iCellHeight = GridRow.Height + 5;
  101.                 int iCount = 0;
  102.                 //Check whether the current page settings allows more rows to print
  103.                 if (iTopMargin + iCellHeight >= e.MarginBounds.Height + e.MarginBounds.Top)
  104.                 {
  105.                     bNewPage = true;
  106.                     bFirstPage = false;
  107.                     bMorePagesToPrint = true;
  108.                     break;
  109.                 }
  110.                 else
  111.                 {
  112.  
  113.                     if (bNewPage)
  114.                     {
  115.                        
  116.                         //Draw Header
  117.                         e.Graphics.DrawString(_ReportHeader,
  118.                             new Font("Verdana",12, FontStyle.Bold),
  119.                             Brushes.Black, 250,
  120.                             e.MarginBounds.Top - e.Graphics.MeasureString(_ReportHeader,
  121.                             new Font(gw.Font, FontStyle.Bold),
  122.                             e.MarginBounds.Width).Height - 13);
  123.  
  124.                         //Draw Footer
  125.                         e.Graphics.DrawString(_ReportFooter,
  126.                             new Font("Verdana", 8, FontStyle.Regular),
  127.                             Brushes.Black, 550,
  128.                             860 - e.Graphics.MeasureString(_ReportFooter,
  129.                             new Font(gw.Font, FontStyle.Regular),
  130.                             e.MarginBounds.Width).Height - 13);
  131.  
  132.                         String strDate = "";
  133.                         //Draw Date
  134.                         e.Graphics.DrawString(strDate,
  135.                             new Font(gw.Font, FontStyle.Bold), Brushes.Black,
  136.                             e.MarginBounds.Left +
  137.                             (e.MarginBounds.Width - e.Graphics.MeasureString(strDate,
  138.                             new Font(gw.Font, FontStyle.Bold),
  139.                             e.MarginBounds.Width).Width),
  140.                             e.MarginBounds.Top - e.Graphics.MeasureString(_ReportHeader,
  141.                             new Font(new Font(gw.Font, FontStyle.Bold),
  142.                             FontStyle.Bold), e.MarginBounds.Width).Height - 13);
  143.  
  144.                         //Draw Columns  
  145.                        
  146.                         iTopMargin = e.MarginBounds.Top;
  147.                         DataGridViewColumn[] _GridCol = new DataGridViewColumn[gw.Columns.Count];
  148.                         int colcount = 0;
  149.                        
  150.                         //Convert ltr to rtl
  151.  
  152.                         foreach (DataGridViewColumn GridCol in gw.Columns)
  153.                         {
  154.                             _GridCol[colcount++] = GridCol;
  155.                         }
  156.                         for (int i = 0; i <= (_GridCol.Count() - 1); i++)
  157.                         {
  158.                             e.Graphics.FillRectangle(new SolidBrush(Color.LightGray),
  159.                                 new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  160.                                 (int)arrColumnWidths[iCount], iHeaderHeight));
  161.  
  162.                             e.Graphics.DrawRectangle(Pens.Black,
  163.                                 new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  164.                                 (int)arrColumnWidths[iCount], iHeaderHeight));
  165.  
  166.                             e.Graphics.DrawString(_GridCol[i].HeaderText,
  167.                                 _GridCol[i].InheritedStyle.Font,
  168.                                 new SolidBrush(_GridCol[i].InheritedStyle.ForeColor),
  169.                                 new RectangleF((int)arrColumnLefts[iCount], iTopMargin,
  170.                                 (int)arrColumnWidths[iCount], iHeaderHeight), strFormat);
  171.                             iCount++;
  172.                         }
  173.                         bNewPage = false;
  174.                         iTopMargin += iHeaderHeight;
  175.                     }
  176.                     iCount = 0;
  177.                     DataGridViewCell[] _GridCell = new DataGridViewCell[GridRow.Cells.Count];
  178.                     int cellcount = 0;
  179.                    
  180.                     //Convert ltr to rtl
  181.                     foreach (DataGridViewCell Cel in GridRow.Cells)
  182.                     {
  183.                         _GridCell[cellcount++] = Cel;
  184.                     }
  185.                     //Draw Columns Contents                
  186.                     for (int i = 0; i <= (_GridCell.Count() - 1); i++)
  187.                     {
  188.                         if (_GridCell[i].Value != null)
  189.                         {
  190.                             e.Graphics.DrawString(_GridCell[i].FormattedValue.ToString(),
  191.                                 _GridCell[i].InheritedStyle.Font,
  192.                                 new SolidBrush(_GridCell[i].InheritedStyle.ForeColor),
  193.                                 new RectangleF((int)arrColumnLefts[iCount],
  194.                                 iTopMargin,
  195.                                 (int)arrColumnWidths[iCount], iCellHeight),
  196.                                 strFormat);
  197.                         }
  198.                         //Drawing Cells Borders
  199.                         e.Graphics.DrawRectangle(Pens.Black,
  200.                             new Rectangle((int)arrColumnLefts[iCount], iTopMargin,
  201.                             (int)arrColumnWidths[iCount], iCellHeight));
  202.                         iCount++;
  203.                     }
  204.                 }
  205.                 iRow++;
  206.                 iTopMargin += iCellHeight;
  207.             }
  208.             //If more lines exist, print another page.
  209.             if (bMorePagesToPrint)
  210.                 e.HasMorePages = true;
  211.             else
  212.                 e.HasMorePages = false;
  213.             //}
  214.             //catch (Exception exc)
  215.             //{
  216.             //    MessageBox.Show(exc.Message, "Error", MessageBoxButtons.OK,
  217.             //       MessageBoxIcon.Error);
  218.             //}
  219.         }
  220.  
  221.         private void _printDocument_BeginPrint(object sender, PrintEventArgs e)
  222.         {
  223.             try
  224.             {
  225.                 strFormat = new StringFormat();
  226.                 strFormat.Alignment = StringAlignment.Center;
  227.                 strFormat.LineAlignment = StringAlignment.Center;
  228.                 strFormat.Trimming = StringTrimming.EllipsisCharacter;
  229.  
  230.                 arrColumnLefts.Clear();
  231.                 arrColumnWidths.Clear();
  232.                 iCellHeight = 0;
  233.                 iRow = 0;
  234.                 bFirstPage = true;
  235.                 bNewPage = true;
  236.  
  237.                 // Calculating Total Widths
  238.                 iTotalWidth = 0;
  239.                 foreach (DataGridViewColumn dgvGridCol in gw.Columns)
  240.                 {
  241.                     iTotalWidth += dgvGridCol.Width;
  242.                 }
  243.             }
  244.             catch (Exception ex)
  245.             {
  246.                 MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  247.             }
  248.         }
  249.  
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement