D
D
Dmitry Filandor2017-06-15 08:33:31
Programming
Dmitry Filandor, 2017-06-15 08:33:31

How to print current page with webBrowser control to pdf?

Hello! I want to print the current page from webBrowser to pdf
In Windows 10 there is “Microsoft Print to PDF”
Standard examples, when graphics are sent to the printer, work .... but how to send the current page of the webBrowser control to print? And without dialog boxes, since I need to programmatically save different pages.

private void button4_Click(object sender, EventArgs e)
        {


            // 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();

            //webBrowser1.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");
        }

saves empty pdf document...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
Zakharov Alexander, 2017-06-15
@AlexZaharow

I didn't try it myself, but I found a description of a similar problem:
alexrazon.blogspot.ru/2008/04/printing-pdf-using-w...
www.vbforums.com/showthread.php?760525-RESOLVED-Ex...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question