Example
Let's start with creating a new Silverlight project in April through Visual Studio 2010. In this project we will Demo.SL4.Impresion name. Visual Studio will create the solution together with the right project. Add to our MainPage.xaml the following code:
Code:
<Grid x: Name = "LayoutRoot" Background = "White">
<Button Content = "A page
Height = "47"
HorizontalAlignment = "Left"
Margin = "12,12,0,0"
Name = "button1"
VerticalAlignment = "Top"
Width = "115"
Click = "button1_Click" />
<Button Content = "Many pages"
Height = "47"
HorizontalAlignment = "Left"
Margin = "133,12,0,0"
Name = "button2"
VerticalAlignment = "Top"
Width = "115"
Click = "button2_Click" />
<TextBlock Height = "30"
HorizontalAlignment = "Left"
Margin = "14,70,0,0"
Name = "textBlock1"
VerticalAlignment = "Top"
Width = "372"
FontSize = "14" />
</ Grid>
The buttons will help us to send to print using the Print method execution () PrintDocument class and the TextBlock will serve to identify the status of printing.
In the first button click will create a new instance of PrintDocument and set up your property DocumentName properly. The value of this property will be what appears in the status window of the printer.
Code:
PrintDocument pd = new PrintDocument ()
(DocumentName = "Print Example");
However, we will handle the event PrintPage. In this case we use a lambda expression, but could also be a method:
Code:
pd.PrintPage + = (s, a) =>
(
// Canvas is a class level variable
// Set the width and height of canvas to using the property PrintableArea
canvas = new Canvas () (Width = a.PrintableArea.Width, Height = a.PrintableArea.Height);
// Add the desired title for the page
CrearTitulo ("This is a test print");
// Set the content property PageVisual
to. PageVisual = canvas;
);
We create an object of type Canvas, which will serve as a canvas to draw on the visual elements we want to print (in this example the content will be created in a TextBlock EditTitle method ()). The width and height of the Canvas object establish from PrintableArea property, which tells us the size of printing on the selected printer (for example, the print area is different in OneNote that one .xps).
Finally, we set this dynamically created content PageVisual property. This property indicates the visual content are going to print. The following code fragment contains all the complete code for this first example:
Code:
Canvas canvas = null;
int page = 0;
private void button1_Click (object sender, RoutedEventArgs e)
(
PrintDocument pd = new PrintDocument () (DocumentName = "Print Example");
pd.PrintPage + = (s, a) =>
(
// Canvas is a class level variable
// Set the width and height of canvas to using the property PrintableArea
canvas = new Canvas () (Width = a.PrintableArea.Width, Height = a.PrintableArea.Height);
// Add the desired title for the page
EditTitle ("This is a test print");
// Set the content property PageVisual
to.PageVisual = canvas;
);
pd.StartPrint + = (s, a) => textBlock1.Text = "Starting printing ..."
pd.EndPrint + = (s, a) => textBlock1.Text = "Print Complete";
pd.Print ();
)
EditTitle void (string title)
(
// Create a TextBlock that will serve as head
Content TextBlock = new TextBlock () (Text = title, FontSize = 20);
content.DropShadowEffect Effect = new ();
canvas.Children. Add (content);
Canvas.SetLeft (content canvas. Width / 2 - (content.ActualWidth / 2));
)
Bookmarks