最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#獲取指定PDF文件頁數(shù)的方法

 更新時間:2015年04月17日 15:02:53   作者:work24  
這篇文章主要介紹了C#獲取指定PDF文件頁數(shù)的方法,涉及C#操作pdf文件的技巧,非常具有實用價值,需要的朋友可以參考下

本文實例講述了C#獲取指定PDF文件頁數(shù)的方法。分享給大家供大家參考。具體如下:

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace RobvanderWoude
{
 class PDFPageCount
 {
  static int Main( string[] args )
  {
   #region Get help
   if ( args.Length == 0 )
   {
    ShowHelp( );
    return 0;
   }
   foreach ( string arg in args )
   {
    if ( arg == "/?" || arg == "-?" || arg.ToLower( ) == "--help" )
    {
     ShowHelp( );
     return 0;
    }
   }
   #endregion
   int errors = 0;
   foreach ( string arg in args )
   {
    try
    {
     Regex regexp = new Regex( @"^(.*)\\([^\\]+\.pdf)$", RegexOptions.IgnoreCase );
     if ( regexp.IsMatch( arg ) )
     {
      // Match means the filespec has a valid format (i.e. *.pdf)
      string[] matches = regexp.Split( arg );
      string folder = matches[1];
      string filespec = matches[2];
      if ( Directory.Exists( folder ) )
      {
       // Folder exists, check for matching files
       string[] fileList = Directory.GetFiles( folder, filespec );
       if ( fileList.Length == 0 )
       {
        // No matching files in this folder
        ShowError( "ERROR: No files matching \"{0}\" were found in \"{1}\"", filespec, folder );
        errors += 1;
       }
       else
       {
        // Iterate through list of matching files
        foreach ( string file in fileList )
        {
         int pagecount = PageCount( file );
         if ( pagecount == -1 )
         {
          // Just increase the error count, the PageCount( )
          // procedure already wrote an error message to screen
          errors += 1;
         }
         else
         {
          // No pages means there is a problem with the file
          if ( pagecount == 0 )
          {
           Console.ForegroundColor = ConsoleColor.Red;
           errors += 1;
          }
          // Display the formated result on screen
          Console.WriteLine( "{0,4} {1,-10} {2}", pagecount.ToString( ), ( pagecount == 1 ? "page" : "pages" ), file );
          if ( pagecount == 0 )
          {
           Console.ForegroundColor = ConsoleColor.Gray;
          }
         }
        }
       }
      }
      else
      {
       // Folder doesn't exist
       ShowError( "ERROR: Folder \"{0}\" not found", folder );
       errors += 1;
      }
     }
     else
     {
      // No match for the regular expression means the filespec was invalid
      ShowError( "ERROR: Invalid filespec \"{0}\", please specify PDF files only", arg );
      errors += 1;
     }
    }
    catch ( Exception e )
    {
     // All other errors: display an error message and then continue
     ShowError( "ERROR: {0}", e.Message );
     errors += 1;
    }
   }
   if ( errors != 0 )
   {
    ShowError( "    {0} finished with {1} error{2}", GetExeName( ), errors.ToString( ), ( errors == 1 ? "" : "s" ) );
   }
   return errors;
  }
  static string GetExeName( )
  {
   string exe = Application.ExecutablePath.ToString( );
   Regex regexp = new Regex( @"\\([^\\]+)$" );
   return regexp.Split( exe )[1];
  }
  static int PageCount( string filename )
  {
   Regex regexp = new Regex( @"\.pdf$", RegexOptions.IgnoreCase );
   if ( regexp.IsMatch( filename ) )
   {
    try
    {
     FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
     StreamReader sr = new StreamReader( fs );
     string pdfText = sr.ReadToEnd( );
     regexp = new Regex( @"/Type\s*/Page[^s]" );
     MatchCollection matches = regexp.Matches( pdfText );
     return matches.Count;
    }
    catch ( Exception e )
    {
     ShowError( "ERROR: {0} ({1})", e.Message, filename );
     return -1;
    }
   }
   else
   {
    ShowError( "ERROR: {0} is not a PDF file", filename );
    return -1;
   }
  }
  static void ShowError( string message, string param1, string param2 = "", string param3 = "" )
  {
   Console.Error.WriteLine( );
   Console.ForegroundColor = ConsoleColor.Red;
   Console.Error.WriteLine( message, param1, param2, param3 );
   Console.ForegroundColor = ConsoleColor.Gray;
   Console.Error.WriteLine( );
  }
  #region Display help text
  static void ShowHelp( )
  {
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "{0}, Version 1.02", GetExeName( ) );
   Console.Error.WriteLine( "Return the page count for the specified PDF file(s)" );
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "Usage: {0} filespec [ filespec [ filespec [ ... ] ] ]", GetExeName( ).ToUpper( ) );
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "Where: \"filespec\"  is a file specification for the PDF file(s) to" );
   Console.Error.WriteLine( "       be listed (wildcards * and ? are allowed)" );
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "Note:  The program's return code equals the number of errors encountered." );
   Console.Error.WriteLine( );
   Console.Error.WriteLine( "Written by Rob van der Woude" );
  }
  #endregion
 }
}

希望本文所述對大家的C#程序設(shè)計有所幫助。

相關(guān)文章

  • C# 當前系統(tǒng)時間獲取及時間格式詳解

    C# 當前系統(tǒng)時間獲取及時間格式詳解

    這篇文章主要介紹了C# 當前系統(tǒng)時間獲取及時間格式詳解的相關(guān)資料,這里提供代碼實例,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下
    2016-12-12
  • C#縮略圖多路徑多格式保存的實例

    C#縮略圖多路徑多格式保存的實例

    這篇文章介紹了C#縮略圖多路徑多格式保存的實例,有需要的朋友可以參考一下
    2013-07-07
  • C#實現(xiàn)簡易計算器功能(2)(窗體應(yīng)用)

    C#實現(xiàn)簡易計算器功能(2)(窗體應(yīng)用)

    這篇文章主要為大家詳細介紹了C#實現(xiàn)簡易計算器功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • C#管道式編程的介紹與實現(xiàn)

    C#管道式編程的介紹與實現(xiàn)

    這篇文章主要給大家介紹了關(guān)于C#管道式編程的介紹與實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用C#具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-07-07
  • C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法

    C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法

    這篇文章主要介紹了C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對象的用法,在C#中數(shù)組也是對象可以被傳遞,需要的朋友可以參考下
    2016-01-01
  • c# 如何實現(xiàn)一個簡單的json解析器

    c# 如何實現(xiàn)一個簡單的json解析器

    這篇文章主要介紹了c# 如何實現(xiàn)一個簡單的json解析器,文中講解非常細致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-07-07
  • 深入理解C#中常見的委托

    深入理解C#中常見的委托

    本篇文章是對C#中常見的委托進行了詳細的分析介紹,需要的朋友參考下
    2013-06-06
  • C# 設(shè)計模式系列教程-橋接模式

    C# 設(shè)計模式系列教程-橋接模式

    橋接模式降低了沿著兩個或多個維度擴展時的復(fù)雜度,防止類的過度膨脹,解除了兩個或多個維度之間的耦合,使它們沿著各自方向變化而不互相影響。
    2016-06-06
  • 利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word

    利用C#/VB.NET實現(xiàn)將PDF轉(zhuǎn)為Word

    眾所周知,PDF 文檔支持特長文件,集成度和安全可靠性都較高,可有效防止他人對 PDF 內(nèi)容進行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下
    2022-12-12
  • 如何在C#中集成Lua腳本

    如何在C#中集成Lua腳本

    這篇文章主要介紹了如何在C#中集成Lua腳本,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下
    2021-02-02

最新評論

越西县| 外汇| 西安市| 大城县| 双牌县| 沙田区| 彰化市| 朝阳市| 务川| 察哈| 泗阳县| 岑溪市| 桑日县| 广水市| 本溪| 呼伦贝尔市| 忻城县| 青州市| 巴东县| 宾川县| 公安县| 合江县| 九龙坡区| 新密市| 额敏县| 巴塘县| 吉木萨尔县| 长阳| 鄂尔多斯市| 阿图什市| 罗江县| 金溪县| 区。| 尼木县| 宜黄县| 宁明县| 长丰县| 保定市| 连江县| 呼玛县| 长沙市|