【问题标题】:C# Printing html file as web pageC#将html文件打印为网页
【发布时间】:2017-08-28 11:34:00
【问题描述】:

我正在尝试生成一些 html 文件并将其发送到打印机。 为了打印 html 页面,我在代码中使用了CMD 命令print /D:PrinterName FilePath 作为进程。

当我启动我的程序时,打印机打印的是 html 代码,而不是我预期的网页。 我明白为什么会发生这种情况,但是如何将 html 文件打印为具有网页样式的网页而不是 html 代码?

我之所以选择将数据生成为 html 文件是因为我可以在样式方面非常灵活,我可以控制元素样式创建表格等。所以像“将数据生成为 txt”这样的解决方案不会乐于助人。

非常感谢。

【问题讨论】:

    标签: c# html printing cmd


    【解决方案1】:

    如果您将网页保存在某处,则可以将路径作为输入提供给 webBrowser.Print() 方法。

    查看以下链接了解更多详情:

    https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.print.aspx

    【讨论】:

      【解决方案2】:

      我找到了这个解决方案from here

      WebBrowser myWebBrowser = new WebBrowser();
      private void Form1_Load(object sender, EventArgs e)
      {
          myWebBrowser.DocumentCompleted += myWebBrowser_DocumentCompleted;
          myWebBrowser.DocumentText = System.IO.File.ReadAllText(@"C:\a.htm");
      }
      
      private void myWebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
      {
          myWebBrowser.Print();
      }
      

      【讨论】:

      • 可以选择打印机吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-28
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      • 2021-04-17
      相关资源
      最近更新 更多