Discussion Fastest way to convert RTF to FlowDocument

  • Tuesday, August 21, 2012 12:33 PM
     
     
    What is the fastest way to convert a RTF to FlowDocument? I store RTF as plain string and then reload it back, I am using following method,

    FlowDocument document = new FlowDocument();
    document.SetValue(FlowDocument.TextAlignmentProperty, TextAlignment.Left);

    TextRange content = new TextRange(document.ContentStart, document.ContentEnd);

    if (content.CanLoad(DataFormats.Rtf) && string.IsNullOrEmpty(rtf) == false)
    {
         // If so then load it with RTF
         byte[] valueArray = Encoding.ASCII.GetBytes(rtf);
         using (MemoryStream stream = new MemoryStream(valueArray))
         {
             content.Load(stream, DataFormats.Rtf);
         }
    }

    But this method is very slow. I need to load many RTFs (around 1000). What can be the trick to make the process fast? Is there any other way around to load a Flowdocument?

    Vibhore

    • Changed Type Vibhore Tanwer Sunday, October 07, 2012 2:00 PM As this code works but I just want to improve it. So I bettert put it as a discussion.
    •  

All Replies

  • Tuesday, August 21, 2012 2:21 PM
    Moderator
     
     

    Is it just running slow due to file size, or is it maxing out on cpu/memory?

    What location are they being read from, local disk or network?

    What media is the storage device, HDD or SSD?

    A complete reproducable example would help.

    Maybe you can simply use parallel Tasks to process them all at once, or in batches:

    How to: Write a Parallel.For Loop That Has Thread-Local Variables

    How to: Speed Up Small Loop Bodies

    Maybe you can do something with your PC performance,

    Or maybe you can manage how your computer spreads the work over the cores.

     

    Hope that helps?
    Pete


    #PEJL

  • Wednesday, August 22, 2012 7:57 AM
     
     

    I am loading it from hard disk, Most of the time is consumed in this single line

    content.Load(stream, DataFormats.Rtf);

    Somehow this is affecting performance. Parallel processing will not help in this case, I can divide task into loading each FlowDocument separately but problem is loading a single FlowDocument is pretty slow even my RTF is not that much big. I believe I am doing something wrong in loading RTF to FlowDocument. Is there any other way to do it? Using XAML may help but for some interoperability issues, I have to store RTF only.


    Vibhore