Link to home
Create AccountLog in
Avatar of Annette Wilson, MSIS
Annette Wilson, MSISFlag for United States of America

asked on

Silverlight - Print Text on second page

Hi,

I am refractoring some code and trying to print the contents of a text box on a second page.  In the following code, I have created a user control "PrintPage" and tried to new up the user control and print the contents on a second page.  Can you tell me how I can accomplish this?  The Bold Code is what I added.

 
private void Printbutton_Click(object sender, RoutedEventArgs e)
        {
         
            [b]_lineIndex = 0;
            _documentBodyLines = new List<string>();

            string[] lines = commentsTextBox.Text.Split(new[] { '\r' }, StringSplitOptions.None);
            _documentBodyLines.AddRange(lines);[/b]
            PrintDocument pd = new PrintDocument();
            [b]pd.PrintPage += new EventHandler<PrintPageEventArgs>(printDocument_PrintPage);[/b]                                 
            pd.PrintPage += (s, args) =>
                {
                    this.CancelChangesButton.Visibility = System.Windows.Visibility.Collapsed;
                    this.SubmitChangesButton.Visibility = System.Windows.Visibility.Collapsed;
                    this.Print2.Visibility = System.Windows.Visibility.Collapsed;
                  
                    this.FilterGrid.Visibility = System.Windows.Visibility.Collapsed;
                   

                   
                    this.TimePrintedLbl.Content = "Printed: " + DateTime.Now.ToString();
                    args.PageVisual = this.Root2;
                   
                };
           
            pd.Print("frmForm_Pu_102_5v1 document");
            

            pd.EndPrint += (s, args) =>
                {
                    this.CancelChangesButton.Visibility = System.Windows.Visibility.Visible;
                    this.SubmitChangesButton.Visibility = System.Windows.Visibility.Visible;
                    this.Print2.Visibility = System.Windows.Visibility.Visible;
                    this.FilterGrid.Visibility = System.Windows.Visibility.Visible;
                    //this.supplierCertificationRadGridView.ShowInsertRow = true;
                    // this.FilterGrid.Height = FltGrdHeight;
                    this.TimePrintedLbl.Content = "";
                };
        }

           [b] void printDocument_PrintPage(object sender, PrintPageEventArgs e)
            {
                PrintPage page = new PrintPage();
                int numberOfLinesAdded = 0;
                while(_lineIndex < _documentBodyLines.Count)
                {
                    page.AddLine(_documentBodyLines[_lineIndex]);
                    page.Measure(new Size(e.PrintableArea.Width, double.PositiveInfinity));
                    if (page.DesiredSize.Height > e.PrintableArea.Height
                        && numberOfLinesAdded > 0)
                    {
                        page.RemoveLastLine();
                        e.HasMorePages = true;
                        break;
                    }
                    _lineIndex++;
                    numberOfLinesAdded++;
                }
                e.PageVisual = page;[/b]            }

Open in new window

Avatar of Aaron Jabamani
Aaron Jabamani
Flag of United Kingdom of Great Britain and Northern Ireland image

Why "pd.PrintPage" event is assigned two times in the begining?
Avatar of Annette Wilson, MSIS

ASKER

I assisgned the following event handler in an attempt to use it to help print the text that is in the comments box on the second page:

[b]pd.PrintPage += new EventHandler<PrintPageEventArgs>(printDocument_PrintPage);[/b]

Open in new window


This code is being used to try and print the second page:

 void printDocument_PrintPage(object sender, PrintPageEventArgs e)
            {
                PrintPage page = new PrintPage();
                int numberOfLinesAdded = 0;
                while(_lineIndex < _documentBodyLines.Count)
                {
                    page.AddLine(_documentBodyLines[_lineIndex]);
                    page.Measure(new Size(e.PrintableArea.Width, double.PositiveInfinity));
                    if (page.DesiredSize.Height > e.PrintableArea.Height
                        && numberOfLinesAdded > 0)
                    {
                        page.RemoveLastLine();
                        e.HasMorePages = true;
                        break;
                    }
                    _lineIndex++;
                    numberOfLinesAdded++;
                }
                e.PageVisual = page;

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Annette Wilson, MSIS
Annette Wilson, MSIS
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
After research, I found a solution using a Third Party control that does exactly what the requirement asks.