In the windows application to print the document is a very important function in the past has been a very complex task, Microsoft .net Framework print functions are provided by way of components, it provides great convenience for programmers, but the use of these components is very complex, it is necessary to explain.
Printing operation generally comprises the following four functions
1 print some parameters such as printer settings to change the printer drivers, etc.
2 Page Setup page size paper types,
3 Print Preview similar word in print preview
4 Print
Now I write my diary (full source code can be http://www.cndot.net download) the code used in the print function to interpret hope to give you some help
The core function is to achieve printing PrintDocument class belongs to the class name space System.Drawing.Printing this class encapsulates the current print settings and the setting page
Some printing-related events and methods
This class includes the following properties and methods of events
1, PrinterSettings property
The printer setup information is stored programmers do not need to set this property because it was acquired by the print dialog
2, PrintCountroller property
Control the printing process
3, DefaultPageSettings property
Page Setup information is stored paper size orientation and other programmers do not need to set up because it was acquired by the page settings dialog
4, DocumentName property
Specify the document name appears in the Printer Status Window
1. BeginPrint event
Issued before printing
2. PrintPage event
Each print is an issue, event accepts a parameter of the parameter PrintPageEventArgs encapsulates the print-related information
There are many important parameters PrintPageEventArgs property
1 Cancel to cancel printing
Drawing Object 2 Graphics page
3 HasMorePages page if there are to be printed
Print method which does not have a parameter called it sets start printing according to the current
If the print function to achieve the object is first constructed PrintDocument print add events
PrintDocument printDocument;
private void InitializeComponent()
{
...
printDocument=new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler
(this.printDocument_PrintPage);
...
}
Print event function to achieve
And the like are printed drawing methods in the Graphics class calls drawing
except that a a printing paper to be printed on a display and some complex calculations
The wrap pagination.
private void printDocument_PrintPage(object
sender,PrintPageEventArgs e)
{
StringReader lineReader = new
StringReader(textBox.Text);
Graphics g = e.Graphics; // get drawing objects
float linesPerPage = 0; // row number of the page
float yPosition = 0; // draw the longitudinal position of the string
int count = 0; // line counter
float leftMargin = e.MarginBounds.Left; // left margin
float topMargin = e.MarginBounds.Top; // top margin
string line = null; line string
Font printFont = this.textBox.Font; // current print font
SolidBrush myBrush = new
SolidBrush(Color.Black);//刷子
= e.MarginBounds.Height linesPerPage /
printFont.GetHeight (G); // number of lines per printable
// print a progressive cycle
while(count < linesPerPage &&
((line=lineReader.ReadLine()) != null))
{
yPosition TOPMARGIN = + (* count
printFont.GetHeight (g));
g.DrawString(line, printFont, myBrush,
leftMargin, yPosition, new StringFormat());
count++;
}
If you print this page and complete the line is not empty there is no complete description of the page which will trigger the next printing of the next print event will lineReader
Automatically read the contents of the last not finished printing because lineReader are members of a class outside of this printing method which can record the current reading position
if(line != null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}
Print settings, the print dialog is configured
to set Document Properties dialog box so the user will be assigned to printDocument PrinterSettings settings are automatically saved to the property printDocument
protected void FileMenuItem_PrintSet_Click(object
sender,EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
printDialog.ShowDialog();
}
Page layout and print preview print setting the same principle is configured to save the user dialog in the dialog box to set the corresponding property class
protected void FileMenuItem_PageSet_Click(object
sender,EventArgs e)
{
PageSetupDialog pageSetupDialog = new
PageSetupDialog();
pageSetupDialog.Document = printDocument;
pageSetupDialog.ShowDialog();
}
Printing preview
protected void FileMenuItem_PrintView_Click(object
sender,EventArgs e)
{
Print preview dialog print preview dialog = new
print preview dialog ();
printPreviewDialog.Document = printDocument;
try
{
printPreviewDialog.ShowDialog();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message, "打印出错",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
Printing can directly call the Print printDocument () method because users may still have to change the print settings before printing it
In the Print Setup dialog box shown here again
protected void FileMenuItem_Print_Click(object
sender,EventArgs e)
{
PrintDialog printDialog = new PrintDialog();
printDialog.Document = printDocument;
lineReader = new StringReader(textBox.Text);
if (printDialog.ShowDialog() ==
DialogResult.OK)
{
try
{
printDocument.Print();
}
catch(Exception excep)
{
MessageBox.Show(excep.Message,
"打印出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
printDocument.PrintController.OnEndPrint(printDocument,new
PrintEventArgs());
}
}
}
The printing process is summarized
When an application is added printDocument form initialization configuration PrintDocument objects
PrintPage method
2 PrintPage way to achieve 4 calls printDocument in the user's click event
Print method enables the printing function
In the middle might want to use PrintPreviewDialog PrintDialog
PageSetupDialog settings and view print effect