0
votes

I am create a UWP/C# desktop application that needs to include reports. I am familiar using the macOS CoreText and CGContext to draw on screen, print or generate PDF documents. I can't seem to find anything similar for UWP. The Microsoft samples for UWP seem to use RichTextBlock and some other interesting approaches, but these seem to rely on the automatic formatting and text flow built in to the RichTextBlock so I don't seem to have control over basic things like starting a new page or being able to check the layout to keep paragraphs together.

Can anyone point me in the right direction for how to do this. Some of the basic requirements are:

  • text formatting - bold, underline, font size and weight, etc.
  • paragraph styles - indents, bullet, wrapping, etc.
  • page headers and footers
  • keep paragraphs together e.g. headings with following paragraphs.
  • Output in PDF or print.

Happy to use the windows print system for generating the PDF as it seems to convert to vector PDF which is fine although it seems under UWP it's not possible to programatically set all the print options and some user intervention is required - will live with that for now.

EDIT

I don't want to edit rich text, I want to generate a report from content in a database and need to be able to do things like force a page break for new sections, keep headers on the same page as the following paragraph, and so on.

With CoreText you can format a paragraph and ask CoreText API how much space it needs to render the text, you can then decide if you have enough space left on the page to print the paragraph or to start a new page.

It's not clear to me how one would achieve the same thing using the UWP APIs - but it must be possible.

For example if it was possible to set the width of a RichTextBlock and ask it how much height it needs to render it's content that would be great.

1
This is offtopicvasily.sib
BTW, there is a site, called Google.com where you can find what you want. Just type "UWP/C# Report Generation" and press "Google Search" buttonvasily.sib
Please check Rich edit box official document. You can use a RichEditBox control to enter and edit rich text documents that contain formatted text, hyperlinks, and images.Nico Zhu - MSFT
@NicoZhu-MSFT - thanks I am very familiar with RichEditBox - see my additions to the question above.Duncan Groenewald
For your scenario, please check RichTextBlockOverflow class that used to create advanced page layouts, such as multi-column text. The content for a RichTextBlockOverflow element always comes from a RichTextBlock element..Nico Zhu - MSFT

1 Answers

0
votes

OK so this is what I am doing...

Dynamically creating XAML pages for each report page - more or less like the UWP printing examples, except without the need of any printer.

And then for sizing paragraphs and starting new pages a relatively simple routine like this :

      private void addParagraph(Paragraph para)
        {
            if (contentCanvas == null)
            {
                return;
            }

            RichTextBlock rtb = new RichTextBlock();
            rtb.Blocks.Add(para);
            rtb.TextWrapping = TextWrapping.Wrap;
            rtb.FontSize = 12;
            rtb.Foreground = new SolidColorBrush(Colors.Black);
            rtb.Width = contentCanvas.Width;
            rtb.Measure(new Size(contentCanvas.Width, Double.PositiveInfinity));
            var size = rtb.DesiredSize;
            rtb.Height = size.Height;


            double fHeight = rtb.Height;
            double cHeight = contentCanvas.Height;

            Debug.WriteLine("y: " + y);
            Debug.WriteLine("cH: " + cHeight);
            Debug.WriteLine("cW: " + contentCanvas.Width);
            Debug.WriteLine("rtb.H: " + fHeight);

            if ((y + fHeight) > contentCanvas.Height)
            {
                // Create a new page
                createPage();
            }

            contentCanvas.Children.Add(rtb);

            Canvas.SetLeft(rtb, x);
            Canvas.SetTop(rtb, y);

            y += fHeight;


        }

A few issues because I have to fix the height and width of the contentCanvas in the XAML template - have not figured out how to do this at runtime and get values other than NaN back. It seems Canvas won't adopt and return the size of its parent. No doubt me not quite understanding the UWP layout system.