|
| ||||||||||
| Tags: onenote, pdf, printing, silverlight beta 4, uielement, xaml, xps |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| How to do Printing in Silverlight Beta 4
Class System.Windows.Printing.PrintDocument Classes for the media in Silverlight have been incorporated into System.Windows.Printing namespace, which contains, among other things, the PrintDocument class. This class PrintDocument class is responsible for sending to print the content you want. The PrintDocument class does not expose a property so we can determine the content you wish to print. However, the method Print () when run opens the print dialog box of our operating system. After selecting the destination printer (physical or virtual), trigger the following sequence of events:
Event StartPrint The event fires once StartPrint closed the dialog box for selecting printer. Immediately after the next event is fired: PrintPage. Event PrintPage Following the event was triggered StartPrint PrintPage. This is the most important event in the print model as it is in the grounds where we actually establish the content we want to print. This is defined in the property which is of type PageVisual UIElement. Since it is of type UIElement content can actually set as anything: whether it is present in the tree of XAML or is content created dynamically (through code or using XamlReader.Load (), etc.). For example, if we take a snapshot to the current application and print, we can establish as PageVisual the root container object (eg LayoutRoot). Or else, if we create a custom printing can generate wrappers and elements dynamically. Besides PageVisual property we have a couple of properties: HasMorePages and PrintableArea. HasMorePages is a property that indicates whether there are still more pages to print, for example in the case of long documents, lists, catalogs etc.. If we set this to true, then the PrintPage event will be fired. PrintableArea indicates the size that has the print area as the selected printer. For example it is not the same size if we send a print to OneNote that a PDF or a physical printer. This property is of type Size and we will also be used to calculate margins , positions of elements, etc. It should be noted that if the selected printer still requires user interaction, this will continue (to indicate the name of a file .Xps or .Pdf for example). |
|
#2
| |||
| |||
| Re: How to do Printing in Silverlight Beta 4 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> 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"); 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;
); 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));
) |
|
#3
| |||
| |||
| Re: How to do Printing in Silverlight Beta 4
In the second button will have a slight variation: send to print multiple pages. This is accomplished after you set the HasMorePages = true. In the case of this example we have set up a class level variable called page that will be our flag. This flag is we'll be reviewing until the completion of 4 pages in total. Code: pd. PrintPage + = (s, a) =>
(
canvas = new Canvas () (Width = a. PrintableArea.Width, Height = a.PrintableArea.Height);
EditTitle ("This is a test print");
EditSubtitle (string. Format ("Page (0)", page + 1));
to.PageVisual = canvas;
to.HasMorePages =! (page == 3);
page + +;
); Code: EditSubtitle void (string title)
(
// Create a TextBlock to serve as a subtitle
Content TextBlock = new TextBlock () (Text = title, FontSize = 14);
canvas.Children. Add (content);
Canvas.SetLeft (content canvas. Width / 2 - (content.ActualWidth / 2));
Canvas.SetTop (content, 40);
)
private void button2_Click (object sender, RoutedEventArgs e)
(
PrintDocument pd = new PrintDocument () (DocumentName = "Print Example");
pd.PrintPage + = (s, a) =>
(
canvas = new Canvas () (Width = a.PrintableArea. Width, Height = a.PrintableArea.Height);
EditTitle ("This is a test print");
CrearSubtitulo (string.Format ("Page (0)", page + 1));
to.PageVisual = canvas;
to.HasMorePages =! (page == 3);
page + +;
);
pd.StartPrint + = (s, a) => textBlock1. Text = "Starting printing ..."
pd.EndPrint + = (s, a) => textBlock1. Text = "Print Complete";
pd.Print ();
) System.Windows.Printing.PrintDocument class. This class includes the method Print (), which when executed triggers a series of events including the event where we can establish PrintPage the desired content for print, knowing the size of the print area and determine if there are more pages to print (for scenes of documents over a page). |
|
#4
| |||
| |||
| Re: How to do Printing in Silverlight Beta 4
Your explanation is very clear, excellent! What would be an example without displaying the print dialog, or print directly to a printer (pre-selected by the application by the name of the printer). Again, many thanks for your work. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "How to do Printing in Silverlight Beta 4" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to install SilverLight 5 beta and SilverLight 4 on same Computer | Mandarmalika | Windows Software | 3 | 17-04-2011 04:55 PM |
| Silverlight 4 RC with Blend 4 Beta | Obama P | Windows Software | 6 | 06-08-2010 10:21 AM |
| Printing and Parameters issues in Silverlight Beta 4 | Mulan | Windows Software | 5 | 05-08-2010 11:14 PM |
| Uninstalling Silverlight 4 Beta on OSX | Fausty | Windows Software | 3 | 05-08-2010 05:11 PM |
| Silverlight 4 DataGrid is not printing properly. | Adrut | Windows Software | 3 | 03-08-2010 05:15 AM |