How to Open a PDF File in VB
Do you need to open a PDF file from a program you're writing in Visual Basic? You can open almost any type of file from within a VB program as long as you know the correct code sequence. One way of opening a file is by coding the open process to a button that the user can click. There are many different ways to open a PDF file, but the basic code will work in many of those cases.
Instructions
-
From Default Viewer
-
1
Create a Private Sub button event to handle the PDF file by using the following code example:
"Private Sub ButtonOpen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click
End Sub"
-
2
Use the Process.Start method to open the PDF with the default PDF viewer. For example, use the following code to open "book.pdf" located in the Documents folder:
"Process.Start("C:\Documents\book.pdf")", but use a valid location for your PDF file. Place this code before the "End Sub" section.
-
-
3
Compile the code and test the results. The button click event opens the PDF file you specified.
From Default Browser
-
4
Create a Private Sub button event to handle the PDF file by using the following code example:
"Private Sub ButtonOpen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click
End Sub"
-
5
Use the WebBrowser.Navigate method to open the PDF with the default Web browser. For example, type the following code before the "End Sub" section:
"WebBrowser.Navigate("C:\Documents\book.pdf")", but use a valid location for your PDF file.
-
6
Compile the code and test the results. The button click event opens the PDF file you specified.
With a Specific Program
-
7
Create a Private Sub button event to handle Adobe Reader by using the following code example:
"Private Sub ButtonOpen(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOpen.Click
End Sub"
-
8
Add the "Adobe PDF Reader" control from your toolbox to the form you are using. Find the AdobeReader.src property and change it to "AdobeReader.src = "C:\Documents\book.pdf"" but use a valid location for your PDF file.
-
9
Compile the code and test the results. The button click event opens the PDF file you specified within the application window.
-
1
Tips & Warnings
Before starting this workflow, it is important to have a basic understanding of VB.
You'll need access to a VB compiler to make this code compile and run.
References
- Photo Credit Jupiterimages/Photos.com/Getty Images