check if a file is a valid pdf


In what code would I use to check if a certain file is a pdf. I want to add this check to the following code

    Protected Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
            Dim oSelectedFile As String
            If Me.ListBox1.SelectedItem.Text <> "" Then
                oSelectedFile = Me.ListBox1.SelectedItem.Text
                Exit Sub
            End If
            'Dim filePath As String = "~/Uploads/" & oSelectedFile
            'Response.ContentType = "application/pdf"

            'Dim FilePath As String = Server.MapPath("piyushsir.pdf")
            Dim FilePath As String = Server.MapPath("~/Uploads/" & oSelectedFile)
            Dim User As New System.Net.WebClient()
            Dim FileBuffer As [Byte]() = User.DownloadData(FilePath)
            If FileBuffer IsNot Nothing Then
                Response.ContentType = "application/pdf"
                Response.AddHeader("content-length", FileBuffer.Length.ToString())
            End If

        Catch ex As Exception
            Me.lblErrorDeleting.Text = ex.Message & " hr556"
        End Try
    End Sub

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NorieAnalyst Assistant Commented:

Do you mean check if the file selected from the listbox is a pdf?

If oSelectedFile.Split(".")(oSelectedFile.split(".").Length - 1) <> "pdf" Then
    ' selected file not a pdf
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rikin ShahMicrosoft Dynamics CRM ConsultantCommented:

Check below code + link-

Private Sub CheckPdf()
    Dim myfileName As String = Server.MapPath("~/Uploads/" & oSelectedFile)

        Dim reader As PdfReader = New PdfReader(myfileName)
    Catch ipe As InvalidPDFException
    End Try
End Sub

Open in new window

If you want to get more detail about this code than check this below link.
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Chinmay PatelChief Technology NinjaCommented:
Hi Murray,

I am not sure how this happened but I put in 3 possible ways to solve this. Somehow that comment never made it.
1. Check file extension and see if it is pdf or not. - Easiest but not reliable.
2. Read header bytes and figure out if the file is a PDF or an image - might not be as accurate as other methods.
3. Check if it is pdf using any PDF library and if it is not, check if it is a valid image and proceed accordingly - Hardest but most accurate.

Hope this helps.

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks. I still don't know how to best view the image. Should I use an image control and just set the Image ImageUrl to this or is there a better way?
Chinmay PatelChief Technology NinjaCommented:
It depends. If you just want to show an image, just use <img> tag and set its source. If you have to do it from the server side code and need to have control
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.