【问题标题】:How to print html in C#如何在 C# 中打印 html
【发布时间】:2011-10-06 15:37:42
【问题描述】:

我想在 C# 中使用 PrintDocument 打印文件。该文件是简单的 HTML(我需要它,因为我需要文件中的文本位于页面内的特定位置。)

我的问题是,我如何打印文件,这样它就不会打印 HTML 本身(标签等),而是打印在 Web 浏览器中显示的 HTML?

【问题讨论】:

  • uhhhhhhhhhhhhhhhhhhhhhhhh。我的蜘蛛感在刺痛。是否可以选择使用用户默认浏览器(或便携式 Firefox)启动网页?否则你必须使用 IE。我也曾经使用过 IE 控件。我让它浏览了一个著名的网站,那天它碰巧有恶意软件广告(google.com/safebrowsing/diagnostic?site=thesite.com 说在 13kpages 中只有一个有恶意软件,所以它非常罕见),不幸的是,自从安装了 IE6 后,我感染了病毒,我真的很生气。
  • windows 窗体但不是米特

标签: c# html printing printdocument


【解决方案1】:

使用web browser control 并像这样调用它的打印方法:

private void PrintHelpPage()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}

private void PrintDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).Print();

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

MSDN Article on doing this

【讨论】:

  • 但是当我们需要打印到网络打印机时该怎么办呢?如果我调用浏览器打印方法,它不会选择我需要的任意打印机。
【解决方案2】:

使用此方法可以成功打印 HTML,因为存在会导致 DocumentCompleted 事件多次触发的错误。我有一个简单的解决方案 -

private class yourClassName
{
        WebBrowser webBrowserForPrinting;
        public void YourForm_Load()
        {
             webBrowserForPrinting = new webBrowserForPrinting();
             // Set the Url property to load the document.
             webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
             webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
        }

        int i = 0;
        private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            i++;
            if (i == 1)
            {
                ((WebBrowser)sender).ShowPrintPreviewDialog();
            }

            else
            {
                ((WebBrowser)sender).Dispose();
            }
        }
}

此方法将向您显示printing dialog 以及预览。如果您不想预览,请改用这个 -

private class yourClassName
{
        WebBrowser webBrowserForPrinting;
        public void YourForm_Load()
        {
             webBrowserForPrinting = new webBrowserForPrinting();
             // Set the Url property to load the document.
             webBrowserForPrinting.Url = new Uri("Your HTML File Directory");
             webBrowserForPrinting.DocumentCompleted += WebBrowserForPrinting_DocumentCompleted;
        }

        int i = 0;
        private void WebBrowserForPrinting_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            i++;
            if (i == 1)
            {
                ((WebBrowser)sender).ShowPrintDialog();
            }

            else
            {
                ((WebBrowser)sender).Dispose();
            }
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多