Click here to Skip to main content
15,559,667 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
create a project of type "Windows Forms"

And you added the tool "WebBrowser"

And I want to save the open web site inside the tool "WebBrowser" Via a printer "Microsoft Print to PDF"

Provided that the process is silent

And the code displayed is actually saving silently

But how do I give it a command to print the displayed website with a tool "WebBrowser"

C#
// generate a file name as the current date/time in unix timestamp format
string file = (DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds.ToString();

// the directory to store the output.
string directory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);




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

    PrinterSettings = new PrinterSettings()
    {
        // set the printer to 'Microsoft Print to PDF'
        PrinterName = "Microsoft Print to PDF",

        // tell the object this document will print to file
        PrintToFile = true,

        // set the filename to whatever you like (full path)
        PrintFileName = Path.Combine(directory, file + ".pdf"),


    }
};

doc.Print();


What I have tried:

How to Print Silent in WebBrowser
Posted
Updated 6-Feb-21 10:16am
Comments
Gerry Schmitz 6-Feb-21 15:03pm    
Put on head phones.

Have you tried the official documentation?
How to: Print with a WebBrowser Control - Windows Forms .NET Framework | Microsoft Docs[^]
All you have to do is tell the PrintDocument instance which printer driver to use ...
 
Share this answer
 
Comments
Realy8 7-Feb-21 13:15pm    
I am a beginner with programming and couldn't use the code

I don't know where the file is saved

I don't know where it will be saved
 
Share this answer
 
Comments
Realy8 7-Feb-21 13:20pm    
The code works but not silently

Also, there is no code in the code to name the file nor where to save it
RickZeeland 7-Feb-21 13:29pm    
Bummer, usually it is not allowed to save files directly from a browser this is a safety measure that's baked into the system.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900