Search code examples
silverlightprintdocument

Printing text in Silverlight that measures larger than page


I have a silverlight application that allows people to enter into a notes field which can be printed, the code used to do this is:

PrintDocument pd = new PrintDocument();

        Viewbox box = new Viewbox();
        TextBlock txt = new TextBlock();
        txt.TextWrapping = TextWrapping.Wrap;
        Paragraph pg = new Paragraph();
        Run run = new Run();
        pg = (Paragraph)rtText.Blocks[0];
        run = (Run)pg.Inlines[0];
        txt.Text = run.Text;

        pd.PrintPage += (s, pe) =>
        {
            double grdHeight = pe.PrintableArea.Height - (pe.PageMargins.Top + pe.PageMargins.Bottom);
            double grdWidth = pe.PrintableArea.Width - (pe.PageMargins.Left + pe.PageMargins.Right);
            txt.Width = grdWidth;
            txt.Height = grdHeight;
            pe.PageVisual = txt;
        };

        pd.Print(lblTitle.Text);

This simply prints the content of the textbox on the page however some of the notes are spanning larger than the page itself causing it to be cut off. How can I change my code to measure the text and add more pages OR is there a better way to do the above where it will automatically create multiple pages for me?


Solution

  • There are several solutions to your problem, all of them under "Multiple Page Printing Silverlight" on Google. I was having a similar problem and tried most of them. The only one that worked for me was this one:

    http://www.codeproject.com/Tips/248553/Silverlight-converting-to-image-and-printing-an-UI

    But honestly you should look at Google first and see whether there are better solutions to your specific problem.

    Answering your question, there is a flag called HasMorePages that indicates you need a new page. Just type pe.HasMorePages and you will see.

    Hope it helps