Magick++ and PDF

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Kuzma

Magick++ and PDF

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

Re: Magick++ and PDF

Post 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;
    }
Kuzma

Re: Magick++ and PDF

Post by Kuzma »

This is situation when pdf file consist of one page. What about multipage pdf?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Magick++ and PDF

Post by magick »

Magick++ will process multiple PDF's. If you want to access individual pages use STL.
Kuzma

Re: Magick++ and PDF

Post 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?
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: Magick++ and PDF

Post 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
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
Kuzma

Re: Magick++ and PDF

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

Re: Magick++ and PDF

Post by magick »

Don't use the double quotes. Double quotes are useful for command shells, not for the API.
Kuzma

Re: Magick++ and PDF

Post by Kuzma »

Thank you, it's work.

Small question: How to identify number of page in multipage pdf without readImages() function?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: Magick++ and PDF

Post 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)).
Kuzma

Re: Magick++ and PDF

Post 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.
Kuzma

Re: Magick++ and PDF

Post by Kuzma »

Is it "analog" of ping() function for STL?
How to change image density to 300dpi before reading image into STL list?
ruhlio
Posts: 1
Joined: 2014-07-30T14:43:44-07:00
Authentication code: 6789

Re: Magick++ and PDF

Post 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.
User avatar
dlemstra
Posts: 1570
Joined: 2013-05-04T15:28:54-07:00
Authentication code: 6789
Contact:

Re: Magick++ and PDF

Post 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");
.NET + ImageMagick = Magick.NET https://github.com/dlemstra/Magick.NET, @MagickNET, Donate
Post Reply