Day: January 11, 2016

  • Powershell : นับหน้าเอกสาร PDF

    1. Download โปรแกรม PDFtk [ https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ ]
    2. Install PDFtk
    3. ใช้ editor ซักตัวเขียน Code Powershell ที่ผมใช้คือใช้ Visual Studio 2013 และ Download PowerShell Tools for Visual Studio 2013 [ https://visualstudiogallery.msdn.microsoft.com/f65f845b-9430-4f72-a182-ae2a7b8999d7 ] ที่ใช้งานตัวนี้เพราะ Tools จะมี InteliSense ให้ไม่ต้องจำคำสั่ง PowerShell ทั้งหมด ก็ทำให้สะดวกดี
    4.  Code ก็ไม่มาก
      #กำหนดที่เก็บผลการ$File
      Path = ‘E:\tmp\result.txt’ -f $env:Path; 
      #เป้าหมาย diretory ที่เก็บ file PDF วนคำนาณแต่ละ file
      dir e:\ *.pdf | foreach-object{

      $pdf = pdftk.exe $_.FullName dump_data
      $NumberOfPages = 0
      $NumberOfPages = [regex]::match($pdf,’NumberOfPages: (\d+)’).Groups[1].Value

      $infoObj = New-Object PSObject -Property @{
      Name = $_.Name
      FullName = $_.FullName
      NumberOfPages = $NumberOfPages
      }
      #บันทึกข้อมูลลง file
      $infoObj.”FullName”,$infoObj.”NumberOfPages” -join ‘,’ | Out-File -FilePath $FilePath -Append -Width 200;

      }

      5. Save แล้วก็ Execute เพื่อทดสอบได้เลยครับ