Leigh
Leigh

Reputation: 1533

Print HTML Files from .NET 2.0 App

What's the best method for printing a directory of HTML files in landscape orientation? I don't mind showing a print dialog or not. I've tried several solutions (exhausting Google & StackOverflow) which either print the HTML as a string, or can't print in landscape.

I'm using a .NET 2.0 Win Forms project to create HTML reports. Now I need to send them to the printer spool.

Thanks

Upvotes: 0

Views: 1425

Answers (1)

HatSoft
HatSoft

Reputation: 11201

Update :

After understanding the requirement correctly following can be achieved

//The example is using WebBrowser Control Version 4.0.0.0  .NET Component
//MSDN : http://msdn.microsoft.com/en-us/library/2te2y1x6(v=vs.80).aspx

//Example 1 : You can print html string using the Web Browser Control

    string htmlString = "<html><head><title>Printing from Win forms - Web Browser Control</title></head><body><h1>Hello World....</h1></body></html>";
    webBrowser1.DocumentText = htmlString;

//Example 2 : Print file or URL using the Web Browser Control

    webBrowser1.Url = new Uri("http://www.stackoverflow.com/faq");

//Call Print function or Print Dialog

    webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(PrintFile);

    private void PrintFile(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        //You can setup page e.g. Orientation to Landscape and choose one of the Print options below
        (WebBrowser)sender).ShowPageSetupDialog();

        // Print the document now that it is fully loaded.
        ((WebBrowser)sender).Print();

        //OR
        (WebBrowser)sender).ShowPrintDialog();

        //OR Even better setup print options and then Print
        (WebBrowser)sender).ShowPrintPreviewDialog();

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

Upvotes: 1

Related Questions