Page 1 of 1

Convert multi page tiff file to pdf

Posted: 2013-04-15T06:53:31-07:00
by kromm
Hello.
I'm trying to convert a multi page tiff file to pdf using Magick++. I'm using the following code:

Code: Select all

#include <Magick++.h>

int main(int argc, char **argv)
{
    Magick::InitializeMagick(argv[0]);
    Magick::Image img;
    img.read(argv[0]);
    string imageSpec = "PDF:";
    imageSpec += argv[1];
    img.write(imageSpec);
    return 0;
}
The problem is that the resulting pdf file has only one page. Example file: https://docs.google.com/file/d/0B5V7SIE ... sp=sharing
The same file is correctly converted if i use the convert tool (i.e. convert.exe input.tif output.pdf)
Since convert tool works well, i suppose my problem lies in Magick++

I'm using ImageMagick-6.8.4-9 in Windows

Re: Convert multi page tiff file to pdf

Posted: 2013-04-15T07:38:22-07:00
by magick
To read a multipage PDF, use readImages().

Re: Convert multi page tiff file to pdf

Posted: 2013-04-16T01:10:23-07:00
by kromm
Thanks for the answer.
This seems to be the correct way to convert a multi page tiff to pdf:

Code: Select all

Magick::InitializeMagick(argv[0]);
std::list<Magick::Image> images; 
readImages(&images, argv[1]); 
writeImages(images.begin(), images.end(), argv[2]);
The filename in argv[2] must have a .pdf extension.
Note that this is not efficient in terms of time and memory, because it seems to load the whole file in memory and then writes it to disk.
If i come up with something more efficient i will post it here.