Page 1 of 1

Get page count of pdf

Posted: 2014-05-21T01:01:23-07:00
by Morfio
Hello,

I want to count pages in a pdf file. I use "printf("%lu\n", MagickGetNumberImages(m_wand));", but it returns always one page (the pdf has 116 pages). I initiate the MagickWand with "MagickReadImage(m_wand, "test.pdf");".

How do I get the real page count?

Thank you very much

Morfio

Re: Get page count of pdf

Posted: 2014-05-21T04:12:32-07:00
by magick
We're using ImageMagick 6.8.9-1, the current release, and MagickGetNumberImages() returns the correct number of pages in our PDF. Do you check for any exceptions? Check the return value of MagickReadImage(). It should be something other than 0. If its 0, use MagickGetException() to display the error.

Re: Get page count of pdf

Posted: 2014-05-21T04:46:17-07:00
by Morfio
With another pdf it works, but it needs a very long time. With my test pdf file it doesn't work (and needs a long time, too).

Exceptions are not thrown and MagickReadImage returns MagickTrue. Here's my code, maybe there are some mistakes:

Code: Select all

#include <wand/magick_wand.h>

int main() {
    MagickWand *m_wand = NULL;
        
    MagickWandGenesis();
        
    m_wand = NewMagickWand();
    if(MagickReadImage(m_wand, "test.pdf") != MagickTrue)
        return 1;

    printf("%lu\n", MagickGetNumberImages(m_wand));

    ExceptionType ex; 
    printf("%s\n", MagickGetException(m_wand, &ex));

    if(m_wand)
        m_wand = DestroyMagickWand(m_wand);
        
    MagickWandTerminus();

    return 0;
}
Thank you

Morfio

Re: Get page count of pdf

Posted: 2014-05-21T05:00:57-07:00
by magick
Try this:

Code: Select all

#define ThrowWandException(wand) \
{ \
  char \
    *description; \
 \
  ExceptionType \
    severity; \
 \
  description=MagickGetException(wand,&severity); \
  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
  description=(char *) MagickRelinquishMemory(description); \
  exit(-1); \
}
...
  status=MagickReadImage(magick_wand,argv[1]);
  if (status == MagickFalse)
    ThrowWandException(magick_wand);
That way any exception is displayed. You code appears to return if there is an exception.

Re: Get page count of pdf

Posted: 2014-05-22T00:35:54-07:00
by Morfio
With this code I get this result:

Code: Select all

1
test2.c main 28
Here's the code:

Code: Select all

#include <wand/magick_wand.h>

#define ThrowWandException(wand) \
{ \
  char \
    *description; \
 \
  ExceptionType \
    severity; \
 \
  description=MagickGetException(wand,&severity); \
  (void) fprintf(stderr,"%s %s %lu %s\n",GetMagickModule(),description); \
  description=(char *) MagickRelinquishMemory(description); \
  exit(-1); \
}

int main() {
	MagickWand *m_wand = NULL;
	
	MagickWandGenesis();
	
	m_wand = NewMagickWand();
	if(MagickReadImage(m_wand, "gna.pdf") != MagickTrue)
		return 1;

	printf("%lu\n", MagickGetNumberImages(m_wand));

	ThrowWandException(m_wand);

	if(m_wand)
		m_wand = DestroyMagickWand(m_wand);
	
	MagickWandTerminus();

	return 0;
}

Re: Get page count of pdf

Posted: 2014-05-22T03:02:02-07:00
by magick
We need to test your PDF. Post a URL to your PDF. We need to reproduce the problem before we can comment further.

Re: Get page count of pdf

Posted: 2014-05-22T04:27:07-07:00
by Morfio

Re: Get page count of pdf

Posted: 2014-05-22T04:59:59-07:00
by magick
We're using ImageMagick 6.8.9-1 and Ghostscript 9.10. Your program returns:
  • -> ./pdf
    116
    pdf.c main 28
We're not sure why its failing for you.

Re: Get page count of pdf

Posted: 2014-05-23T05:53:26-07:00
by Morfio
I'm using "6.8.0 Q16 HDRI" on FreeBSD 10.0 amd64.

Re: Get page count of pdf

Posted: 2014-05-23T05:54:42-07:00
by dlemstra
And your ghostscript version is?