c#webBrowser打印到Pdf

时间:2017-07-26 15:16:47

标签: c# .net printing webbrowser-control

我的默认打印机设置为"细微差别pdf"这会将文件保存为pdf。我有使用webBrowser.Print()的控制台应用程序。此命令旁路打印弹出并显示"另存为"页面:

enter image description here

我想以编程方式输入文件名并保存文件,而不会看到弹出窗口。 我该如何实现这一目标?有什么建议/指针吗?

这是我从中获取的代码:C# : "How to use WebBrowser and print the html to the console. "

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            PrintHelpPage();
            Console.ReadKey();
        }

        public static void PrintHelpPage()
        {
            var th = new Thread(() => {
                var br = new WebBrowser();
                br.DocumentCompleted += PrintDocument;
                br.Navigate("http://google.com");
                Application.Run();
            });
            th.SetApartmentState(ApartmentState.STA);
            th.Start();
        }
    public static void PrintDocument(object sender,
        WebBrowserDocumentCompletedEventArgs e)
    {
        var browser = sender as WebBrowser;
        // Print the document now that it is fully loaded.
        browser.Print();

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

}

0 个答案:

没有答案