zoukankan      html  css  js  c++  java
  • dotnet获取PDF文件的页数

            #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
  • 相关阅读:
    python -django 之第三方支付
    python 的排名,已经python的简单介绍
    第三方登录
    linux 基础命令
    JWT 加密
    Docker 简介
    中文分词库:结巴分词
    Django websocket 长连接使用
    jQuery截取字符串的几种方式
    Python 操作redis
  • 原文地址:https://www.cnblogs.com/EasonJim/p/6258649.html
Copyright © 2011-2022 走看看