C#获取指定PDF文件页数的方法
程序员文章站
2023-11-06 21:32:28
本文实例讲述了c#获取指定pdf文件页数的方法。分享给大家供大家参考。具体如下:
using system;
using system.io;
using s...
本文实例讲述了c#获取指定pdf文件页数的方法。分享给大家供大家参考。具体如下:
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#程序设计有所帮助。
上一篇: C#操作SQLite方法实例详解