Search results for '.NET Print'. 1 post(s) found.

  1. 2007/08/28 Simplified .NET Printing In C#
2007/08/28 08:02

Simplified .NET Printing In C#


This is a .NET approach to Simplified Printing in which,we are going to use a RichTextBox to cache all of our text for Printing in PrintDocument.

// Form Load
private void Form1_Load(object sender,System.EventArgs e)
{
   // Write to richTextBox
   richTextBox1.Text = "                               "  
   DateTime.Now.Month   "/"   DateTime.Now.Day   "/"  
   DateTime.Now.Year   "\r\n\r\n";
   richTextBox1.AppendText("This is a greatly simplified Print "  
   "Document Method\r\n\r\n");
   richTextBox1.AppendText("We can write text to a richTextBox,"  
   "or use Append Text,"   "\r\n"   "or Concatenate a String,and "  
   "write that textBox. The "   "\r\n"   "richTextBox does not "  
   "even have to be visible. "   "\r\n\r\n"   "Because we use a "  
   "richTextBox it's physical dimensions are "   "\r\n" 
   "irrelevant. We can place it anywhere on our form,and set the "  
   "\r\n"   "Visible Property to false.\r\n\r\n");
   richTextBox1.AppendText("This is the document we will Print. The "  
   "rich TextBox serves "   "\r\n"   "as a Cache for our Report," 
   "or any other text we wish to Print.\r\n\r\n");
   richTextBox1.AppendText("I have also included Print Setup " 
   "and Print Preview. ");
}
// Print Event
private void miPrint_Click(object sender,System.EventArgs e)
{
    if (PrintDialog1.ShowDialog() == DialogResult.OK)
    {
         PrintDocument1.Print();
    }
}
       
// OnBeginPrint
private void OnBeginPrint(object sender,System.Drawing.Printing.PrintEventArgs e)
{
    char[] param = {'\n'};

    if (PrintDialog1.PrinterSettings.PrintRange == PrintRange.Selection)
    {
         lines = richTextBox1.SelectedText.Split(param);
    }
    else
    {
         lines = richTextBox1.Text.Split(param);
    }
           
    int i = 0;
    char[] trimParam = {'\r'};
    foreach (string s in lines)
    {
         lines[i  ] = s.TrimEnd(trimParam);
    }
}
// OnPrintPage
private void OnPrintPage(object sender,System.Drawing.Printing.PrintPageEventArgs e)
{
    int x = e.MarginBounds.Left;
    int y = e.MarginBounds.Top;
    Brush brush = new SolidBrush(richTextBox1.ForeColor);
           
    while (linesPrinted < lines.Length)
    {
    e.Graphics.DrawString (lines[linesPrinted  ],richTextBox1.Font,brush,x,y);
    y  = 15;
    if (y >= e.MarginBounds.Bottom)
    {
         e.HasMorePages = true;
         return;
    }
    else
    }
         e.HasMorePages = false;
    }
  }
}
// Page Setup
private void miSetup_Click(object sender,System.EventArgs e)
{
     // Call Dialog Box
     pageSetupDialog1.ShowDialog();
}
// Print Preview
private void miPreview_Click(object sender,System.EventArgs e)
{
     // Call Dialog Box
     PrintPreviewDialog1.ShowDialog();
   
}

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 01:25 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 09:44 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:44 delete

    moneyideas