Pdf counter

I have a bunch of pdf files in a folder and I would like to know the best way, either using the free software for a PDF counter, or programmatically, how to count the number of pages for each pdf file and put the result in Excel or access table. I already have a table filled with pdf file names. I googled a "PDF page counter" and there were a few hits, however I'm not sure how trustworthy these tools are. So, what are some of the names of reliable PDF page counting tools and software, and are there any good examples of VB.NET code that try to do this?

Thank!

+3
source share
4 answers

iText pdf. http://www.itextpdf.com/ java-, #, .

; java pdf :

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

. pdftk powershell

dir c:\ *.pdf | foreach-object {

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

    New-Object PSObject -Property @{
        Name = $_.Name
        FullName = $_.FullName
        NumberOfPages = $NumberOfPages
    }
} | select name,fullname,numberofpages | export-csv -notypeinformation d:\list.txt 

, , PDF . itextsharp

[void][System.Reflection.Assembly]::LoadFrom("c:\itextsharp\itextsharp.dll")
gci -path c:\ *.pdf | foreach-object{

    $itext = new-object itextsharp.text.pdf.PdfReader($_.fullname)
    if (-not $itext.IsEncrypted() ) {
    $pdf = pdftk.exe $_.FullName dump_data
    $NumberOfPages = [regex]::match($pdf,'NumberOfPages: (\d+)').Groups[1].Value

    New-Object PSObject -Property @{
        Name = $_.Name
        FullName = $_.FullName
        NumberOfPages = $NumberOfPages
        }
    }

    else {
     New-Object PSObject -Property @{
        Name = $_.Name
        FullName = $_.FullName
        NumberOfPages = "encrypted"
        }

    }

} |Select-Object name,fullname,numberofpages | export-csv -notypeinformation d:\list2.txt 

, .

. , script , powershell:)

+4

Nick, pdftk , itextsharp.

Why do you need this? Well, it turns out that pdftk cannot read (returning java.NulPointerException) some PDF files that itextsharp can use. In fact, I managed to create a function using pdftk and regular expressions, but I had to switch to itextsharp due to these exceptions.

The function is as follows (and quite simple):

function Count-PdfPages{
Param([System.IO.FileSystemInfo]$file)
# loads itextsharp
[void][System.Reflection.Assembly]::LoadFrom("C:\Users\me\Desktop\itextsharp-all-5.3.4\itextsharp.dll")

$itext = new-object itextsharp.text.pdf.PdfReader($file.fullname)

if (-not $itext.IsEncrypted() ) {
    $NumberOfPages = $itext.NumberOfPages
    return $numberOfPages
}

else{
    return "The file $($file.fullname) is encrypted"
}

}
# Example
Set-Location 'C:\Users\me\Desktop\Nueva carpeta'

Get-ChildItem | Where-object{$_.extension -eq '.pdf'} | ForEach-Object{Count-PdfPages $_}
+1
source

One line:

Dim pdfPageCount As Integer = System.IO.File.ReadAllText("example.pdf").Split(New String() {"/Type /Page"}, StringSplitOptions.None).Count() - 2

Recommended: iTextSharp

imports iTextSharp.text.pdf

Dim pdfPath As String = "test.pdf"
Dim pdfReader As New PdfReader(pdfPath)
Dim numberOfPages As Integer = pdfReader.NumberOfPages
0
source

All Articles