dotnet擷取PDF檔案的頁數

來源:互聯網
上載者:User

標籤: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檔案的頁數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.