【问题标题】:View Docx documents in WebBrowser Control在 WebBrowser 控件中查看 Docx 文档
【发布时间】:2013-08-14 18:12:47
【问题描述】:

几天来,我一直在尝试将 word docx 文件加载到存在于 windows 窗体 c# 中的 webbrowser 控件中。 经过几天努力完成这项工作,但在谷歌的帮助和一些有用的帖子的帮助下,我设法做到了,这真是太棒了。我已经完成了:

  1. 将 docx 文件转换为临时 html 文件。
  2. 我将网络浏览器控件导航到该临时 html 文档。

我只注意到一个问题: webbrowser 控件似乎在 Web 布局中查看文件。那就是 Ms-Word Web Layout,你知道 Ms-Word 有 3 种主要的查看布局,阅读模式,打印布局和 Web 布局。这样做的问题是,一些重格式的 docx 文件在该 webbrowser 控件中全部倾斜,因为它会将它们拉伸,就好像它们会出现在实际的 web 浏览器应用程序中一样。

现在我想要实现的是能够以类似于 Ms-Word 中的打印布局的方式查看该 webbrowser 控件的内容,或者至少让控件能够在控件自身的大小范围内重新调整内容。

(如果需要我的代码,我可以提供)

【问题讨论】:

  • 这是一个 Windows 窗体应用程序。我已将它作为自定义控件添加到我的工具箱中,右键单击工具箱>>“选择工具箱项(窗口)/ .NET Framework 组件(选项卡)/ Web 浏览器(System.windows.Forms 命名空间)。所以我认为这是 WinForm 控件,但如果我错了,请纠正我。

标签: c# winforms ms-word webbrowser-control docx


【解决方案1】:

发现于http://codinglight.blogspot.de/2008/10/simple-docbrowser-control.html

这使用 webBrowser-Control,但将您的文档转换为 HTML 文件。 此外,您还必须安装 MS Word。

使用以下代码创建一个类:

using System;
using System.Linq;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.IO;

namespace WordControls
{
    public partial class DocBrowser : UserControl
    {
        private System.Windows.Forms.WebBrowser webBrowser1;

    delegate void ConvertDocumentDelegate(string fileName);

    public DocBrowser()
    {
        InitializeComponent();

        // Create the webBrowser control on the UserControl. 
        // This code was moved from the designer for cut and paste
        // ease. 
        webBrowser1 = new System.Windows.Forms.WebBrowser();

        webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
        webBrowser1.Location = new System.Drawing.Point(0, 0);
        webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
        webBrowser1.Name = "webBrowser1";
        webBrowser1.Size = new System.Drawing.Size(532, 514);
        webBrowser1.TabIndex = 0;

        Controls.Add(webBrowser1);

        // set up an event handler to delete our temp file when we're done with it. 
        webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
    }

    string tempFileName = null;

    public void LoadDocument(string fileName)
    {
        // Call ConvertDocument asynchronously. 
        ConvertDocumentDelegate del = new ConvertDocumentDelegate(ConvertDocument);

        // Call DocumentConversionComplete when the method has completed. 
        del.BeginInvoke(fileName, DocumentConversionComplete, null);
    }

    void ConvertDocument(string fileName)
    {
        object m = System.Reflection.Missing.Value;
        object oldFileName = (object)fileName;
        object readOnly = (object)false;
        ApplicationClass ac = null;

        try
        {
            // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.
            ac = new ApplicationClass();

            // Now we open the document.
            Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
                ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                 ref m, ref m, ref m, ref m, ref m, ref m);

            // Create a temp file to save the HTML file to. 
            tempFileName = GetTempFile("html");

            // Cast these items to object.  The methods we're calling 
            // only take object types in their method parameters. 
            object newFileName = (object)tempFileName;

            // We will be saving this file as HTML format. 
            object fileType = (object)WdSaveFormat.wdFormatHTML;

            // Save the file. 
            doc.SaveAs(ref newFileName, ref fileType,
                ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                ref m, ref m, ref m, ref m, ref m, ref m, ref m);

        }
        finally
        {
            // Make sure we close the application class. 
            if (ac != null)
                ac.Quit(ref readOnly, ref m, ref m);
        }
    }

    void DocumentConversionComplete(IAsyncResult result)
    {
        // navigate to our temp file. 
        webBrowser1.Navigate(tempFileName);
    }

    void webBrowser1_DocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
        if (tempFileName != string.Empty)
        {
            // delete the temp file we created. 
            File.Delete(tempFileName);

            // set the tempFileName to an empty string. 
            tempFileName = string.Empty;
        }
    }

    string GetTempFile(string extension)
    {
        // Uses the Combine, GetTempPath, ChangeExtension, 
        // and GetRandomFile methods of Path to 
        // create a temp file of the extension we're looking for. 
        return Path.Combine(Path.GetTempPath(),
            Path.ChangeExtension(Path.GetRandomFileName(), extension));
        }
    }
}

将控件添加到表单并调用 LoadDocument 方法。

docBrowser1.LoadDocument(@"Path_to_Doc_as_String");

【讨论】:

  • 我看过上面的代码,它帮助我创建了自己的类,除了我认为它很长而且很多都被过度编码了。我有自己的小班,做同样的事情。其次,当我尝试原始课程时,该课程仍然几乎没有错误。最后但并非最不重要的一点是,即使该类在正常工作时检查了代码,它仍然会遇到与我在 Web 布局中加载 word 文档相同的问题。我希望将文档加载打印布局或将其内容正确地重新安装在 webbrowser 控件中。谢谢。
  • 当然,因为 webBrowser-Control 不是用来查看文档的。为什么你甚至必须使用 webBrowser?例如,您可以在应用程序中启动单词
【解决方案2】:

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

您可以将网络浏览器用作 ActiveX 文档服务器,然后访问单词 DOM。设置布局类型的方式是通过 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)。

【讨论】:

    猜你喜欢
    • 2013-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 2015-07-30
    • 1970-01-01
    • 2023-01-23
    • 1970-01-01
    相关资源
    最近更新 更多