查看DOCX WebBrowser控件中的文档 [英] View Docx documents in WebBrowser Control

查看:149
本文介绍了查看DOCX WebBrowser控件中的文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直想了几天,现在一个字的docx文件加载到存在于Windows窗体C#中的WebBrowser控件。
挣扎天后完成这件事,但与谷歌的帮助下,我已经成功地做​​了一些有益的职位,这是beauuuuuutiful。我已经做到了通过。

I've been trying for a few days now to load a word docx file into a webbrowser control that exists in a windows form c#. After struggling for days to get this done but with the help of Google and some helpful posts I've managed to do and it is beauuuuuutiful. I've done it through:


  1. 的DOCX文件转换成一个临时的HTML文件

  2. 我驾驶我的WebBrowser控件到临时HTML文档

只有我注意到一个问题:
WebBrowser控件似乎是在Web版式查看文件。这是女士字Web版式,你知道有在MS-Word中,读取模式,打印布局和Web版式3个主要观赏布局。这里的问题是,一些重格式化的docx文件被扭曲都用光了WebBrowser控件,因为它延伸出来,如果​​他们将出现在一个实际的Web浏览器应用程序。

Only that I've noticed one problem: The webbrowser control seems to view the files in Web Layout. That is Ms-Word Web Layout, you know there are 3 main viewing layouts in Ms-Word, Read Mode, Print Layout, and Web Layout. The Problem with this is that some of the heavily formatted docx files gets skewed all up in that webbrowser control because it stretches them out as if they would appear in an actual web browser application.

现在我想实现的是要能够查看WebBrowser控件的内容相似,在MS-Word中打印布局,或至少是控制改装的控制自己的尺寸范围内的内容的东西。

Now what I would like to achieve is to be able to view the content of that webbrowser control in something similar to the Print Layout in Ms-Word, or at least for the control to refit the content within the control's own size.

(如果我的代码是必要的话,我可以提供的话)

(If my code is necessary then I can provide it)

推荐答案

有对于保存方法来选择布局可没有这样的选择。

There is no such option available for the save method to choose a layout.

您可以使用web浏览器作为ActiveX文档服务器然后访问DOM字。设置布局类型的方法是通过Document.ActiveWindow.View.Type:

You can use the webbrowser as an ActiveX document server then access the word DOM. The way to set a layout type is via Document.ActiveWindow.View.Type:

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            var webbrowser = webBrowser1.ActiveXInstance as SHDocVw.IWebBrowser2;
            var document =webbrowser.Document;
            if (document != null)
            {
                var wordDocument = document as Microsoft.Office.Interop.Word.Document ;
                if (wordDocument != null)
                {
                    var activeWindow=wordDocument.ActiveWindow;
                    if (activeWindow != null)
                    {
                        var view=activeWindow.View;
                        if (view != null)
                        {
                            view.Type = WdViewType.wdPrintView;
                            Marshal.ReleaseComObject(view);
                        }
                        Marshal.ReleaseComObject(activeWindow);
                    }
                    Marshal.ReleaseComObject(wordDocument);
                }
                Marshal.ReleaseComObject(document);
            }
            Marshal.ReleaseComObject(webbrowser);
        }
    }



根据不安全的对象用户的Internet安全设置,您可能会看到一个提示,打开文档之前,或者干脆从IWebBrowser2.Document得到空(因此不能自动字DOM)。

Depending on the user's internet security settings for unsafe objects, you may see a prompt before opening the document, or simply get null from IWebBrowser2.Document (thus can't automate the word DOM).

这篇关于查看DOCX WebBrowser控件中的文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆