tags:

views:

257

answers:

1

Due to circumstances beyond my control, I'm replacing all of our Crystal Reports with home-built XML reports, which are working beautifully. For most of the reports that pop up a Crystal Reports viewer, the following code opens them nicely in IE, transforming it to HTML via an XSLT stylesheet.

ProcessStartInfo psi = new ProcessStartInfo(reportFilename)
{
   UseShellExecute = true
};

using (Process p = new Process {StartInfo = psi})
{
    p.Start();
}

The problem is that some of the reports just print directly to a printer, never showing the report to the user, which works fine in CR. I can't figure out how to do this using the code above.

I'd prefer not to specifically launch an IE process if possible, but I am guaranteed that they're running Windows, so that's not a hard requirement. Also, will printing directly in this manner transform the XML into HTML via the XSL and print that, or just print the actual XML text?

EDIT: I've already tried adding:

Verb = "Print"

to the ProcessStartInfo object, but that winds up with an exception being thrown saying:

"No application is associated with the specified file for this operation"

EDIT AGAIN: Specifying IE as the exe to launch loaded the XML again just fine, but does not offer a "print" action. Adding "window.print()" in a JavaScript block works, but requires manually clicking the print button after allowing the script to run, because IE blocks it.

EDIT THE THIRD: My boss told me not to worry about it, that they can print from IE. I still want to figure this out. I've tried the command line "print.exe", but that only prints the raw XML to the printer. Tried XslCompiledTransform with a PrintDocument, but that's not what I'm looking for, either.

+1  A: 

Figured it out, finally. I just created an invisible WebBrowser control that does the IE rendering, and on DocumentCompleted, call its Print() method. Worked like a charm using the default printer settings.

private static void PrintReport(string reportFilename)
{
    WebBrowser browser = new WebBrowser();

    browser.DocumentCompleted += browser_DocumentCompleted;

    browser.Navigate(reportFilename);
}

private static void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    WebBrowser browser = sender as WebBrowser;

    if (null == browser)
    {
        return;
    }

    browser.Print();

    browser.Dispose();
}
Chris Doggett