Reputation: 21
I have a menustrip that has a tab I have created to be labeled "Contents". I want to be able to click on the Content tab and a window pop up with a pdf file listing the contents. I have the pdf file that lists the contents saved to my desktop. Is there a way to add this pdf file to visual studio so that when I click on the Content tab, the pdf file will be opened?
I do not want to have to click another button to search my computer for the file such as using OpenFileDialog. I just want to click the Contents tab and have it open a window with the pdf file.
Upvotes: 2
Views: 5481
Reputation: 1886
There are multiple ways of doing that.
1) One way would be to launch a process from your app that will open the default registered viewer of PDF files (such as Adobe Reader) on your PC and pass the full path to the PDF file as a parameter:
Here you can find out ho to determine the path to the default handler application by file extension (".pdf" in your case):
http://www.pinvoke.net/default.aspx/shlwapi/AssocQueryString.html
string execPath = GetAssociation(".pdf");
Once you know the path to the executable, you can launch it with a path to your PDF file as a parameter:
using System.Diagnostics;
...
// Start new process
Process.Start(execPath, "C:\\myfile.pdf").WaitForExit(0);
2) Another way would be to create a Windows form in your app and add web browser control to it. The web browser control can then be programmatically "navigated to" your specific PDF file. That is assuming that your Internet Explorer can display PDF files already by using something like Adobe Reader within its window, i.e. as an inline attachment:
Add a reference from your project to Microsoft Internet Controls 1.1
(Right-click on References > Add reference... > COM).
In your form code (here panePdfViewer
is a placeholder System.Windows.Forms.Panel
control):
private AxSHDocVw.AxWebBrowser axWebBrowser;
...
private void InitializeWebControl()
{
this.SuspendLayout();
this.axWebBrowser = new AxSHDocVw.AxWebBrowser();
((ISupportInitialize)(this.axWebBrowser)).BeginInit();
this.axWebBrowser.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
| AnchorStyles.Left)
| AnchorStyles.Right)));
this.axWebBrowser.Enabled = true;
this.axWebBrowser.Location = this.panelPdfViewer.Location;
this.axWebBrowser.Size = this.panelPdfViewer.Size;
((ISupportInitialize)(this.axWebBrowser)).EndInit();
this.axWebBrowser.Visible = false;
this.Controls.Add(this.axWebBrowser);
this.ResumeLayout(false);
}
and then:
// Clear browser
object blank = "about:blank";
this.axWebBrowser.Navigate2(ref blank);
// Display file
object loc = "file:///" + System.IO.Path.GetFullPath(fileName).Replace('\\', '/');
object null_obj_str = null;
object null_obj = null;
this.axWebBrowser.Navigate2(ref loc, ref null_obj, ref null_obj, ref null_obj_str, ref null_obj_str);
3) A third way is to use a third party control library that can display PDF files.
Upvotes: 2