コード例 #1
0
        //</SnippetClipboard>
        #endregion

        #region Print

        //<SnippetPrint>
        //Print the document
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintPreview cw = new PrintPreview();

            cw.ShowPreview(rtb);
            cw.HasCloseButton = false;

            //Hook up a handler to the Closed event before displaying the PrintPreview window by calling the Show() method.
            cw.Closed += (t, a) =>
            {
                if (cw.DialogResult.Value)
                {
                    PrintDocument theDoc = new PrintDocument();
                    theDoc.PrintPage += (s, args) =>
                    {
                        args.PageVisual   = rtb;
                        args.HasMorePages = false;
                    };

                    theDoc.EndPrint += (s, args) =>
                    {
                        MessageBox.Show("The document printed successfully", "Text Editor", MessageBoxButton.OK);
                    };

                    theDoc.Print("Silverlight 4 Text Editor");
                    ReturnFocus();
                }
            };
            cw.Show();
        }
コード例 #2
0
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            PrintPreview cw = new PrintPreview();

            cw.ShowPreview(rta);
            cw.HasCloseButton = false;
            cw.Closed        += (t, a) =>
            {
                if (cw.DialogResult.Value)
                {
                    PrintDocument theDoc = new PrintDocument();
                    RichTextArea  rta2   = new RichTextArea();
                    rta2.Width        = 816;
                    rta2.TextWrapping = TextWrapping.Wrap;
                    rta.FontSize      = 20;
                    Paragraph         p    = null;
                    Paragraph         bp   = null;
                    InlineUIContainer iuic = null;
                    Run r  = null;
                    Run br = null;
                    for (int i = 0; i < rta.Blocks.Count; i++)
                    {
                        if (rta.Blocks[i] is Paragraph)
                        {
                            p  = new Paragraph();
                            bp = ((Paragraph)rta.Blocks[i]);
                            for (int j = 0; j < bp.Inlines.Count; j++)
                            {
                                if (bp.Inlines[j] is Run)
                                {
                                    br = ((Run)bp.Inlines[j]);

                                    r                 = new Run();
                                    r.Text            = br.Text;
                                    r.FontFamily      = br.FontFamily;
                                    r.FontSize        = br.FontSize;
                                    r.FontStretch     = br.FontStretch;
                                    r.FontStyle       = br.FontStyle;
                                    r.FontWeight      = br.FontWeight;
                                    r.Foreground      = br.Foreground;
                                    r.TextDecorations = br.TextDecorations;
                                    p.Inlines.Add(r);
                                }
                                if (bp.Inlines[j] is InlineUIContainer)
                                {
                                    iuic = new InlineUIContainer();
                                    if (((InlineUIContainer)bp.Inlines[j]).Child is Image)
                                    {
                                        iuic.Child = getImage();
                                    }
                                    else if (((InlineUIContainer)bp.Inlines[j]).Child is DataGrid)
                                    {
                                        iuic.Child = getDataGrid();
                                    }
                                    else if (((InlineUIContainer)bp.Inlines[j]).Child is Calendar)
                                    {
                                        iuic.Child = getCalendar();
                                    }
                                    p.Inlines.Add(iuic);
                                }
                            }
                            rta2.Blocks.Add(p);
                        }
                    }

                    theDoc.DocumentName = "Silverlight 4 Text Editor";
                    theDoc.PrintPage   += (s, args) =>
                    {
                        //Size sz = args.PrintableArea;

                        //layout again
                        //WriteableBitmap wb = new WriteableBitmap(rta2, null);
                        //Image previewImage = new Image();
                        //previewImage.Source = wb;
                        //args.PageVisual = previewImage;
                        args.PageVisual   = rta2;
                        args.HasMorePages = false;
                    };

                    theDoc.EndPrint += (s, args) =>
                    {
                        MessageBox.Show("The document printed successfully", "Text Editor", MessageBoxButton.OK);
                    };

                    theDoc.Print();
                    ReturnFocus();
                }
            };
            cw.Show();
        }