wpf 在富文本框中加载 word 文件 (.docx)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30722434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:11:30  来源:igfitidea点击:

load word file (.docx) in richtextbox

c#wpftextms-wordrichtextbox

提问by Phil

I was already able to load a .docx file into my wpf application but it doesn't seem to show up in my richtextbox:

我已经能够将 .docx 文件加载到我的 wpf 应用程序中,但它似乎没有出现在我的富文本框中:

if (openFile.ShowDialog() == true)
{
     // Open document 
     string originalfilename = System.IO.Path.GetFullPath(openFile.FileName);

     if (openFile.CheckFileExists)
     {
         var document = DocX.Load(originalfilename);
         string contents = document.Text;
         rtfMain.Document = contents; 

         MessageBox.Show("file loaded");
     }
} 

The contents string variable is not accepted by the richtextbox in wpf. Any idea how to make it work?

wpf 中的富文本框不接受内容字符串变量。知道如何使它工作吗?

回答by Matias Cicero

That code shouldn't compile, RichTextBox.Documentis of type FlowDocument, and you are assigning it to a string.

该代码不应编译,RichTextBox.Document属于 类型FlowDocument,并且您将其分配给string.

Maybe you should look for ways to convert a .docxfile to a FlowDocumentyou can assign to the RichTextBox.

也许您应该寻找将.docx文件转换为FlowDocument可以分配给.docx文件的方法RichTextBox

There is a popular tool you can use which is called: Word to XAML

您可以使用一种流行的工具,称为:Word to XAML

Another options are:

另一种选择是:

回答by Kassoum Traoré

 if (openFile.ShowDialog() == true)
        {
            // Open document 
            string originalfilename = System.IO.Path.GetFullPath(openFile.FileName);

            if (openFile.CheckFileExists && new[] { ".docx", ".doc", ".txt", ".rtf" }.Contains(Path.GetExtension(originalfilename).ToLower()))
            {
                Microsoft.Office.Interop.Word.Application wordObject = new Microsoft.Office.Interop.Word.Application();
                object File = originalfilename;
                object nullobject = System.Reflection.Missing.Value;
                Microsoft.Office.Interop.Word.Application wordobject = new Microsoft.Office.Interop.Word.Application();
                wordobject.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsNone;
                Microsoft.Office.Interop.Word._Document docs = wordObject.Documents.Open(ref File, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject, ref nullobject);
                docs.ActiveWindow.Selection.WholeStory();
                docs.ActiveWindow.Selection.Copy();
                rtfMain.Document.Paste();
                docs.Close(ref nullobject, ref nullobject, ref nullobject);
                wordobject.Quit(ref nullobject, ref nullobject, ref nullobject);


                MessageBox.Show("file loaded");
            }
        }