龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

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

时间:2014-10-09 11:43来源:网络整理 作者:网络 点击:
分享到:
#region 2.01 读取pdf文件的总页数 +GetPageCount(string pdf_filename) /// summary /// 读取pdf文件的总页数 /// /summary /// param name="pdf_filename"pdf文件/param /// returns/returns pu

        #region 2.01 读取pdf文件的总页数 +GetPageCount(string pdf_filename)
        /// <summary>
        /// 读取pdf文件的总页数
        /// </summary>
        /// <param name="pdf_filename">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

精彩图集

赞助商链接