首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在WebBrowser控件中查看Docx文档

在WebBrowser控件中查看Docx文档
EN

Stack Overflow用户
提问于 2013-08-14 18:12:47
回答 2查看 13.2K关注 0票数 3

几天来,我一直试图将一个word docx文件加载到一个存在于windows表单c#中的few浏览器控件中。经过几天的努力来完成这个任务,但是在谷歌和一些有帮助的帖子的帮助下,我成功地做到了这一点,这是一个很好的选择。我已经做到了:

  1. 将docx文件转换为临时html文件。
  2. 我将我的webbrowser控件导航到那个临时html文档。

只是我注意到了一个问题: webbrowser控件似乎查看Web布局中的文件。即Ms-Word Web布局,您知道在Ms-Word、读取模式、打印布局和Web布局中有3种主要的查看布局。这方面的问题是,一些格式较重的docx文件在该web浏览器控件中被倾斜,因为它将它们展开,就好像它们会出现在实际的web浏览器应用程序中一样。

现在,我想要实现的是能够在类似于Ms-Word的打印布局中查看own浏览器控件的内容,或者至少让控件在控件自身大小的范围内重新修改内容。

(如果我的代码是必需的,那么我可以提供它)

EN

回答 2

Stack Overflow用户

发布于 2013-08-14 21:45:22

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

这使用一个webBrowser-控件,但是将您的文档转换为一个HTML-文件.此外,您将不得不安装MS。

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

代码语言:javascript
复制
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-方法。

代码语言:javascript
复制
docBrowser1.LoadDocument(@"Path_to_Doc_as_String");
票数 1
EN

Stack Overflow用户

发布于 2013-08-14 23:09:26

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

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

代码语言:javascript
复制
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)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18239208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档