C#获取PDF文件的页数

 1使用方法:GetPDFPageCount(string path)//path参数是pdf文件的路径
 2/**//晴穹//
 3
 4  获取pdf文件的页数#region 获取pdf文件的页数
 5       private int BytesLastIndexOf(Byte[] buffer, int length, string Search)
 6        {
 7            if (buffer == null)
 8                return -1;
 9            if (buffer.Length <= 0)
10                return -1;
11            byte[] SearchBytes = Encoding.Default.GetBytes(Search.ToUpper());
12            for (int i = length - SearchBytes.Length; i >= 0; i--)
13            {
14                bool bFound = true;
15                for (int j = 0; j < SearchBytes.Length; j++)
16                {
17                    if (ByteUpper(buffer[i + j]) != SearchBytes[j])
18                    {
19                        bFound = false;
20                        break;
21                    }
22                }
23                if (bFound)
24                    return i;
25            }
26            return -1;
27        }
28        private byte ByteUpper(byte byteValue)
29        {
30            char charValue = Convert.ToChar(byteValue);
31            if (charValue < 'a' || charValue > 'z')
32                return byteValue;
33            else
34                return Convert.ToByte(byteValue - 32);
35        }
36        /** <summary>
37        /// 获取pdf文件的页数
38        /// </summary>
39        ///
40        public int GetPDFPageCount(string path) //获取pdf文件的页数
41        {
42            byte[] buffer = File.ReadAllBytes(path);
43            int length = buffer.Length;
44            if (buffer == null)
45                return -1;
46            if (buffer.Length <= 0)
47                return -1;
48            try
49            {
50                //Sample
51                //      29 0 obj
52                //      <</Count 9
53                //      Type /Pages
54                int i = 0;
55                int nPos = BytesLastIndexOf(buffer, length, "/Type/Pages");
56                if (nPos == -1)
57                    return -1;
58                string pageCount = null;
59                for (i = nPos; i < length - 10; i++)
60                {
61                    if (buffer[i] == '/' && buffer[i + 1] == 'C' && buffer[i + 2] == 'o' && buffer[i + 3] == 'u' && buffer[i + 4] == 'n' && buffer[i + 5] == 't')
62                    {
63                        int j = i + 3;
64                        while (buffer[j] != '/' && buffer[j] != '>')
65                            j++;
66                        pageCount = Encoding.Default.GetString(buffer, i, j - i);
67                        break;
68                    }
69                }
70                if (pageCount == null)
71                    return -1;
72                int n = pageCount.IndexOf("Count");
73                if (n > 0)
74                {
75                    pageCount = pageCount.Substring(n + 5).Trim();
76                    for (i = pageCount.Length - 1; i >= 0; i--)
77                    {
78                        if (pageCount[i] >= '0' && pageCount[i] <= '9')
79                        {
80                            return int.Parse(pageCount.Substring(0, i + 1));
81                        }
82                    }
83                }
84                return -1;
85            }
86            catch (Exception ex)
87            {
88                return -1;
89            }
90        }
91       #endregion

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页