Printing text in Silverlight that measures larger than page

半城伤御伤魂 提交于 2019-12-25 12:13:47

问题


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?


回答1:


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




回答2:


  • 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



来源:https://stackoverflow.com/questions/20869456/printing-text-in-silverlight-that-measures-larger-than-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!