Get page count of pdf
Get page count of pdf
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
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
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
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:
Thank you
Morfio
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;
}
Morfio
Re: Get page count of pdf
Try this:
That way any exception is displayed. You code appears to return if there is an exception.
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);
Re: Get page count of pdf
With this code I get this result:
Here's the code:
Code: Select all
1
test2.c main 28
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
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
We're using ImageMagick 6.8.9-1 and Ghostscript 9.10. Your program returns:
- -> ./pdf
116
pdf.c main 28
Re: Get page count of pdf
I'm using "6.8.0 Q16 HDRI" on FreeBSD 10.0 amd64.
Re: Get page count of pdf
And your ghostscript version is?