Antonio Bakula
Antonio Bakula

Reputation: 20693

RichTextBlock overflow, keep paragraphs together on one page (in one overflow control)

I am printing RichTextBlock contents, using RichTextOverflow for pagination, it's working great but I would like to keep paragraphs together on one page, and now sometimes I have page breaks in middle of paragraph.

Here is the example :

printing problem

Paragraph 3 should be on the next page, it seems that ITextParagraphFormat.KeepTogether is just what I need but don't know how to get that for RichTextBlock.

Upvotes: 0

Views: 757

Answers (2)

Denis
Denis

Reputation: 4115

If you put your paragraph inside TextBlock/RTB inside InlineUIContainer it will be treated as a single element and will carry over to the next page unbroken if needed. This approach may have some side effects, but I believe it should work for printing.

Upvotes: 2

Antonio Bakula
Antonio Bakula

Reputation: 20693

After all I made a method that will "push" paragraphs that are broken on two pages on to a next page, I am not very happy about this solution but I didn't found another way

public static void BreakBeforeLastParagraph(RichTextBlockOverflow rto)
{
  if (!rto.HasOverflowContent)
    return;

  var pageBreak = rto.ContentEnd.Offset;
  var brokenPar = rto.ContentSource.Blocks.FirstOrDefault(pr => pr.ElementStart.Offset <= pageBreak && pr.ElementEnd.Offset >= pageBreak);
  if (brokenPar != null)
  {
    double spacerSize = pageBreak - brokenPar.ElementStart.Offset;
    var spacer = new Paragraph();
    spacer.Margin = new Windows.UI.Xaml.Thickness(0,spacerSize, 0,0);
    rto.ContentSource.Blocks.Insert(rto.ContentSource.Blocks.IndexOf(brokenPar), spacer);
  }
  rto.UpdateLayout();      
}

Upvotes: 0

Related Questions