0

I wont print in pdf file current page on webBrowser control,? its code:

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

File genereted, but its empty....

Andrea Antonangeli
  • 1,242
  • 1
  • 21
  • 32
user3331122
  • 969
  • 1
  • 7
  • 14
  • Hi, could you please edit your post to let us know precisely what it is you're trying to achieve, and why the current code you've provided isn't working? – Clint Jun 15 '17 at 05:29
  • 1
    Did you find out any solution? – kavain Apr 06 '20 at 17:16

1 Answers1

1

Try direct print method of web browser

   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 
    }
Abinash
  • 471
  • 3
  • 13
  • webBrowser1.Print(); - In this case, a dialog box "save file" is showing, but I need to work in batch mode, I specify the file name programmatically – user3331122 Jun 15 '17 at 09:42
  • file name means printer document name??then what is the use of that – Abinash Jun 15 '17 at 11:40
  • Yes. i have 1000 url in db, and i need make their screenshot in pdf format. Therefore, I need automatic saving, without user intervention – user3331122 Jun 15 '17 at 12:38