-
Notifications
You must be signed in to change notification settings - Fork 1
/
pages.go
59 lines (53 loc) · 1.31 KB
/
pages.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright 2012 Kevin Gillette. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
Package npdfpages has no purpose other than to determine the number of pages in
a PDF file. No checking is done to determine that the input is well formatted.
It's possible, but extremely unlikely that PDF streams could contain byte
sequences that result in false positives, though the number returned will be no
less than the number of actual pages in the PDF.
*/
package npdfpages
import (
"bufio"
"io"
"os"
)
const match = "/Page\x00"
// Pages reads the given io.ByteReader until EOF is reached, returning the
// number of pages encountered.
func Pages(reader io.ByteReader) (pages int) {
i := 0
for {
b, err := reader.ReadByte()
if err != nil {
return
}
check:
switch match[i] {
case 0:
if !(b >= 'A' && b <= 'Z' || b >= 'a' && b <= 'z') {
pages++
}
i = 0
goto check
case b:
i++
default:
i = 0
}
}
// flow shouldn't reach this point
return
}
// PagesAtPath opens a PDF file at the given file path, returning the number
// of pages found.
func PagesAtPath(path string) (pages int) {
if reader, err := os.Open(path); err == nil {
reader.Chdir()
pages = Pages(bufio.NewReader(reader))
reader.Close()
}
return
}