Printing PDF Using WebBrowser Control in .Net 2.0
Okay, after almost a year and an offshore assignment, I finally had a chance to update this blog.
I was developing a .Net 2.0 C# windows application wherein I basically have to print PDF files from a Windows application. I tried to use the Adobe Acrobat Reader ActiveX object embedded in my form to preview and print PDF documents. It worked fine; I just have to import the COM component and Visual Studio will do most of the Interop work for me. I then have to locate the file and load it to the AxAcroPDF control:
axAcroPDF1.LoadFile(pdfFileName);
I'd then delete the PDF file immediately afterwards to somehow obscure it from the user. The ActiveX object seems to save it in memory such that I can make a call like:
axAcroPDF1.Print();
...and all went smoothly. But when I ran it on another machine with the most recent version of Acrobat (8.1.2), I got an E_NOINTERFACE exception. After much research, I then decided to use a WebBrowser .Net 2.0 control instead. Thus, my codes looked like so:
webBrowser1.Navigate(pdfFileName);
...assuming of course that the user has a PDF reader installed and integrated into his web browser. This is a much easier alternative than to using unmanaged code. However, it does not go away without problems. When I try to call:
webBrowser1.Print();
I get nothing. I can't seem to force the browser to print its PDF contents. To keep the long story short, I had to tinker with the underlying ActiveX object of the WebBrowser control. First, I needed a reference to shdocvw.dll (windows system32 directory). Then, I did something like:
object n = null;
SHDocVw.WebBrowser wb = (SHDocVw.WebBrowser)webBrowser1.ActiveXInstance;
wb.ExecWB(OLECMDID.OLECMDID_PRINT
, OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER
, ref n
, ref n);
It worked! (MSDN says that the two pointers are optional so I just put null in there.)
Labels: acrobat, activex, c#, dotnet, microsoft, pdf, programming, visual studio


3 Comments:
pare,
meron ka ba dyang sample "online pdf viewer"?
-Marlon
http://devpinoy.org/blogs/marl
marlon,
wala pa pre. pero good idea... parang ganito sinasabi mo diba?http://view.samurajdata.se
very awesome blog some how i found you looking for things on our sons birth defect esophageal atresia. I wish you nothing but the best.
Post a Comment
Subscribe to Post Comments [Atom]
<< Home