用于webBrowser控件的“Microsoft Print to PDF”打印机

时间:2017-06-15 05:26:36

标签: c# pdf printing

我不会在webBrowser控件上的pdf文件当前页面打印,?它的代码:

 // generate a file name as the current date/time in unix timestamp format
    string file = (string)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();
 // the directory to store the output.
    string directory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

 // initialize PrintDocument object
    PrintDocument doc = new PrintDocument();

    doc.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
    doc.PrinterSettings.PrinterName = "Microsoft Print to PDF";
 // tell the object this document will print to file
    doc.PrinterSettings.PrintToFile = true;
    doc.PrinterSettings.PrintFileName = Path.Combine(directory, file + ".pdf");
    doc.Print();

     private void pd_PrintPage(object o, PrintPageEventArgs e)
     {   
       webBrowser1.Navigate("https://stackoverflow.com/questions/40812996/
       programmatically-provide-a-filepath-as-input-file-for-microsoft-print-to-pdf-p");
     }

文件生成,但它是空的......

1 个答案:

答案 0 :(得分:1)

尝试使用网络浏览器的直接打印方法

   private void button1_Click(object sender, EventArgs e)
    {
        //webBrowser1.ShowPrintDialog(); //Printer Dialog will appear
        //webBrowser1.ShowPrintPreviewDialog(); //preview of the document
        webBrowser1.Print(); //directly print with current printer settings 
    }