2016-10-19 5 views
0

현재 vb.net에서 작업 중입니다. 우리 회사는 서류없이 가고 있으며 종이 절약에 대한 비용 절감 분석을하고 싶습니다. 현재 우리는 모든 PDF 파일을 서버에 저장합니다. 파일 경로는이 "Server> Folder1> Folder2> Folder3> Folder4> PDF 파일과 같습니다." 폴더 1과 폴더 2는 항상 탐색에 사용됩니다. 폴더 3은 부서 목록이고 폴더 4는 각 작업입니다. 각 폴더 4에는 여러 pdf 파일이 있습니다. 간단히 말하면 폴더 1과 폴더 2의 이름은 정적이며 폴더 3과 4는 동적입니다. 폴더 4 이후에있는 모든 PDF 파일을 더 세게 만들기 위해 이름이 다릅니다. pdf가 얼마나 많은 페이지를 열지 않고도 발견 할 수있는 코드가 있지만 파일 경로가 필요합니다. 수천 개의 pdf 파일이 아니라면 수백 개가 있다고 생각해도 프로그램 적으로 모든 파일을 반복하고 파일이 pdf 파일인지 확인한 다음 발견 된 모든 페이지를 합산합니다. 그런 다음이 번호를 사용하여 종이없는 비용 절감을 계산할 수 있습니다.여러 위치에 저장된 여러 PDF 파일의 총 페이지 수를 프로그래밍 방식으로 계산합니다.

PdfReader pr = new PdfReader("/path/to/yourFile.pdf"); 
return pr.getNumberOfPages(); 

또 다른 아이디어는 어떻게 든 많은 페이지가 얼마나 볼 수있는 파일을 여는 등은 간단하게 만들 수있는 하나의 PDF 파일로 함께로 모든 파일을 병합하는 것입니다.

+0

vb.net에 있어야합니까? 이 작업은 쉘 스크립팅 언어에서 훨씬 더 단순한 것처럼 들립니다. –

+0

@ EliSadoff 다른 언어에서는 더 쉬울 수도 있지만 C# 및 VB.NET에만 익숙합니다. 코드가 어렵지 않다면 알아낼 수있을 것입니다. – Cheddar

+0

각 디렉토리의 파일을 검사하는 재귀 적 하위처럼 보입니다. 그러면 서브 디렉토리가 훌륭하게 작동하는지 확인할 수 있습니다. sun-directories가 발견되면 스스로 다시 호출하고 각 하위 디렉토리에서 같은 검사를 수행하십시오 ... – soohoonigan

답변

0

다음은 VBA 솔루션입니다. Excel에서 코드를 실행하십시오.

Sub PDFandNumPages() 

    Dim Folder As Object 
    Dim file As Object 
    Dim fso As Object 
    Dim iExtLen As Integer, iRow As Integer 
    Dim sFolder As String, sExt As String 
    Dim sPDFName As String 

    sExt = "pdf" 
    iExtLen = Len(sExt) 
    iRow = 1 
    ' Must have a '\' at the end of path 
    sFolder = "C:\your_path_here\" 

    Set fso = CreateObject("Scripting.FileSystemObject") 

    If sFolder <> "" Then 
     Set Folder = fso.GetFolder(sFolder) 
     For Each file In Folder.Files 
     If Right(file, iExtLen) = sExt Then 
      Cells(iRow, 1).Value = file.Name 
      Cells(iRow, 2).Value = pageCount(sFolder & file.Name) 
      iRow = iRow + 1 
     End If 
     Next file 
    End If 

End Sub 

Function pageCount(sFilePathName As String) As Integer 

Dim nFileNum As Integer 
Dim sInput As String 
Dim sNumPages As String 
Dim iPosN1 As Integer, iPosN2 As Integer 
Dim iPosCount1 As Integer, iPosCount2 As Integer 
Dim iEndsearch As Integer 

' Get an available file number from the system 
nFileNum = FreeFile 

'OPEN the PDF file in Binary mode 
Open sFilePathName For Binary Lock Read Write As #nFileNum 

    ' Get the data from the file 
    Do Until EOF(nFileNum) 
     Input #1, sInput 
     sInput = UCase(sInput) 
     iPosN1 = InStr(1, sInput, "/N ") + 3 
     iPosN2 = InStr(iPosN1, sInput, "/") 
     iPosCount1 = InStr(1, sInput, "/COUNT ") + 7 
     iPosCount2 = InStr(iPosCount1, sInput, "/") 

    If iPosN1 > 3 Then 
     sNumPages = Mid(sInput, iPosN1, iPosN2 - iPosN1) 
     Exit Do 
    ElseIf iPosCount1 > 7 Then 
     sNumPages = Mid(sInput, iPosCount1, iPosCount2 - iPosCount1) 
     Exit Do 
    ' Prevent overflow and assigns 0 to number of pages if strings are not in binary 
    ElseIf iEndsearch > 1001 Then 
     sNumPages = "0" 
     Exit Do 
    End If 
     iEndsearch = iEndsearch + 1 
    Loop 

    ' Close pdf file 
    Close #nFileNum 
    pageCount = CInt(sNumPages) 

End Function 

다음은 본질적으로 동일한 작업을 수행하는 대체 방법입니다.

Sub Test() 
    Dim MyPath As String, MyFile As String 
    Dim i As Long 
    MyPath = "C:\your_path_here\" 
    MyFile = Dir(MyPath & Application.PathSeparator & "*.pdf", vbDirectory) 
    Range("A:B").ClearContents 
    Range("A1") = "File Name": Range("B1") = "Pages" 
    Range("A1:B1").Font.Bold = True 
    i = 1 
    Do While MyFile <> "" 
     i = i + 1 
     Cells(i, 1) = MyFile 
     Cells(i, 2) = GetPageNum(MyPath & Application.PathSeparator & MyFile) 
     MyFile = Dir 
    Loop 
    Columns("A:B").AutoFit 
    MsgBox "Total of " & i - 1 & " PDF files have been found" & vbCrLf _ 
      & " File names and corresponding count of pages have been written on " _ 
      & ActiveSheet.Name, vbInformation, "Report..." 
End Sub 
' 
Function GetPageNum(PDF_File As String) 
    'Haluk 19/10/2008 
    Dim FileNum As Long 
    Dim strRetVal As String 
    Dim RegExp 
    Set RegExp = CreateObject("VBscript.RegExp") 
    RegExp.Global = True 
    RegExp.Pattern = "/Type\s*/Page[^s]" 
    FileNum = FreeFile 
    Open PDF_File For Binary As #FileNum 
     strRetVal = Space(LOF(FileNum)) 
     Get #FileNum, , strRetVal 
    Close #FileNum 
    GetPageNum = RegExp.Execute(strRetVal).Count 
End Function