PDF page count

//Here we can see how to count total number of pages in a pdf file
//itextsharp is a dll availabe through this we can count the number
//of pages in a PDF file
//The itextsharp can be downloaded from sourceforge.net/projects/itextsharp
//After downloading the itextsharp.dll add reference to your project.


using System;
using System.Collections.Generic;
using System.Text;
using iTextSharp.text.pdf; //namespace for PDF manipulation
using System.IO;

namespace PDFPageCounter
{
public class PageCount
{
//function for getting the total number of pages in a PDF file

public int GETPDFPageCount(string filepath)
{
int page_count = 0;
try
{
//check for the extension
string extension = Path.GetExtension(filepath);
if (extension == ".PDF" || extension == ".pdf")
{
//Create instance for the PDF reader
PdfReader pdf_fie = new PdfReader(filepath);
//read it's pagecount
page_count = pdf_fie.NumberOfPages;
//close the file
pdf_fie.Close();
}
}
catch (PdfException ex)
{
WriteLog(ex.Message);
}
return page_count;
}
//write log function is used to record the error message
//The error file will be saved in the application bin
public void WriteLog( string message)
{
string dt = DateTime.Now.Date.ToString();
string[] today = dt.Split('/');
string filedate = today[0].ToString() + "-" + today[1].ToString() + DateTime.Now.Year.ToString();
string filename = @"Error" + "-" + filedate + ".txt";
StreamWriter sw = new StreamWriter(AppDomain.CurrentDomain.BaseDirectory + filename);
sw.WriteLine(message );
sw.Close();
}
}
}


Comments



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: