C#获取pdf文档的页数

 1 /// <summary>
 2 /// 获取pdf文档的页数
 3 /// </summary>
 4 /// <param name="filePath"></param>
 5 /// <returns>-1表示文件不存在</returns>
 6 public static int GetPDFofPageCount(string filePath)
 7 {
 8     int count = -1;//-1表示文件不存在
 9     if (File.Exists(filePath))
10     {
11         using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
12         {
13             StreamReader reader = new StreamReader(fs);
14             //从流的当前位置到末尾读取流
15             string pdfText = reader.ReadToEnd();
16             Regex rgx = new Regex(@"/Type\s*/Page[^s]");
17             MatchCollection matches = rgx.Matches(pdfText);
18             count = matches.Count;
19         }
20     }
21     return count;
22 }

转自:https://blog.csdn.net/weixin_34037515/article/details/86193239

posted @ 2022-07-12 18:26  枫亦  阅读(19)  评论(0)    收藏  举报