I have developped a Web application to display html documents (mixed text and graphics). The application includes some print functionaly (Print / Prewview). The MS Webbrowser Print Template mechanism are used for formatting (with custom header/footer) the printed pages. The Print functionality in the Web app will call API methods provided by a client component (.NET COM DLL) on the client side to print the html document displayed in IE 8.
In the client component codes (implemented with C# and using Web browser control from .NET Framework 3.5), I have implemented a function to print a html document (mixed text and graphics)
by calling Web browser control ‘Navigate’ method to load html document (invisible) and relying on Web browser control event to indicate when the html document loading process is complete before calling Web browser control ExecWB method to initiate the printing process .
The codes are as follows :
// Create a WebBrowser instance.
System.Windows.Forms.WebBrowser webBrowserForPrinting = new System.Windows.Forms.WebBrowser();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
string nullStr = "";
object nullObjStr = nullStr;
byte[] nullArrayObj = null;
byte[] postData = encoding.GetBytes(keyList);
string addHeaders = "Content-Type: application/x-www-form-urlencoded" + "\n" + "\r";
object tplObjStr = templateURL;
// Load html web page
if (keyList == null || keyList.Length == 0)
webBrowserForPrinting.Navigate(target, nullStr, nullArrayObj, nullStr);
else
webBrowserForPrinting.Navigate(target, nullStr, postData, addHeaders);
// Waiting until the web page download process is completed
Application.UseWaitCursor = true;
while (webBrowserForPrinting.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
IWebBrowser2 browser = (IWebBrowser2)webBrowserForPrinting.ActiveXInstance;
// Start printing ...
switch (printOption)
{
case 1:
browser.ExecWB(OLECMDID.OLECMDID_PRINTPREVIEW, OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref tplObjStr, ref nullObjStr);
break;
case 2:
browser.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER, ref tplObjStr, ref nullObjStr);
break;
case 3:
browser.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, ref tplObjStr, ref nullObjStr);
break;
default :
browser.ExecWB(OLECMDID.OLECMDID_PRINT, OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER, ref tplObjStr, ref nullObjStr);
break;
}
// Terminate web browser control instance
webBrowserForPrinting.Dispose();I have experienced some unexpected results when printing a simple html document. The correct result should be two pages :one text page and one graphic
page. But sometimes I've got one text page and two blank pages.
I have investigated the IE temp files (generated by Web browser when html doc is loaded completely) for two cases :
(1) No missing graphic (an emf file is generated by Web browser to store the graphic bitmap image) : The following parameter is added to <OBJECT> tag in IE temp html file : PARAM NAME="_Snapshot_EMF" VALUE=".......GN1FX1Q2.emf">
(2) Missing graphic (blank page), no emf file is generated : The following parameter is added to <OBJECT> tag in IE temp html file <PARAM NAME="_Snapshot_EMF" VALUE="_ZERO">
For this type of the issue, I assume there might be caused by some network latency/ timing issue that affects IE Web browser behavior because Web browser need time to get graphic data from the Web application server and convert to bitmap.
However, even when I add some time delay (5 sec) in the codes after the html document is loaded completely to Web browser as follows :
Application.UseWaitCursor = true;
while (webBrowserForPrinting.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
Thread.Sleep(5000);
................................. The issue still cannot be resolved.
Have anyone experienced similar issues?
Is this .NET Web browser control issue?
How do we resolve this issue?
Any ideas / comments / suggestions are greatly appreciated.
Thanks,
Kevin Ton