There is an rtf file. You need to open it and write each page to your richtextbox . It is required to write it in richtextbox.rtf so that the formatting and all the tables are preserved. How is this possible to implement?

upd. I did this as follows: I opened the rtf file in the temporary richtextbox, wrote it in the string variable str = richtextbox.rtf, after richtextbox.text = str. and further in the text I searched for a page break (in rtf a page break "\ page"), copied an rtf header in which the font, font size, etc. were indicated, and added what was between breaks, then assigned the resulting string to the desired richtextbox.rtf

  • 2
    What have you tried? - VladD

1 answer 1

You can download rtf to offline FlowDocument , and pull pieces out of it. For example:

 var document = new FlowDocument(); TextRange txtRange = new TextRange(document.ContentStart, document.ContentEnd); using (var stream = File.OpenRead(@"D:\simple.rtf")) txtRange.Load(stream, DataFormats.Rtf); 

With FlowDocument , you can break it into pieces according to your logic. After splitting, each of the pieces can be easily inserted into another FlowDocument in this way:

 void CloneContent(TextRange from, TextRange to) { using (var s = new MemoryStream()) { from.Save(s, DataFormats.XamlPackage); to.Load(s, DataFormats.XamlPackage); } } void CopyInto(FlowDocument d, TextRange source) { CloneContent(source, new TextRange(d.ContentStart, d.ContentEnd)); } 
  • and how to break the document into pages, at the end of each page of the rtf file there is a page break, no experience with FlowDocunent, I cannot read. at the moment I load the rtf file into richtextbox1, and from there I copy the text for the rest, so the formatting and tables disappear. - NaPaS Ludoman
  • Well, you can scan the content and search for a page break, in theory. I will try to find how the page break is coded until I can. Look and you too. - VladD
  • Thanks for the help! - NaPaS Ludoman