c# - How to read parts of an RTF file and keep formatting -
i asked create control reads in rtf file , display file in parts. problem running font styling seems stored @ higher level snippets pulling out. there way read snippets 1 file , keep formatting? using richtextbox in wpf.
this not perfect solution, works specific scenario. posting in case helps others later, still looking better solution. below work, has 2 pre-requisites:
- my delimiter has @ beginning , end of text
- my delimiter has different font or style has own distinct markup surrounding text
code:
private static ienumerable<string> getlistfromdelimitedtext(string text) { var textsplit = text.split(new[] { "~~~~~~~~" }, stringsplitoptions.none); var header = getheaderofrtf(textsplit); var footer = getfooterofrtf(textsplit); var listwithoutheaderandfooter = textsplit.where((text, index) => index > 0 && index < textsplit.length - 1); return getsnippetswithheaderandfooter(listwithoutheaderandfooter, header, footer); } private static ienumerable<string> getsnippetswithheaderandfooter(ienumerable<string> snippetlist, string header, string footer) { return snippetlist.select(text => { var textwithoutleadingbracket = text.substring(text.indexof('}') + 1); var cleanedtext = textwithoutleadingbracket.substring(0, textwithoutleadingbracket.lastindexof('{')); return header + cleanedtext + footer; } ); } private static string getfooterofrtf(ienumerable<string> textsplit) { var lastsplit = textsplit.last(); return lastsplit.substring(lastsplit.indexof('}') + 1); } private static string getheaderofrtf(ienumerable<string> textsplit) { var firstsplit = textsplit.first(); return firstsplit.substring(0, firstsplit.lastindexof('{')); }
Comments
Post a Comment