tags:

views:

526

answers:

1

I need to import a RTF document into a FlowDocument for further parsing.. But I have a very strange problem:

public string ConvertRTF(byte[] bytes)
{
    if (bytes == null)
    {
        throw new ArgumentNullException();
    }

    FlowDocument document = new FlowDocument();

    // open the file for reading
    using (MemoryStream stream = new MemoryStream(bytes, true))
    {
        // create a TextRange around the entire document
        TextRange documentTextRange = new TextRange(document.ContentStart, document.ContentEnd);
        if (documentTextRange.CanLoad(DataFormats.Rtf))
            documentTextRange.Load(stream, DataFormats.Rtf);
    }

    return XamlWriter.Save(document);

}

I have tested this method in three different projects:

  • Wpf stand alone app: gives me no problems what so ever, but alas, I cannot use this kind of application.
  • Console app: it often works, but it breaks on the documents with image from time to time, and I cannot get my finger around when it breaks and why... The error I am receiving is on the Load method of TextRange: "Unrecognized structure in data format 'Rich Text Format'. Parameter name: stream"
  • Xbap application: Does not even get past the CanLoad method... :( So gives me jack whathisname as a result...

The stange thing is, that when I test it with the console app it works without errors in the following construction:

[STAThread]
static void Main(string[] args)
{
    OpenFileDialog dialog = new OpenFileDialog
    {
        Filter = "import files (*.rtf)|*.rtf"
    };

    if (dialog.ShowDialog() != DialogResult.OK)
        return;


    byte[] data;
    using (Stream filestream = dialog.OpenFile())
    {
        int offset = 0;
        data = new byte[filestream.Length];
        int remaining = data.Length;
        while (remaining > 0)
        {
            int read = filestream.Read(data, offset, remaining);
            if (read <= 0)
                throw new EndOfStreamException
                    (String.Format("End of stream reached with {0} bytes left to read", remaining));
            remaining -= read;
            offset += read;
        }
    }

    FlowDocument document = new FlowDocument();

    using (MemoryStream stream = new MemoryStream(data))
    {
        // create a TextRange around the entire document
        TextRange documentTextRange = new TextRange(document.ContentStart, document.ContentEnd);
        documentTextRange.Load(stream, DataFormats.Rtf);
    }

    Console.WriteLine("test ok");
}

Which just make me clueless, because that is exactly what I am doing but then phased in two steps... first retrieve the bits, then use the memorystream to make it into a RTF... :(

Can it be that there might be a conflict in some dll version somehow? We are using 3.5 SP1 for our project...

Can someone help me find a solution for one of last two posibilities mentioned above?

Thanks

A: 

You likely have problems with the trust level. Xbap Internet applications defaults for partial trust. You may use certificate to allow full trust with xpab internet applications.

standard
Certificates are not an option.
Arcturus
I do not know why, but rtf requires the full trust.
standard