Skip to main content
Participating Frequently
August 19, 2008
Question

C#: Display a pdf in WindowForm

  • August 19, 2008
  • 18 replies
  • 30649 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

Participating Frequently
August 19, 2008
Okay thank you, that helps.
There is no possibility to get the current pagenumber? If not, I can use the BrowserComponent, because that's what I want. 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.
But this isn't possible in this or any other way?

Thank you so much. I really read the documentation and I really try to understand, but I am programming since about 2 months, so it's hard to understand what the handle is doing and which parameter must be set to which value.

But with the code in my last post, no pdf is displayed. I am opening a new WindowForm and doing this code in the constructor.

And the AxAcroPDFLib is also a dll which I can refer to? I always read, that I have to include the Acrobat Type Library. Perhaps I am reading the wrong documents of the docu. ;)

Many thanks,
Manuel
Patrick_Leckey
Participating Frequently
August 19, 2008
> I defined the constants

What constants? The ones used in the example should be imported when you include the Acrobat namespace.

> this.Handle.ToInt32()

This is the window handle of the component you are placing the PDF into. You should read over the entire project, and the documentation included with the SDK tells you what the parameters for the functions are.

> CAcroAVDoc avDoc = new AcroAVDocClass();

This class is not available if you only have Reader. Again I suggest you read the documentation carefully so that you know what is available to Reader-only users. The only class available to people with only Reader installed is AxAcroPDFLib.AxAcroPDF.

> But to read the pdf and set a pagenumber/read the actual pagenumber is possible if the client has an installed Acrobat Reader?

With Reader only, you can open a PDF and set the current page. However there is no method to get the current page number.

> Is there a method to test if the reader is installed?

You can check any number of ways, best is probably to check for the correct keys existing in the registry.
Participating Frequently
August 19, 2008
Okay thank you. I compiled the VB example and that's just what I want! But I never used VB, so it's very hard to understand for me. Some functions aren't available in C# and now I am, where I was at the beginning:

CAcroAVDoc avDoc = new AcroAVDocClass();
avDoc.OpenInWindowEx("C:\\test.pdf", this.Handle.ToInt32(), AV_DOC_VIEW, 1, 0, PDUseBookmarks, AVZoomFitWidth, 0, 0, 0);

I defined the constants and try to read the pdf-File, but I really don't know how to say: Use this form and place the pdf in this form at... or use this panel for displaying the selected pdf.

Where can I find this in the example? I am searching in ActiveViewVB => FileOpen.vb. Or am I at the wrong place for loading the pdf? I am really sorry :-/

But to read the pdf and set a pagenumber/read the actual pagenumber is possible if the client has an installed Acrobat Reader? Is there a method to test if the reader is installed?

Many thanks to you!!!
Manuel
Patrick_Leckey
Participating Frequently
August 19, 2008
> and don't have to write a complete Acrobat Reader on my own

Heh, yes, that is correct.

However, the user will require Acrobat or Reader to be installed on their machine to use your application. The control is not redistributable on its own. And many of the advanced functions will only work with Acrobat, not Reader, so you will want to be careful if your intended audience may only have Reader installed.
Participating Frequently
August 19, 2008
Okay, then I am right to use these examples and have the control to set a page, read the actual pagenumber and don't have to write a complete Acrobat Reader on my own, right? Then i will see if these examples can help me. Thank you for your help so far!
Patrick_Leckey
Participating Frequently
August 19, 2008
There are ActiveView and StaticView examples in VB.NET as well. Should give you an idea of how to proceed in C#, since they are both .NET languages.
Participating Frequently
August 19, 2008
Thank you for your reply!

Yes, I have downloaded the SDK and I found the C++ example for a staticView. But I am not sure how to use this in C# and if this is a solution for my problem. I couldn't compile/test it, so that I don't know if what i described is possible in an "easy" way with the SDK.

Thank you,
Manuel
Participating Frequently
August 19, 2008
Have you downloaded the Acrobat SDK? have you looked at the samples provided? There are both staticView and activeView samples - as well as demonstrating how to use Reader as well.