Question

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?

Was it helpful?

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

OTHER TIPS

  • First you need to work out how many pages are needed

    Dim pagesNeeded As Integer = Math.Ceiling(gridHeight / pageHeight) 'gets number of pages needed

  • Then once the first page has been sent to the printer, you need to move that data out of view and bring the new data into view ready to print. I do this by converting the whole dataset into an image/UI element, i can then adjust Y value accordingly to bring the next set of required data on screen.

    transformGroup.Children.Add(New TranslateTransform() With {.Y = -(pageIndex * pageHeight)})

  • Then once the number of needed pages is reached, tell the printer to stop

    'sets if there is more than 1 page to print If pagesLeft <= 0 Then e.HasMorePages = False Exit Sub Else e.HasMorePages = True End If

Or if this is too much work, you can simply just scale all the notes to fit onto screen. Again probably by converting to UI element.

Hope this helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top