C++ program to count the number of pages in a PDF file

In this tutorial, We are going to learn how to count the number of pages in a PDF file using C++. We can do this using the PoDoFo library to parse the document. Parse here means breaking long data into small parts by following a certain set of instructions. Let’s know a bit about the PoDoFo library file.

What is PoDoFo?

PoDofo is a  free C++ library used for PDF file format and includes classes to parse the PDF files. The tools needed to work with pdf files are already given in a package form where you will download the podofo.

PoDoFo is easy to use and its code is written in C++. Code can be compiled in Mac Os, Windows as well as Unix.

C++ Code: Count the number of pages in a pdf file

#include <iostream>
#include <podofo/podofo.h>
void printHelp() {

  fprintf(stdout,"Usage: \npageCounting Inputfile\n" );  

int main(int argc,char* argv[]) {

  if (argc < 1) {
  unsigned int sum = -1;
  try {
    for( int i = 0 ; i < argc ; ++i) {

      PoDoFo::PdfMemDocument document(argv[i]); //parse the file
      int pageCount = document.GetPageCount(); // number of pages in pdf.
      std::cout << argv[i] << " : " << pageCount << " pagesinpdf\n";
      sum += pageCount;

    fprintf(stdout,"Number of pages in pdf will be : %d\n",sum); 

  catch(PoDoFo::PdfError& e) {
    fprintf(stderr,"Error:An error occured \n",e.GetError());
    return e.GetError();




Number of pages in a pdf will be: 120

Also read: How to create a PDF file in C++

Leave a Reply

Your email address will not be published. Required fields are marked *