ASP.net VB.net check if a file is a valid pdf

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

In ASP.net 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
        Try
            Dim oSelectedFile As String
            If Me.ListBox1.SelectedItem.Text <> "" Then
                oSelectedFile = Me.ListBox1.SelectedItem.Text
            Else
                Exit Sub
            End If
            'Response.Clear()
            'Dim filePath As String = "~/Uploads/" & oSelectedFile
            'Response.ContentType = "application/pdf"
            'Response.WriteFile(filePath)
            'Response.TransmitFile(filePath)

            '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())
                Response.BinaryWrite(FileBuffer)
            End If

        Catch ex As Exception
            'Response.Write(ex.Message)
            Me.lblErrorDeleting.Text = ex.Message & " hr556"
        End Try
    End Sub

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Analyst Assistant
Commented:
Murray

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

Rikin ShahMicrosoft Dynamics CRM Consultant
Commented:
Hi,

Check below code + link-

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

    Try
        Dim reader As PdfReader = New PdfReader(myfileName)
    Catch ipe As InvalidPDFException
        MessageBox.Show(ipe.Message)
    End Try
End Sub

Open in new window


If you want to get more detail about this code than check this below link.

http://www.eggheadcafe.com/community/aspnet/2/10141925/hoiw-to-validate-pdf-file-using-pdfsharp-libraries.aspx
Murray BrownASP.net/VSTO Developer

Author

Commented:
Thanks
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Chinmay PatelChief Technology Ninja
Distinguished Expert 2018

Commented:
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.

Regards,
Chinmay.
Murray BrownASP.net/VSTO Developer

Author

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 Ninja
Distinguished Expert 2018
Commented:
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 https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.image?view=netframework-4.8

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial