欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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#程序設計有所幫助。

相關(guān)文章

  • C#數(shù)據(jù)類型實現(xiàn)背包、隊列和棧

    C#數(shù)據(jù)類型實現(xiàn)背包、隊列和棧

    本文詳細講解了C#數(shù)據(jù)結(jié)構(gòu)類型,并實現(xiàn)背包、隊列和棧的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-04-04
  • C#調(diào)用Java代碼的方法介紹

    C#調(diào)用Java代碼的方法介紹

    這篇文章介紹了C#調(diào)用Java代碼的方法,有需要的朋友可以參考一下
    2013-10-10
  • C#調(diào)用WebService的方法介紹

    C#調(diào)用WebService的方法介紹

    這篇文章介紹了C#調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • C#中out保留字用法實例分析

    C#中out保留字用法實例分析

    這篇文章主要介紹了C#中out保留字用法,實例分析了方法返回值時采用out保留字的用法,需要的朋友可以參考下
    2014-09-09
  • C#實現(xiàn)文本文件讀寫方法匯總

    C#實現(xiàn)文本文件讀寫方法匯總

    本文給大家匯總介紹了C#實現(xiàn)文本文件讀寫的方法,十分的簡單實用,有需要的小伙伴可以參考下。
    2015-06-06
  • 在C#中捕獲內(nèi)存不足異常

    在C#中捕獲內(nèi)存不足異常

    這篇文章主要介紹了在C#中捕獲內(nèi)存不足異常,下面文章內(nèi)容圍繞如何在C#中捕獲內(nèi)存不足異常的相關(guān)資料展開詳細內(nèi)容,具有一定的參考價值,需要的小伙伴可以參考一下,希望對你有所幫助
    2021-12-12
  • C# WPF實現(xiàn)動態(tài)3D光照效果

    C# WPF實現(xiàn)動態(tài)3D光照效果

    這篇文章主要為大家詳細介紹了如何利用C# WPF實現(xiàn)動態(tài)3D的光照效果,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2023-03-03
  • c#基礎系列之值類型和引用類型的深入理解

    c#基礎系列之值類型和引用類型的深入理解

    這篇文章主要給大家介紹了關(guān)于c#基礎系列之值類型和引用類型的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2018-09-09
  • c#實現(xiàn)抓取高清美女妹紙圖片

    c#實現(xiàn)抓取高清美女妹紙圖片

    本文給大家分享的是一則使用c#實現(xiàn)抓取網(wǎng)絡高清美女妹紙圖片的代碼,這么好的東西,當然不能獨享,推薦給小伙伴們。
    2015-03-03
  • C#微信分享代碼

    C#微信分享代碼

    這篇文章主要為大家詳細介紹了C#微信分享的實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10

最新評論