Skip to main content
Participating Frequently
August 19, 2008
Question

C#: Display a pdf in WindowForm

  • August 19, 2008
  • 18 replies
  • 30547 views
Hello!

I am reading the documentation and this forum for a few days, but my problem isn't really solved.
I want to display a pdf document in a WindowForm in C#.

CAcroPDDoc pdDoc = new AcroPDDocClass();
pdDoc.Open("C:\\test.pdf");

CAcroPDPage pdPage;
pdPage = (CAcroPDPage)pdDoc.AcquirePage(0);
AcroRect rect = new AcroRect();
rect.Left = 0;
rect.Top = 0;
rect.right = 1000;
rect.bottom = 1000;
pdPage.DrawEx(0, 0, rect, 0, 0, 100);

With this, I am able to display or render the first page of a document. It's just rendered, nothing more. I read, that I can implement a piece of the acrobat reader, so that I don't have to implement everything on my own.

I think this is something like this:

CAcroAVDoc avDoc = new AcroAVDocClass();
avDoc.Open("C:\\test.pdf", "Test");

But I don't know how to display this pdf in my application. What I want to do is to open a new window and display a pdf + some extra functions, that i want to code on my own. I thought about the BrowserComponent of VisualStudio, but there I am only having control of which page to display, but no chance to get the actual displayed page, right?
I want to get the actual page and set the page when the document is opening.

It would be very great if you could help me. I really don't know a good solution for this and my trials all didn't work.

Thank you very much for your reply,
Manuel
This topic has been closed for replies.

18 replies

New Participant
October 24, 2008
I thought I'd already set this to x86, but checking showed any cpu.
I haven't done anything with the control but it doesn't crash, so it's looking hpopeful.
Thanks
Patrick_Leckey
Participating Frequently
October 23, 2008
I'll check to verify, but I believe this is currently not supported for 64-bit. Try compiling as a 32-bit application.
New Participant
October 23, 2008
I have added the Adobe PDF Reader to my VS2005 toolbox, I have dragged the icon onto my windows form, when I compile the program I get no errors, but when I activate the form with the reader on it, I get the following: Class not recognised (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
I am developing on windows XP 64bit, is the message because I'm using a 64 bit OS, or have I missed something?
New Participant
September 16, 2008
I'm having this problem where the focus is on my ListView control but as soon as the pdf is loaded in code my ListView loses focus to the PDF Browser Controls. I've tried to look for something like an onLoadCompleted event in AxAcroPDF but without luck in order to set the focus back to my ListView. Has anyone experienced this problem? Please help!!

axAcroPDF1.LoadFile(C:\\test.pdf);
// if i set the focus like this in code it only works in debugging mode
listView1.Focus();
listView1.Items[index].Focused = true;
listView1.Select();

Thank you,
Bernard
New Participant
June 8, 2009

(bnguyen) wrote:

I'm having this problem where the focus is on my ListView control but as soon as the pdf is loaded in code my ListView loses focus to the PDF Browser Controls. I've tried to look for something like an onLoadCompleted event in AxAcroPDF but without luck in order to set the focus back to my ListView. Has anyone experienced this problem? Please help!!

axAcroPDF1.LoadFile(C:\\test.pdf);
// if i set the focus like this in code it only works in debugging mode
listView1.Focus();
listView1.Items[index].Focused = true;
listView1.Select();

Thank you,
Bernard

Bernard,

I have this same issue - have you been able to find a solution?  Or can anyone recommend a solution? I would appreciate some help, thanks!

New Participant
June 29, 2009

ekb0211 wrote:

(bnguyen) wrote:

I'm having this problem where the focus is on my ListView control but as soon as the pdf is loaded in code my ListView loses focus to the PDF Browser Controls. I've tried to look for something like an onLoadCompleted event in AxAcroPDF but without luck in order to set the focus back to my ListView. Has anyone experienced this problem? Please help!!

axAcroPDF1.LoadFile(C:\\test.pdf);
// if i set the focus like this in code it only works in debugging mode
listView1.Focus();
listView1.Items[index].Focused = true;
listView1.Select();

Thank you,
Bernard

Bernard,

I have this same issue - have you been able to find a solution?  Or can anyone recommend a solution? I would appreciate some help, thanks!

I also have this issue. Anyone managed to figure this out yet?

Thanks, Nate

Participating Frequently
August 20, 2008
I made a screenshot of the reader, so that you know which button I mean:
http://www.der-webdesigner.net/pdf-navi.jpg

This button is also available for users who have only installed the reader. Can I deactivate this button? Or is there another way? If this button is deactivated, I am happy with the solution :)

Nice greetings,
Manuel
Patrick_Leckey
Participating Frequently
August 20, 2008
> But how am I able to hide or deactivate the "save"-button? I don't want, that the user can save this document

If the user only has Reader installed, they can't save anyways. With Acrobat, this can't be disabled.

Please note that Acrobat 8.1 was the first version compatible with Windows Vista. If you have a version prior to 8.1 (including 8.0), it will not work in Vista.
Participating Frequently
August 20, 2008
Thanks for your answers!
My problem is, what malkyt explains. I want to add the Reader to my Toolbox Items. But in my COM Components tab, there are only 2 Items, but no Adobe PDF Reader.

Now I copied the AxInterop.AcroPDFLib.dll and the Interop.AcroPDFLib.dll from an example in the SDK. I found some code to display a pdf (http://www.sciencecode.com/text.htm#30):

try
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PdfForm));
AxAcroPDFLib.AxAcroPDF axAcroPdf = new AxAcroPDFLib.AxAcroPDF();

((System.ComponentModel.ISupportInitialize)(axAcroPdf)).BeginInit();

axAcroPdf.Location = new Point(50, 50);
axAcroPdf.Size = new Size(500, 500);
axAcroPdf.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axAcroPdf.OcxState")));

this.Controls.Add(axAcroPdf);
((System.ComponentModel.ISupportInitialize)(axAcroPdf)).EndInit();

axAcroPdf.LoadFile("C:\\test.pdf");
axAcroPdf.Show();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

But if I do this, I get something like this:
"Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))"

What am I doing wrong? How can I get this COM-Component or can I just use these 2 libs from the examples?

Many many thanks!!!

EDIT: Okay, on my Laptop (WinXP) this works. Vista64Bit doesn't show these components. Now the pdf is displayed and I am very happy. But how am I able to hide or deactivate the "save"-button? I don't want, that the user can save this document.
New Participant
August 20, 2008
In Visual Studio 2005 you need to add the control to the toolbox.

Tool->Choose Toolbox Items.

This will display a dialog, select the COM Components tab.
Then select - > Adobe PDF Reader

Then just drag and drop it like any other control.

HTH

Malky
Participating Frequently
August 20, 2008
Okay, I am giving it up. I read every document of the documentation and I didn't find how to use the "AxAcroPDFLib". I don't find any information how to integrate this in Visual Studio. I found another post, that this should be in the COM-Tab of the toolbox, but there it isn't.

The only thing I want to do now: Display a pdf in a WindowForm but without the savebutton of the reader. Am I able to disable this button or to hide it? Or can I hide the complete navbar and write my own with only using the Acrobat Reader, not the fullversion, because I can't ensure, that every client has this version.

Thank you very much for your help. I really begin to desperate... :-/
Manuel
Participating Frequently
August 20, 2008
>Okay, I am giving it up. I read every document of the documentation and I didn't find how to use the "AxAcroPDFLib".

Did you read the Interapplication Communication documents?

> I don't find any information how to integrate this in Visual Studio.

Why would you do that? That doesn't sound correct.

There is a code example in the documents which shows how to associate
the control with an existing control. Do you need help finding it? Or
does it just not help?


Aandi Inston
Patrick_Leckey
Participating Frequently
August 20, 2008
> The user should be able to save some own information to a page of the pdf and therefor i wanted to save the actual pagenumber + the comment of the user in a local database. Then the user can click on this comment and the page with this comment is shown.

This functionality already exists in Acrobat, and Reader as well if the PDF is Reader Enabled.