You are on page 1of 3

Public Function GetFileDetails(strFilePath As String) As String

Dim strFilExtn As String = Path.GetExtension(strFilePath).ToLower


Dim PageCount As Integer = 0
Dim strResult As String = "0,TBD"

Dim arrPaperSize As String = "A0 A1 A2 A3 Letter A4 A5 A6 A7 A8"


Dim arrWidth As String = "2384 1684 1190 842 612 595 420 298 210 148"
Dim arrHeight As String = "3370 2384 1684 1190 792 842 595 420 298 210"
Dim strPaperSize As String = String.Empty
Dim arrSizeToCompare() As String

Dim intWidth As Integer = 0


Dim intHeight As Integer = 0
Dim intPageCount As Integer = 0
Dim i As Integer = 0

Select Case strFilExtn


Case ".pdf"
Using pdfReader As PdfReader = New PdfReader(strFilePath)
intWidth = pdfReader.GetPageSize(1).Width
intHeight = pdfReader.GetCropBox(1).Height

PageCount = pdfReader.NumberOfPages
End Using

Case ".xls", ".xlsx", ".xlsm"


Dim objExcel As Object = Nothing
Dim objWorkbook As Excel.Workbook = Nothing

Try
Dim isFileOpen As Boolean = FileOpenTest(strFilePath)

If isFileOpen = True Then


objExcel = GetObject(strFilePath)

PageCount = objExcel.Worksheets.Count
Else
objExcel = New Excel.Application
objWorkbook = objExcel.Workbooks.Open(strFilePath)
objExcel.DisplayAlerts = False

PageCount = objExcel.Worksheets.Count

objWorkbook.Close(SaveChanges:=False)
objExcel.ScreenUpdating = True
objExcel.Application.Quit()

'Release objects from memory


GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End If

strResult = PageCount.ToString & "," & "TBD"

Return strResult
Catch ex As Exception
If Err.Number = 429 Then MessageBox.Show("Unable to find to
selected document in the specified path/drive.", "Missing Document",
MessageBoxButtons.OK, MessageBoxIcon.Warning)

Return strResult
End Try

'Acquired separately as getting data from word files take time; run
through background worker
Case ".doc", ".docx"
Dim objWordApp As Object = Nothing
Dim objDoc As Object = Nothing
Dim isFileOpen As Boolean = FileOpenTest(strFilePath)

If isFileOpen = True Then


objWordApp = GetObject(strFilePath)
PageCount =
objWordApp.ComputeStatistics(Word.WdStatistic.wdStatisticPages)
intHeight =
objWordApp.ActiveWindow.Panes(1).Pages.Item(1).Height
intWidth = objWordApp.ActiveWindow.Panes(1).Pages.Item(1).Width

Else
objWordApp = CreateObject("Word.Application") 'New
Word.Application
objDoc = objWordApp.Documents.Open(strFilePath)

'Repaginate the document.


objDoc.Repaginate

PageCount =
objDoc.ComputeStatistics(Word.WdStatistic.wdStatisticPages)
intHeight = objDoc.ActiveWindow.Panes(1).Pages.Item(1).Height
intWidth = objDoc.ActiveWindow.Panes(1).Pages.Item(1).Width

objDoc.Close(SaveChanges:=False)
objWordApp.Application.Quit()
End If

Case ".tif", ".tiff"


Try
Dim theTIFF As Image = Image.FromFile(FileListSelectedPath)

PageCount = theTIFF.GetFrameCount(FrameDimension.Page)
theTIFF.Dispose()

'Set data for Main Form


CurrentPage = 1
TotalNumOfSheets = PageCount
Catch ex As Exception
CurrentPage = 0
TotalNumOfSheets = 0
End Try

strResult = PageCount.ToString & "," & "TBD"


Case ".bmp", ".jpg", ".jpeg"
'Set data for Main Form
PageCount = 1
CurrentPage = 1
TotalNumOfSheets = PageCount
strResult = "1,TBD"
Case Else
Return strResult
End Select

'For paper size - pdf / doc only


Select Case strFilExtn
Case ".pdf", ".doc", ".docx"
For x As Integer = 1 To 2
If intWidth > intHeight Then
arrSizeToCompare = arrHeight.Split()
Else
arrSizeToCompare = arrWidth.Split()
End If

For Each wdth As Integer In arrSizeToCompare

If intWidth >= wdth - 6 AndAlso intWidth <= wdth + 6 Then


strPaperSize = arrPaperSize.Split()(i)
Exit For
End If

i += 1
Next

If String.IsNullOrEmpty(strPaperSize) AndAlso x = 1 Then


If x = PageCount Then strPaperSize = "TBD"

strResult = PageCount & "," & strPaperSize


Exit For
Else
If String.IsNullOrEmpty(strPaperSize) Then strPaperSize =
"TBD"

strResult = PageCount.ToString & "," & strPaperSize


Exit For
End If
Next
End Select

Return strResult
End Function

You might also like