שתף באמצעות


VB.NET Opening a PDF file using a file path

Question

Wednesday, May 30, 2018 8:51 PM

I currently have a simple PDF reader that requires the user to press on a button and navigate their way through to find a file.

Using a MYSQL database I am able to store the path of a file and I want to use this path to automatically load the PDF file rather than have the user navigate their way through. The path is located on a textbox called:

pathname.text

I want to use the path in 'pathname.text' to automatically load the file located with a click of a button but i'm not sure how to alter the code below to support this.

 Dim opf As New OpenFileDialog
            opf.Filter = "PDF File | *.pdf"

            If opf.ShowDialog = DialogResult.OK Then
                AxAcroPDF1.src = opf.FileName
            End If

All replies (2)

Wednesday, May 30, 2018 9:14 PM ✅Answered | 1 vote

If you have the full path in the database just use the following:

AxAcroPDF1.LoadFile(pathname.text)

Paul ~~~~ Microsoft MVP (Visual Basic)


Wednesday, May 30, 2018 9:21 PM

You can use an If statement like below or an If statement not to show the OpenFileDialog if the TextBox contains a path.

Below code will place the filename in the OpenFileDialog as well set its initial directory to the path in the TextBox. If nothing is in the TextBox then it will path to the desktop as currently coded.

Option Strict On

Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Location = New Point(CInt((Screen.PrimaryScreen.WorkingArea.Width / 2) - (Me.Width / 2)), CInt((Screen.PrimaryScreen.WorkingArea.Height / 2) - (Me.Height / 2)))
        TextBox1.Text = "C:\Users\John\Desktop\Test Text.Txt"
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Using OFD As New OpenFileDialog
            With OFD
                If TextBox1.Text <> "" Then
                    .FileName = TextBox1.Text
                    .InitialDirectory = IO.Path.GetDirectoryName(TextBox1.Text)
                    MessageBox.Show(OFD.InitialDirectory)
                Else
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                End If
                .Filter = "Text Files (*.Txt)|*.Txt"
                .Title = "Open text files"
                .Multiselect = False
            End With
            If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                TextBox2.Text = My.Computer.FileSystem.ReadAllText(OFD.FileName)
            End If
        End Using
    End Sub

End Class

If statement if TextBox contains text i.e. path and filename.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        If TextBox1.Text <> "" Then
            TextBox2.Text = My.Computer.FileSystem.ReadAllText(TextBox1.Text)
        Else
            Using OFD As New OpenFileDialog
                With OFD
                    .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
                    .Filter = "Text Files (*.Txt)|*.Txt"
                    .Title = "Open text files"
                    .Multiselect = False
                End With
                If OFD.ShowDialog = Windows.Forms.DialogResult.OK Then
                    TextBox2.Text = My.Computer.FileSystem.ReadAllText(OFD.FileName)
                End If
            End Using
        End If
    End Sub

La vida loca