1: Create a Silverlight4 application named SLStudy. After the configuration is completed as follows:
2: Create a Silverlight user control under SLStudy. Print1.xaml is used as the control to be printed.
Add the code in Print1.xaml as follows:
Copy codeThe Code is as follows: <Grid x: Name = "LayoutRoot" Background = "White">
<Button> This is the first example, simple Button </Button>
</Grid>
3: the content to be printed has been created. A button is printed here.
4: Modify the MainPage. xaml Code as follows:
Copy codeThe Code is as follows: <Grid x: Name = "LayoutRoot" Background = "White">
<StackPanel>
<Button x: Name = "btnPrint1" Click = "btnPrint1_Click"> Print1 </Button>
</StackPanel>
</Grid>
5: the background code is:
Copy codeThe Code is as follows: private void btnPrint1_Click (object sender, RoutedEventArgs e)
{
PrintDocument printDocument = new PrintDocument ();
PrintDocument. PrintPage + = new EventHandler <PrintPageEventArgs> (printDocument_PrintPage );
PrintDocument. Print ("Name of the document to be printed, which can be set as needed ");
}
Void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
E. PageVisual = new Print1 ();
}
The PrintDocument class is provided for printing in SL4, so an object of this class is instantiated first.
Register a PrintPage event. The PrintPage event is triggered during printing.
Call the Print method of printDocument to Print the image.
In the PrintPage event of PrintDocument, PrintPageEventArgs is the print parameter.
To obtain information about the current printer.
Set PageVisual here, that is, the object to be printed.
Copy codeThe Code is as follows: void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
E. PageVisual = new Print1 ();
}
After writing all the information, run the application and click Print1. The print window is displayed. The printing result is as follows:
Of course, our printing requirements cannot be so simple. You may need to set the content of print1. Suppose we want to modify the word displayed by the button, we can do this:
Copy codeThe Code is as follows: void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
Print1 printVisual = new Print1 ();
PrintVisual. btnSample. Content = "the modified value can also be obtained from the database ";
E. PageVisual = printVisual;
}
By setting the PageVisual object of the PrintPageEventArgs parameter, we can print the page.
Here I will summarize:
1: confirm the content to be printed, and create a new UserControl to display the printed content.
2: Create a PrintDocument object, register the PrintPage event, and call the Print method.
3: In the PrintPage event, construct the object to be printed, obtain data from the database, and then bind the data to the control, then, assign the bound data control to the PageVisual object of PrintPageEventArgs.
Multi-page printing:
If you only need to print one document, this method should be enough, but sometimes you need to print multiple documents,
For example, how can I print five buttons above.
Do you still remember the HasMorePages parameter of PrintPageEventArgs above?
After the PrintPage event is triggered, the default HasMorePages value is false. Set HasMorePages to true to enable the PrintPage event to be triggered continuously. When the HasMorePages attribute is true, the PrintPage event occurs multiple times until HasMorePages is false.
If we want to print five buttons above, set HasMorePages to true for four times, and set HasMorePages to false.
The modified printDocument_PrintPage method is as follows:
Copy codeThe Code is as follows: int count = 5;
Int printCount = 0;
Void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
Print1 printVisual = new Print1 ();
PrintVisual. btnSample. Content = "the modified value can also be obtained from the database ";
E. PageVisual = printVisual;
PrintCount ++;
If (printCount <count) // if the number of printed pages is smaller than the number of pages to be printed, it must be printed.
{
E. HasMorePages = true;
}
Else
{
E. HasMorePages = false;
}
}
Sometimes you need to know the page number that is currently printed, which can be obtained by querying the printDocument. PrintedPageCount attribute,
In the PrintDocument_PrintPage method, the sender object is actually a PrintDocument object, so we can force type conversion.
Suppose we want to change the content of the above five buttons to 1, 2, 3, 4, 5. Then we can modify the code:
Copy codeThe Code is as follows: int count = 5;
Int printCount = 0;
Void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
PrintDocument printDocument = sender as PrintDocument;
Print1 printVisual = new Print1 ();
PrintVisual. btnSample. Content = string. Format ("button {0}", printDocument. PrintedPageCount );
E. PageVisual = printVisual;
PrintCount ++;
If (printCount <count)
{
E. HasMorePages = true;
}
Else
{
E. HasMorePages = false;
}
}
In fact, we don't need all the printCount variables. Simply use printDocument. PrintedPageCount. You can implement the specific code by yourself.
Microsoft Bug ??
If your printer is set
The printed result is the *. xps file, but a prompt box is displayed during the printing process, asking for the SAVE address.
If you place a breakpoint in the PrintPage event, you can see that the PrintPage method has been executed when you ask to save the address, that is, the PringPage method will be executed twice, there is no real print for the first time.
For example:
If you click Cancel on the interface, the system may lose the response and become stuck,
If you click Save, The PrintPage event is triggered again.
However, since it has been printed once, it may cause problems when printing multiple pages.
Use two flag variables to solve this problem.
For example, modify the code:
Copy codeThe Code is as follows: int count = 5;
Int printCount = 0;
/// <Summary>
/// Whether it is the first printing, because only the second printing starts to print.
/// </Summary>
Private bool isInitialized = false;
Private bool realPrint = false;
Void printDocument_PrintPage (object sender, PrintPageEventArgs e)
{
PrintDocument printDocument = sender as PrintDocument;
Int currentPage = printDocument. PrintedPageCount;
# Because region has to go through two times, the first time is initialization, and the second time is the real printing, and the two PrintedPageCount are both 0
If (currentPage = 0)
{
If (isInitialized) // if it has been initialized, set realPrint to true
{
RealPrint = true;
}
IsInitialized = true; // run here, it indicates that the initialization has been completed.
}
# Endregion
If (realPrint)
{
// PrintDocument printDocument = sender as PrintDocument;
Print1 printVisual = new Print1 ();
PrintVisual. btnSample. Content = string. Format ("button {0}", printDocument. PrintedPageCount );
E. PageVisual = printVisual;
PrintCount ++;
If (printCount <count)
{
E. HasMorePages = true;
}
Else
{
E. HasMorePages = false;
}
}
}
Because two prints can be considered initialization for the first time, and the second can be considered as a printer to start printing,
Therefore, two variables, isInitialized and realPrint, can be used to indicate the initialization or actual printing.
During the first execution, printDocument. PrintedPageCount = 0. At this time, set isInitialized to true.
During the second execution, you can set realPrint to true because isInitialized = true.
In the subsequent code, you only need to judge that realPrint is true.