0

I want to save a html content in pdf from a webbrowser control in win-form

this.webBrowser1.DocumentText = htmltext;

i have used this to render html text in webbrowser now using below to print that html content into pdf

webBrowser1.Print();

but when i use this it will open a print dialog box, but i don't want to show it and wants to do give printer and file name from code side

  • 2
    Why do you want to _print_ this in a PDF ? Why not use a third party library like iText7 to create a PDF and write your text to that PDF ? – MarleneHE Sep 16 '20 at 14:50
  • 3
    Does this answer your question? [How can I send a file document to the printer and have it print?](https://stackoverflow.com/questions/6103705/how-can-i-send-a-file-document-to-the-printer-and-have-it-print) – Steve Norwood Sep 16 '20 at 15:07
  • I have tried this stuff but not getting appropriate results that i want – Rishabh Shrivastava Sep 16 '20 at 17:14

1 Answers1

1

I would agree with @MarleneHE that you should be using a library to convert HTML to PDF for this, have a look here for discussion on this.

If you are insistent on using the PDF printer, have a look at this post which uses ProcessWindowStyle.Hidden to get around the printer dialog:

private void SendToPrinter()
{
   ProcessStartInfo info = new ProcessStartInfo();
   info.Verb = "print";
   info.FileName = @"c:\output.pdf";
   info.CreateNoWindow = true;
   info.WindowStyle = ProcessWindowStyle.Hidden;

   Process p = new Process();
   p.StartInfo = info;
   p.Start();

   p.WaitForInputIdle();
   System.Threading.Thread.Sleep(3000);
   if (false == p.CloseMainWindow())
      p.Kill();
}
Steve Norwood
  • 363
  • 5
  • 19