码迷,mamicode.com
首页 > Web开发 > 详细

dotnet获取PDF文件的页数

时间:2017-01-07 09:10:19      阅读:275      评论:0      收藏:0      [点我收藏+]

标签:class   tin   cat   length   span   false   --   indexof   last   

        #region 获取PDF文件的页数

        private int BytesLastIndexOf(Byte[] buffer, int length, string Search)
        {
            if (buffer == null)
                return -1;
            if (buffer.Length <= 0)
                return -1;
            byte[] SearchBytes = Encoding.Default.GetBytes(Search.ToUpper());
            for (int i = length - SearchBytes.Length; i >= 0; i--)
            {
                bool bFound = true;
                for (int j = 0; j < SearchBytes.Length; j++)
                {
                    if (ByteUpper(buffer[i + j]) != SearchBytes[j])
                    {
                        bFound = false;
                        break;
                    }
                }
                if (bFound)
                    return i;
            }
            return -1;
        }

        private byte ByteUpper(byte byteValue)
        {
            char charValue = Convert.ToChar(byteValue);
            if (charValue < a || charValue > z)
                return byteValue;
            else
                return Convert.ToByte(byteValue - 32);
        }

        /// <summary>
        /// 获取pdf文件的页数
        /// </summary>
        public int GetPDFPageCount(string path) //获取pdf文件的页数
        {
            path = HttpContext.Current.Server.MapPath(path);
            byte[] buffer = File.ReadAllBytes(path);
            int length = buffer.Length;
            if (buffer == null)
                return -1;
            if (buffer.Length <= 0)
                return -1;
            try
            {
                //Sample
                //      29 0 obj
                //      <</Count 9
                //      Type /Pages
                int i = 0;
                int nPos = BytesLastIndexOf(buffer, length, "/Type/Pages");
                if (nPos == -1)
                    return -1;
                string pageCount = null;
                for (i = nPos; i < length - 10; i++)
                {
                    if (buffer[i] == / && buffer[i + 1] == C && buffer[i + 2] == o && buffer[i + 3] == u && buffer[i + 4] == n && buffer[i + 5] == t)
                    {
                        int j = i + 3;
                        while (buffer[j] != / && buffer[j] != >)
                            j++;
                        pageCount = Encoding.Default.GetString(buffer, i, j - i);
                        break;
                    }
                }
                if (pageCount == null)
                    return -1;
                int n = pageCount.IndexOf("Count");
                if (n > 0)
                {
                    pageCount = pageCount.Substring(n + 5).Trim();
                    for (i = pageCount.Length - 1; i >= 0; i--)
                    {
                        if (pageCount[i] >= 0 && pageCount[i] <= 9)
                        {
                            return int.Parse(pageCount.Substring(0, i + 1));
                        }
                    }
                }
                return -1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        #endregion

 

dotnet获取PDF文件的页数

标签:class   tin   cat   length   span   false   --   indexof   last   

原文地址:http://www.cnblogs.com/EasonJim/p/6258649.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!