简体   繁体   中英

how to print .htm files in c#?

I can't seem to find a good way to print .htm files in c# using .net 4.0, visual studio 2010 and windows forms . When i tried to print it directly, it printed the raw html data instead of printing the "page" itself.

The only way i know to print it, is to use a WebBrowser control. When i print the document, it doesn't print colors and the page isn't printed correctly. For example, the edges are not drawn and so on.

Code for Web Browser :

public void Print()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(Core.textLog);
}

private void PrintDocument(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    ((WebBrowser)sender).ShowPrintDialog();
    //// Print the document now that it is fully loaded.
    //((WebBrowser)sender).Print();

    //// Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

What can i do?

Thank you!

Printing web pages will forever be the bane of your existence. There just isn't a solution out there that prints HTML directly to your printer really, really well. And even if you do find a program that does it well, it's only a matter of time until you try to print a page with some unsupported formatting, in which case you're right back where you started.

What we do is print HTML to a pdf file with a program called wkhtmltopdf . Then we open it in Acrobat (which has excellent printing support) and print from there. I can't say enough good things about wkhtmltopdf. It's command line driven, and its super, super fast. Best of all, its free . It has a companion program called wkhtmltoimage that will print to most popular image formats, too (bmp, jpg, png, etc).

After downloading/installing the program, you can run a quick test by going to your command prompt, navigating to the install folder, and typing:

wkhtmltopdf "http://YouWebAddress.com" "C:/YourSaveLocation.pdf"

It also has a ton of command line switches that give you greater control over the outputs (headers, footers, page numbering, etc etc).

Ok, as i said, problem was that edge are not drawn and neither are the backgrounds.

Here is how i solved it.

Hashtable values = new Hashtable();

values.Add("margin_left", "0.1");
values.Add("margin_right", "0.1");
values.Add("margin_top", "0.1");
values.Add("margin_bottom", "0.1");
values.Add("Print_Background", "yes");

using (RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\PageSetup", true))
{
    if (key == null) return;

    foreach (DictionaryEntry item in values)
    {
        string value = (string)key.GetValue(item.Key.ToString());

        if (value != item.Value.ToString())
        {
            key.SetValue(item.Key.ToString(), item.Value);
        }
    }
}

So before i print, i go to regedit, change the values, and the document gets printed perfectly. Hope this helps other people that have the same problem when printing from webbrowser control in windows forms.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM