Function for getting page sizes and number of pages in a PDF-file

Suppose you need to find out how many pages and what page sizes are in a particular PDF file. This can be done quite easily using our SCAN_PDF function. This function must be called separately for every page in the PDF file. The return values of this function indicate  whether there was only one page, or more, in your file, or other results as to why the information cannot be ascertained.

 

specific description of the function SCAN_PDF

call : retwert = SCAN_PDF( input_file, width, length );

 

1.      The parameters are:

a.       input file is a pointer to an ASCII string containing the name of the PDF file. Type is char *.

b.      The function stores the width of the actual page in mm in the address width. Type is float *.

c.       The function stores the length of the actual page in mm in the address length. Type is float *.

2.      The return values (all types int) are:

SCAN_FAILURE

-1

input_file cannot be opened for reading.

SCAN_UNKNOWN

0

input_file is not a PDF file. File was closed.

SCAN_NOSIZE

1

input_file is a PDF file, but no size information  has been found. File was closed.

SCAN_FRAME

2

There was only one page in the input_file and it’s size was stored in *width und *length. The file was closed.

SCAN_FRAME_MORE

3

The page size in the file was stored in *width und *length, but there might be further pages in the file. So the file has stayed open after the call.

SCAN_EOF

4

No further pages were found  in the input_file. Nothing was stored in *width and *length. The file was closed.

Example:

You want to find out the amount of paper it takes to print a PDF file book.pdf. As each page is printed simplex, the program calculates the sizes of all the pages in book.pdf.

 

 
#include <stdio.h>
#include "scan_pdf.h"
 
main()
{
   int retwert;         // the return value of the function SCAN_PDF
   float width;         // this variable SCAN_PDF stores the width of
                        // the actual page of book.pdf in mm.
 
   float length;        // this variable SCAN_PDF stores the length of
                        // the actual page of book.pdf in mm.
 
   double whole_size;   // this variable adds all the sizes of all the pages 
                        // of book.pdf.
 
   whole_size = 0.;
 
   do
   {
      retwert = SCAN_PDF( "buch.pdf", &width, &length );
 
      if ( retwert == SCAN_FAILURE ) 
      {
         error message;
         return;
      }
      else if ( retwert == SCAN_UNKNOWN )
      {
         error message;
         return;
      }
      else if ( retwert == SCAN_NOSIZE )
      {
         error message;
         return;
      }
      else if ( retwert == SCAN_FRAME )
      {
         whole_size = width * length;
         break;
      }
      else if ( retwert == SCAN_FRAME_MORE )
      whole_size += width * lenth ; 
 
   } while ( retwert != SCAN_EOF );
 
   printf("The whole size needed to print book.pdf is %f mm*mm\n", whole_size);
 
}