读取相同风格的dox、docx文件

发布于 2024-11-25 08:26:44 字数 1016 浏览 2 评论 0原文

我使用下面的代码读取 doc、docx 文件并在 richtextbox 中显示它们,但这种方式无法显示具有相同样式的文件。例如,如果文本为红色,richtextbox 将显示为黑色 我应该怎么办 ?

 Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
        object nullobj = System.Reflection.Missing.Value;
        object file = openFileDialog1.FileName;

        Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        IDataObject data = Clipboard.GetDataObject();
        string text = data.GetData(DataFormats.Text).ToString();
        Console.WriteLine(text);
        doc.Close(ref nullobj, ref nullobj, ref nullobj);
        app.Quit(ref nullobj, ref nullobj, ref nullobj);

        richTextBox1.Text = text;

I use below code for read doc,docx files and show them in richtextbox but this way can't show files with the same style.for example if text is with red color richtextbox show it with black color
what should i do ?

 Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.ApplicationClass();
        object nullobj = System.Reflection.Missing.Value;
        object file = openFileDialog1.FileName;

        Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(ref file, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj, ref nullobj);

        doc.ActiveWindow.Selection.WholeStory();
        doc.ActiveWindow.Selection.Copy();
        IDataObject data = Clipboard.GetDataObject();
        string text = data.GetData(DataFormats.Text).ToString();
        Console.WriteLine(text);
        doc.Close(ref nullobj, ref nullobj, ref nullobj);
        app.Quit(ref nullobj, ref nullobj, ref nullobj);

        richTextBox1.Text = text;

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

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

发布评论

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

评论(2

沩ん囻菔务 2024-12-02 08:26:44

我认为在这种情况下你运气不好。

Richtextbox 远不如 Word 强大。如果是这样,每个人都会复制 Word,微软就会赔钱。

您可能会使其部分工作(就像您所做的那样)。但永远不够好。

I think you are out of luck in this case.

Richtextbox is nowhere near as powerful as Word. If it were, everybody would make copies of Word and MS would loose money.

You may get it working partially (like you did). But never good enough.

西瑶 2024-12-02 08:26:44

我建议你看看 Telerik's RadRichTextBox 之类的东西,它可以正确读取处理 .docx (我认为是 .doc)文件。否则,您将必须将数据转换为 RichTextBox 控件可以理解的内容(其中您将失去很多功能)。

I would suggest you look at something like Telerik's RadRichTextBox which can read properly handle .docx (and I think .doc) files. Otherwise you are going to have to convert the data to something the RichTextBox control can understand (in which you'll lose a lot of features).

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