Page 1 of 1

Magick++ and PDF

Posted: 2009-03-11T09:53:37-07:00
by Kuzma
In program I need read PDF files and them convert it to BLOB.
The default density 72dpi very small for me. I need 300-600dpi.
In the program I use Magick++ class.

Please give me example of setting pdf density before reading file into memory.

Re: Magick++ and PDF

Posted: 2009-03-11T10:28:02-07:00
by magick
Please give me example of setting pdf density before reading file into memory.

Code: Select all

    try
    {
       Image image;
       image.density("100");
       image.read("logo.pdf");
       image.write("logo.miff");
    }
    catch (Magick::WarningCoder &e)
    {
        cout << "Warning " << e.what() << endl;
    }

Re: Magick++ and PDF

Posted: 2009-03-11T10:37:30-07:00
by Kuzma
This is situation when pdf file consist of one page. What about multipage pdf?

Re: Magick++ and PDF

Posted: 2009-03-11T10:40:33-07:00
by magick
Magick++ will process multiple PDF's. If you want to access individual pages use STL.

Re: Magick++ and PDF

Posted: 2009-03-11T20:40:06-07:00
by Kuzma
I try such code

...
image.read(&fname[1]); //I wont read second page of pdf file "fname"
...
It compile fine, but when I run program with filename "file_venugopal.pdf" I recive error "ImageMagick: unable to open image `ile_venugopal.pdf': No such file or directory @ blob.c/OpenBlob/2411"
Where is my mistake?

Re: Magick++ and PDF

Posted: 2009-03-12T06:24:09-07:00
by el_supremo
&fname[1] is the address of the second character of the string in fname. So, since fname is "file_venugopal.pdf", &fname[1] will pass the string "ile_venugopal.pdf" to image.read.
What you need is to put "[1]" as part of the file name so that fname is "file_venugopal.pdf[1]"

Pete

Re: Magick++ and PDF

Posted: 2009-03-12T10:53:04-07:00
by Kuzma
I try

Code: Select all

    try {                                  
        Image image;                       
        image.density("300");              
        int i=1;                           
        char* temp;                        
        sprintf(temp, "\"%s[%d]\"",fname, i );
        cout << "Now we form file name"<<"\n";
        cout << temp;                         
        image.read(temp);                     
        // Write to BLOB in BMP format        
        image.write(&blob, "DIB");            
    } catch(Exception &error_) {              
        cerr << error_.what() << "\n";        
        return NULL;                          
 } 
with result:
...
Now we form file name
"file_venugopal.pdf[1]"ImageMagick: unable to open image `"file_venugopal.pdf[1]"': No such file or directory @ blob.c/OpenBlob/2411
...

Re: Magick++ and PDF

Posted: 2009-03-12T11:07:17-07:00
by magick
Don't use the double quotes. Double quotes are useful for command shells, not for the API.

Re: Magick++ and PDF

Posted: 2009-03-12T11:25:42-07:00
by Kuzma
Thank you, it's work.

Small question: How to identify number of page in multipage pdf without readImages() function?

Re: Magick++ and PDF

Posted: 2009-03-12T12:47:55-07:00
by magick
Use ping() to just return metadata associated with an image including the page count. To make it efficient, set the density / resolution to something small like 10 (e.g. image.density(10)).

Re: Magick++ and PDF

Posted: 2009-03-12T21:56:34-07:00
by Kuzma
I read ping() documentation and no found where it is number of page image atribute:
Ping is similar to read except only enough of the image is read to determine the image columns, rows, and filesize. The columns , rows , and fileSize attributes are valid after invoking ping. The image data is not valid after calling ping.

Re: Magick++ and PDF

Posted: 2009-03-14T22:55:22-07:00
by Kuzma
Is it "analog" of ping() function for STL?
How to change image density to 300dpi before reading image into STL list?

Re: Magick++ and PDF

Posted: 2014-07-30T14:49:46-07:00
by ruhlio
I know this is a dead thread but I'm stuck in the same pickle. I'm reading in a PDF with multiple pages. Each page needs to be read in at a DPI of 300x300. Seeing as I cannot pass options into Magick::readImages() my only option as far as I am aware is preallocating a STL container of Magick::Image and looping through setting calling density() on each of them before passing the container to Magick::readImages(). The issue is that ping()ing a Magick::Image doesn't allow it to return the number of pages. The only way I see to retrieve the number of pages in a PDF document is to call Magick::readImages() and call size() on the STL container. I'd rather not have to read the PDFs in twice, one to preallocate the Magick::Images and set the density, then again to read in the PDF again with the desired density.

Re: Magick++ and PDF

Posted: 2014-07-31T00:06:56-07:00
by dlemstra
I will see if I can add something that will allow you to specify the density before the next release.

Edit: With the next release of ImageMagick (6.8.9-7) you will be able to do this:

Code: Select all

ReadOptions options;
options.density(Geometry(300, 300));

std::list<Image> images;
readImages(&images, "C:\\test.pdf", options);

size_t i;
std::list<Image>::iterator image;
for (image = images.begin(), i = 0; image != images.end(); image++, i++)
  image->write("C:\\test." + std::to_string(i) + ".png");