.NET实现在网页中预览Office文件的3个方法(3)

#region 2.01 读取pdf文件的总页数 +GetPageCount(string pdf_filename)
        /// <summary>
        /// 读取pdf文件的总页数
        /// </summary>
        /// <param>pdf文件</param>
        /// <returns></returns>
        public static int GetPageCountByPDF(string pdf_filename)
        {
            int pageCount = 0;
            if (System.IO.File.Exists(pdf_filename))
            {
                try
                {
                    byte[] buffer = System.IO.File.ReadAllBytes(pdf_filename);
                    if (buffer != null && buffer.Length > 0)
                    {
                        pageCount = -1;
                        string pdfText = Encoding.Default.GetString(buffer);
                        Regex regex = new Regex(@"/Type\s*/Page[^s]");
                        MatchCollection conllection = regex.Matches(pdfText);
                        pageCount = conllection.Count;
                    }
                }
                catch (Exception ex)
                {
                    Souxuexiao.API.Logger.error(string.Format("读取pdf文件的总页数执行GetPageCountByPowerPoint函数发生异常原因是:{0}", ex.Message));
                }
            }
            return pageCount;
        }
        #endregion

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wjzdgf.html