Rishabh
Rishabh

Reputation: 23

PageBreak in flow documents at runtime

I AM able to get a text file on a flow document but now I have to divide the contents in proper pagebreaks at runtime i.e if contents are huge they shud get itself in number of pages that too at runtime.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TextOnFlowDoc
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {
        public Page1()
        {
            InitializeComponent();
            Paragraph paragraph = new Paragraph();

            paragraph.Inlines.Add(System.IO.File.ReadAllText(@"C:\Lis.txt"));
            paragraph.FontFamily = new FontFamily("CourierNew");

            FlowDocument document = new FlowDocument(paragraph);
            // FlowDocumentReader rdr = new FlowDocumentReader();
            FlowDocScl.Document = document;
        }
    }
}

Now this "FlowDocScl" is now a flow document and needs to be breaked into pages AT RUNTIME.

Upvotes: 0

Views: 4670

Answers (1)

brunnerh
brunnerh

Reputation: 184441

I am not sure why you want custom page-breaks, if you display it in a FlowDocumentPageViewer for example you get automatic breaks if the content is too large for the viewer.

If you must insert breaks on demand you need to split the document in Blocks, those have a property called BreakPageBefore which when set to true inserts a page break before that block obviously.

Something like this (untested):

private void BreakAndAddText(string text)
{
    var pages = text.Split(new string[] { "\\f" }, StringSplitOptions.None);
    foreach (var page in pages)
    {
        document.Blocks.Add(new Paragraph(new Run(page)) { BreakPageBefore = true });
    }
}

Upvotes: 2

Related Questions