在 xbap 中导入带图像的 RTF

发布于 2024-07-15 03:30:24 字数 2353 浏览 8 评论 0原文

我需要将 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 的加载方法:“无法识别数据格式‘富文本格式’的结构。参数名称:流”
  • Xbap 应用程序:甚至无法通过 CanLoad 方法...:( 所以给了我杰克什么名字结果...

奇怪的是,当我用控制台应用程序测试它时,它在以下构造中没有错误:

[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...

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-07-22 03:30:25

显然无法完成。

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

Cannot be done apparently.

What we ended up with is sending the rtf to the server who has more privileges, and send the result back to the client. Nasty, but it works.

晨曦÷微暖 2024-07-22 03:30:24

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文