【问题标题】:Import RTF with images in xbap在 xbap 中导入带有图像的 RTF
【发布时间】:2010-10-12 23:10:16
【问题描述】:

我需要将一个 RTF 文档导入到 FlowDocument 中进行进一步解析。但是我有一个很奇怪的问题:

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);

}

我已经在三个不同的项目中测试了这种方法:

  • Wpf 独立应用程序:从来没有给我带来任何问题,但是我不能使用这种应用程序。
  • 控制台应用程序:它经常工作,但它有时会在带有图像的文档上中断,当它中断时我无法找到我的手指,为什么......我收到的错误是TextRange的加载方法:“数据格式'Rich Text Format'中无法识别的结构。参数名称:流”
  • Xbap 应用程序:甚至没有通过 CanLoad 方法... :( 所以给了我 jack whathisname 结果...

奇怪的是,当我使用控制台应用程序对其进行测试时,它在以下构造中可以正常工作:

[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");
}

这只是让我一无所知,因为这正是我正在做的,但随后分两步进行......首先检索位,然后使用内存流将其制成 RTF...... :(

会不会是某些 dll 版本中存在某种冲突?我们正在为我们的项目使用 3.5 SP1...

有人可以帮我找到上述最后两个可能性之一的解决方案吗?

谢谢

【问题讨论】:

    标签: c# wpf xaml rtf xbap


    【解决方案1】:

    您的信任级别可能存在问题。 Xbap Internet 应用程序默认为部分信任。您可以使用证书来完全信任 xpab 互联网应用程序。

    【讨论】:

    • 证书不是一个选项。
    • 不知道为什么,rtf需要完全信任。
    【解决方案2】:

    显然做不到。

    我们最终将 rtf 发送到具有更多权限的服务器,并将结果发送回客户端。讨厌,但它有效。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 1970-01-01
      • 2015-12-02
      • 2011-07-11
      • 2022-01-18
      • 2010-12-10
      • 2012-05-07
      • 2013-01-24
      • 2011-03-04
      相关资源
      最近更新 更多