Convert multi-page PDF to multi-page TIFF

Magick++ is an object-oriented C++ interface to ImageMagick. Use this forum to discuss, make suggestions about, or report bugs concerning Magick++.
Post Reply
bnr
Posts: 2
Joined: 2016-12-21T08:26:19-07:00
Authentication code: 1151

Convert multi-page PDF to multi-page TIFF

Post by bnr »

Hello, I'm trying to convert a multi-page PDF to a multi-page TIFF.

It works fine when using a single page PDF, but when trying to convert multi-page PDFs only the first page gets converted.

My C++ Code:

Code: Select all

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "boost/program_options.hpp"
#include "Magick++.h"
#include <boost/filesystem/convenience.hpp>
#include <boost/format.hpp>

using namespace Magick;


int main(int argc, char* argv[])
{
	namespace po = boost::program_options;
	po::options_description desc("Optionen");
	desc.add_options()
		("help,h", "Help")
		("inf", po::value<std::string>(), "Eingabedatei")
		("outf", po::value<std::string>(), "Ausgabedatei")
	;

	po::variables_map vm;
	po::store(po::parse_command_line(argc, argv, desc), vm);
	po::notify(vm);

	if (vm.count("help")) {
		std::cout << desc << "\n";
		return 1;
	}

	if (!vm.count("inf") && !vm.count("outf")) {
		std::cout << "Ein- und Ausgabedatei müssen gesetzt sein.";
	}

	//Parameter passen, kann verarbeitet werden
	std::string infilePath = vm["inf"].as<std::string>();
	std::string outfilePath = vm["outf"].as<std::string>();

	//Datei existiert nicht
	if (!std::ifstream(infilePath))
	{
		std::cout << "ERROR: Infile existiert nicht.";
		return 1;
	}

	Image infile;
	std::string outfileBasename = outfilePath;

	try {
		outfileBasename = boost::filesystem::change_extension(outfilePath, "").string();
	}
	catch (Exception &e) {
		std::cout << "keine Extension angegeben\n";
	}

	try {
		infile.density(200);
		infile.read(infilePath);
		char* extPath = new char[128];

		sprintf_s(extPath, 128, "%s_0%%04d.tif", outfileBasename.c_str());
		
		infile.write(extPath);

		delete[] extPath;
	}
	catch (Exception &e) {
		std::cout << "Exception: \n";
		std::cout << e.what();
		return 1;	
	}

	return 0;
}
Example multi-page PDF: https://www.dropbox.com/s/l6jzdqre9cjxe ... e.pdf?dl=1
Example single-page PDF: https://www.dropbox.com/s/3sowi7698koek ... e.pdf?dl=1

I'm running W8.1 Enterprise x64, VS2013, Ghostscript 9.20 x32 and IM 7.0.4-0, both compiled from source.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Convert multi-page PDF to multi-page TIFF

Post by snibgo »

The datatype "Image" stores only a single image. See the start of http://www.imagemagick.org/Magick++/Image++.html :
Image is the primary object in Magick++ and represents a single image frame (see design ). The STL interface must be used to operate on image sequences or images (e.g. of format GIF, TIFF, MIFF, Postscript, & MNG) which are comprized of multiple image frames.
snibgo's IM pages: im.snibgo.com
bnr
Posts: 2
Joined: 2016-12-21T08:26:19-07:00
Authentication code: 1151

Re: Convert multi-page PDF to multi-page TIFF

Post by bnr »

Thank you, I was able to resolve this.
Post Reply