C#獲取指定PDF文件頁數(shù)的方法
更新時(shí)間:2015年04月17日 15:02:53 作者:work24
這篇文章主要介紹了C#獲取指定PDF文件頁數(shù)的方法,涉及C#操作pdf文件的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
本文實(shí)例講述了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
}
}
希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:
- C#將Word轉(zhuǎn)換成PDF方法匯總(基于Office和WPS)
- C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
- C#使用itextsharp生成PDF文件的實(shí)現(xiàn)代碼
- C# 生轉(zhuǎn)換網(wǎng)頁為pdf
- word ppt excel文檔轉(zhuǎn)換成pdf的C#實(shí)現(xiàn)代碼
- C#將jpg轉(zhuǎn)換為pdf的方法
- c#開發(fā)word批量轉(zhuǎn)pdf源碼分享
- C#實(shí)現(xiàn)HTML轉(zhuǎn)WORD及WORD轉(zhuǎn)PDF的方法
- C#實(shí)現(xiàn)pdf導(dǎo)出 .Net導(dǎo)出pdf文件
- C#編程讀取文檔Doc、Docx及Pdf內(nèi)容的方法
- C#實(shí)現(xiàn)TIF圖像轉(zhuǎn)PDF文件的方法
- C#實(shí)現(xiàn)WPS文件轉(zhuǎn)PDF格式的方法示例
相關(guān)文章
C# 當(dāng)前系統(tǒng)時(shí)間獲取及時(shí)間格式詳解
這篇文章主要介紹了C# 當(dāng)前系統(tǒng)時(shí)間獲取及時(shí)間格式詳解的相關(guān)資料,這里提供代碼實(shí)例,幫助大家學(xué)習(xí)參考,需要的朋友可以參考下2016-12-12
C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能(2)(窗體應(yīng)用)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)簡(jiǎn)易計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對(duì)象的用法
這篇文章主要介紹了C#編程中使用ref和out關(guān)鍵字來傳遞數(shù)組對(duì)象的用法,在C#中數(shù)組也是對(duì)象可以被傳遞,需要的朋友可以參考下2016-01-01
c# 如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的json解析器
這篇文章主要介紹了c# 如何實(shí)現(xiàn)一個(gè)簡(jiǎn)單的json解析器,文中講解非常細(xì)致,代碼幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下2020-07-07
利用C#/VB.NET實(shí)現(xiàn)將PDF轉(zhuǎn)為Word
眾所周知,PDF 文檔支持特長(zhǎng)文件,集成度和安全可靠性都較高,可有效防止他人對(duì) PDF 內(nèi)容進(jìn)行更改,所以在工作中深受大家喜愛。本文將分為兩部分介紹如何以編程的方式將 PDF 轉(zhuǎn)換為 Word,需要的可以參考一下2022-12-12

