在 WebBrowser Control 中查看 Docx 文档 [英] View Docx documents in WebBrowser Control

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

问题描述

我已经尝试了几天来将 word docx 文件加载到存在于 windows 窗体 c# 中的 webbrowser 控件中.经过几天的努力才完成这项工作,但在 Google 的帮助和一些有用的帖子的帮助下,我设法做到了,而且很漂亮.我已经完成了:

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. 我将我的网络浏览器控件导航到该临时 html 文档.

只是我注意到一个问题:webbrowser 控件似乎在 Web 布局中查看文件.那就是 Ms-Word 网页版式,您知道 Ms-Word 中有 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.

现在我想要实现的是能够在类似于 Ms-Word 中的打印布局的东西中查看该 webbrowser 控件的内容,或者至少让控件在控件自己的大小内重新调整内容.

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)

推荐答案

save 方法没有这样的选项来选择布局.

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

您可以将网络浏览器用作 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 中获取 null(因此无法自动生成 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).

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

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