The first assignment I got from my lead in my previous company was to create Print option to the accounting package we were working. The application was developed in VB 6.0. And we were used batch files and command prompt for printing accounting related documents from the application. Few days back one of colleague asked how we can print from C#, he was developing an accounting package for his uncle. I said something like we need to use PrintDocument class for that, but I couldn’t give him a sample code. Today I got some free time in Office so I thought about implementing printing from C#. Here is a simple implementation, which displays a Print Dialog, and based on the settings, it will print the contents of the textbox.
I tried to print the document based on the ForeColor of the TextBox, but it was throwing some error, I couldn’t resolve itIts fixed.
Source code is pretty self-explanatory. Happy Printing.
private int startIndex = 0;
private void printToolStripMenuItem_Click(object sender, EventArgs e)
{
PrintDocument printDocument = new PrintDocument();
//Print Options dialog.
using (PrintDialog printDialog = new PrintDialog())
{
printDialog.ShowHelp = false;
printDialog.ShowNetwork = false;
printDialog.Document = printDocument;
printDialog.AllowPrintToFile = true;
//As its a simple text file, disabling the advanced options.
printDialog.AllowCurrentPage = false;
printDialog.AllowSelection = false;
printDialog.AllowSomePages = false;
//Display XP Style Print Dialog
printDialog.UseEXDialog = true;
if (printDialog.ShowDialog(this) == DialogResult.OK)
{
//Assigning the Printer settings to the Document.
printDocument.PrinterSettings = printDialog.PrinterSettings;
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
//Number of copies
for (int i = 0; i < printDialog.PrinterSettings.Copies; i++)
{
startIndex = 0;
//Print method call.
printDocument.Print();
}
}
}
}
And here is the Print Page event.
protected void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
//Setting the color as Black.
Brush brush = Brushes.Black;
//Setting color dynamically
//Brush brush = new SolidBrush(this.txtEditor.ForeColor);
int lineCounter = 0;
float lineTop = 0;
//Looping throught each line in the textbox.
for (int lineIndex = startIndex; lineIndex < this.txtEditor.Lines.Length; lineIndex++)
{
string line = this.txtEditor.Lines[lineIndex];
lineCounter++;
//Writing the line to the document.
lineTop = e.MarginBounds.Top + lineCounter * this.txtEditor.Font.Size;
e.Graphics.DrawString(line,
this.txtEditor.Font,
brush,
e.MarginBounds.Left,
lineTop);
//Checking for more pages.
if (lineTop > e.MarginBounds.Bottom)
{
startIndex = lineIndex;
e.HasMorePages = true;
return;
}
}
}
printDocument.PrinterSettings.Copies = 3;
but has no effect on the number of copies.
I can not do the following
for (int j = 0; j <copiesCount; j++)
{
PrintDocument.Print ();
}
that if I send many copies simultaneously to the printer, sometimes the copies arestaggered.
Is there any solution?
Thank you very much.