【问题标题】:Print HTML Files from .NET 2.0 App从 .NET 2.0 应用程序打印 HTML 文件
【发布时间】:2012-06-30 22:20:53
【问题描述】:

横向打印 HTML 文件目录的最佳方法是什么?我不介意是否显示打印对话框。我尝试了几种解决方案(用尽 Google 和 StackOverflow),它们要么将 HTML 打印为字符串,要么无法横向打印。

我正在使用 .NET 2.0 Win Forms 项目来创建 HTML 报告。现在我需要将它们发送到打印机假脱机。

谢谢

【问题讨论】:

  • @reuben - 我已经尝试过那个示例项目,如何以横向打印?
  • 查看 OLECMDID (msdn.microsoft.com/en-us/library/ms691264(v=vs.85).aspx) 的 MSDN 文档;有一个显示“打印预览”的选项,可让您(通过 UI)选择横向...
  • 然后我必须为目录中的每个 HTML 报告显示 UI?
  • 请更清楚您的要求。您的原始请求表明您可以显示打印对话框。

标签: c# .net html printing


【解决方案1】:

更新:

正确理解需求后即可实现

//The example is using WebBrowser Control Version 4.0.0.0  .NET Component
//MSDN : http://msdn.microsoft.com/en-us/library/2te2y1x6(v=vs.80).aspx

//Example 1 : You can print html string using the Web Browser Control

    string htmlString = "<html><head><title>Printing from Win forms - Web Browser Control</title></head><body><h1>Hello World....</h1></body></html>";
    webBrowser1.DocumentText = htmlString;

//Example 2 : Print file or URL using the Web Browser Control

    webBrowser1.Url = new Uri("http://www.stackoverflow.com/faq");

//Call Print function or Print Dialog

    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintFile);

    private void PrintFile(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        //You can setup page e.g. Orientation to Landscape and choose one of the Print options below
        (WebBrowser)sender).ShowPageSetupDialog();

        // Print the document now that it is fully loaded.
        ((WebBrowser)sender).Print();

        //OR
        (WebBrowser)sender).ShowPrintDialog();

        //OR Even better setup print options and then Print
        (WebBrowser)sender).ShowPrintPreviewDialog();

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

【讨论】:

  • 此方法将 HTML 打印为字符串,这不是我想要的。以及为什么我在我的问题中明确说明了这一点。
  • 你也说“我不介意显示打印对话框。”在你上面的问题中,基于我给你的解决方案
  • 是的,我不介意对话框,但我的问题背后的想法是打印 HTML,而不是文本字符串...
  • 所以如果我理解正确,您是否想要渲染 html 并打印它,例如我打印当前的 SO 页面,它将打印呈现的 html 输出而不是该页面的 HTML 字符串,对吗?
  • 正确。但是我希望打印的文件是由我的程序生成并存储在本地的。哦,他们必须横向打印。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多