Get page count of pdf

The MagickWand interface is a new high-level C API interface to ImageMagick core methods. We discourage the use of the core methods and encourage the use of this API instead. Post MagickWand questions, bug reports, and suggestions to this forum.
Post Reply
Morfio
Posts: 5
Joined: 2014-05-21T00:55:07-07:00
Authentication code: 6789

Get page count of pdf

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Get page count of pdf

Post 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.
Morfio
Posts: 5
Joined: 2014-05-21T00:55:07-07:00
Authentication code: 6789

Re: Get page count of pdf

Post 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
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Get page count of pdf

Post 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.
Morfio
Posts: 5
Joined: 2014-05-21T00:55:07-07:00
Authentication code: 6789

Re: Get page count of pdf

Post 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;
}
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Get page count of pdf

Post 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.
Morfio
Posts: 5
Joined: 2014-05-21T00:55:07-07:00
Authentication code: 6789

Re: Get page count of pdf

Post by Morfio »

User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Get page count of pdf

Post 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.
Morfio
Posts: 5
Joined: 2014-05-21T00:55:07-07:00
Authentication code: 6789

Re: Get page count of pdf

Post by Morfio »

I'm using "6.8.0 Q16 HDRI" on FreeBSD 10.0 amd64.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Get page count of pdf

Post by dlemstra »

And your ghostscript version is?
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply