Search results for '.NET Print'. 1 post(s) found.
- 2007/08/28 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();
}
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();
}
Another posts included in "C#"
| A Customizable Printing Text Class (0) | 2007/08/28 |
| Print HTML In C# With Or Without The Web Browser Control And The Print... (0) | 2007/08/28 |
| Adding Custom Paper Sizes To Named Printers (0) | 2007/08/28 |
| How To Check If Your Printer Is Connected Using C# (0) | 2007/08/28 |
| How To Print Text In C# (0) | 2007/08/28 |
| C Sharp Lists (0) | 2007/08/28 |
| Simple Example Of IF/THEN Using C# (0) | 2007/08/28 |
| Simple C# CGI working on Apache (0) | 2007/08/27 |
Trackback : Cannot send a trackbact to this post.
-
Subject different money making ideas
2010/01/29 01:25
moneyideas
-
Subject different money making ideas
2010/01/29 09:44
moneyideas
-
Subject different money making ideas
2010/01/31 16:44
moneyideas

Prev

Rss Feed