Quotes .....

There never was, there never will be, a man who is always praised, or a man who is always blamed....

Thursday, August 9, 2012

LoadFile() Method of the object AxAcroPDFLib.AxAcroPDF can't use to load already open PDF files

The Question:

I have made a Windows desktop application(in C#) where I use the AxAcroPDF control to display a pdf-file on a form when user give the path of the pdf and click a button. Pdf-file is located in one network shared file folder where multiple users can access it.
(I use the LoadFile() Method of the object AxAcroPDFLib.AxAcroPDF)

This is working fine. But when the same pdf file which my applications try to open is already opened using Adobe Reader then my application can’t open the same pdf file.

If the PC my application installed has Adobe reader 7 then application throw the error message as "System.ApplicationException Failed Loading PDF Template" and application freeze.

And if the application running PC’s having Adobe Reader X installed then no error will appear but the loading screen appears for ever.

But opening the same pdf simultaneously in two or more PC using my application works perfect.


The Answer:

Solution01 - Not recommended (this sometimes not work)

After desperate search on forums I couldn't find a proper solution as I was expecting to find. But to avoid the program get crash, I put a check before loading the pdf.

bool PDFAvailable = axAcroPDF1.LoadFile(PDFPath.ToString());

If the Boolean variable 'PDFAvailable' return true I'll load the PDF like below. Else a proper message is thrown.
bool PDFAvailable = axAcroPDF1.LoadFile(PDFPath.ToString());                                                if (PDFAvailable == true)                        {                            axAcroPDF1.LoadFile(PDFPath.ToString());                            axAcroPDF1.setShowToolbar(false); //disable pdf toolbar.                            axAcroPDF1.Enabled = true;                                                    }                        else                        {                            MessageBox.Show("Selected PDF Template Is Locked By Another Application.", ""Test Application"", MessageBoxButtons.OK, MessageBoxIcon.Information);                                                    }
Solution02 - Recommended (Work fine for Adobe Acrobat Reader X)

I found a workaround in this
post
Rather than LoadFile() we can use the "src" property of the control.
The drawback is Adobe acrobat Reader 7 doesn't support src property.

Solution03 - Recommended for any version

Copy the pdf file to client PC temp folder and then read :-)