C#: Display a pdf in WindowForm
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
This functionality already exists in Acrobat, and Reader as well if the PDF is Reader Enabled.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
(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!
Copy link to clipboard
Copied
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,
BernardBernard,
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
Copy link to clipboard
Copied
Right now the only way I get it to work is by setting the Activate and Focus of the non-Acrobat controls with a timer event; but this isn't reliable depending on changes in performance.
Let me know if you figure this problem out.
Copy link to clipboard
Copied
I am developing on windows XP 64bit, is the message because I'm using a 64 bit OS, or have I missed something?
Copy link to clipboard
Copied
Copy link to clipboard
Copied
I haven't done anything with the control but it doesn't crash, so it's looking hpopeful.
Thanks

