在C#中打印格式化的HTML页面

Dem*_*tri 12 html c# printing wpf

我正在寻找一种在c#(主要是wpf)中以横向模式打印格式化的html文件的方法.打印对话框很不错,以便将页面设置设置为横向.我尝试使用Microsoft的html到xaml转换器,它破坏了格式化.我觉得很奇怪,没有方法或抓取文件并直接发送到打印机.

任何想法将不胜感激.

Thu*_*der 14

 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();
        }
Run Code Online (Sandbox Code Playgroud)


小智 9

您可以在C#中访问WebBrowser类的Print()方法来执行此操作.很酷的是,您不需要在应用程序的任何位置实际显示WebBrowser.只需简单地将内容提供给WebBrowser控件,如下所示:

webBrowser1.DocumentContent = openfiledialog.FileName;

然后只需调用该webBrowser1.Print();方法.这是一个MSDN参考:

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

希望这可以帮助.


Ken*_*ett 8

多年来,我们使用.net WebBrowser控件,如其他答案所述,但最近控制变得越来越不稳定.即使重建我们用于在仓库中打印我们的拣货单以运行Windows 10的机器,我们仍然会遇到问题,直到我们重新启动计算机才会打印页面.相同的代码在4年多的时间里为我们提供了良好的服务,但现在看来微软的最新更新使得这种控制比过去更加错误.

另一个主要问题是没有简单的方法可以打印到除Internet Explorer的默认设置之外的打印机,因此如果您希望打印到不同的打印机,那么您很可能对该控件不满意.

很多年前,我们使用C++和QtWebKit库编写了一个打印代码的打印代码.因此,为了解决这些问题,我挖出了打印网页文件的旧C++应用程序,并将其转换为此项目以通过命令行进行打印,并使其能够打印到不同的打印机.

你可以在这里获得它的源代码:

https://github.com/kendallb/PrintHtml

您可以从这里使用MinGW下载为Windows预编译的32位二进制文​​件:

https://github.com/kendallb/PrintHtml/blob/master/deploy/PrintHtml-window-x86.zip?raw=true

代码是完全可移植的,因此如果需要,您可以轻松地将其编译为在macOS或Linux上运行.

命令行很容易使用,用法如下:

Usage: PrintHtml [-test] [-p printer] [-l left] [-t top] [-r right] [-b bottom] <url> [url2]

-test         - Don't print, just show what would have printed.
-p printer    - Printer to print to. Use 'Default' for default printer.
-l left       - Optional left margin for page.
-t top        - Optional top margin for page.
-r right      - Optional right margin for page.
-b bottom     - Optional bottom margin for page.
url           - Defines the list of URLs to print, one after the other.
Run Code Online (Sandbox Code Playgroud)

显然,要从.net应用程序中使用它,您需要在命令行上生成它,但这很容易做到.假设PrintHtml.exe程序位于应用程序或网站的bin目录中,您可以从.net运行它,如下所示:

public bool PrintHtmlPages(
    string printer,
    List<string> urls)
{
    try {
        // Spawn the code to print the packing slips
        var info = new ProcessStartInfo();
        info.Arguments = $"-p \"{printer}\" \"{string.Join("\" \"", urls)}\"";
        var pathToExe = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
        info.FileName = Path.Combine(pathToExe, "PrintHtml.exe");
        using (var p = Process.Start(info)) {
            // Wait until it is finished
            while (!p.HasExited) {
                Application.DoEvents();
                System.Threading.Thread.Sleep(10);
            }

            // Return the exit code
            return p.ExitCode == 0;
        }
    } catch {
        return false;
    }
}
Run Code Online (Sandbox Code Playgroud)

请享用!

  • 为了向你表示感谢,我必须花一天时间才能获得+50声望:D。你救了我的命。 (4认同)

Jam*_*nne 3

在我看来,最好的方法是首先选择 HTML 渲染引擎并寻求它的打印支持。无论语言、操作系统或框架如何,都不存在打印 HTML 文档的“标准”方法。