mirror of
https://github.com/qpdf/qpdf.git
synced 2024-12-22 19:08:59 +00:00
cb769c62e5
This comment expands all tabs using an 8-character tab-width. You should ignore this commit when using git blame or use git blame -w. In the early days, I used to use tabs where possible for indentation, since emacs did this automatically. In recent years, I have switched to only using spaces, which means qpdf source code has been a mixture of spaces and tabs. I have avoided cleaning this up because of not wanting gratuitous whitespaces change to cloud the output of git blame, but I changed my mind after discussing with users who view qpdf source code in editors/IDEs that have other tab widths by default and in light of the fact that I am planning to start applying automatic code formatting soon.
123 lines
2.7 KiB
C++
123 lines
2.7 KiB
C++
#include <qpdf/QPDFJob.hh>
|
|
#include <qpdf/QUtil.hh>
|
|
#include <qpdf/QIntC.hh>
|
|
|
|
#include <iostream>
|
|
#include <cstring>
|
|
|
|
// This program is a simple demonstration of different ways to use the
|
|
// QPDFJob API.
|
|
|
|
static char const* whoami = 0;
|
|
|
|
static void usage()
|
|
{
|
|
std::cerr
|
|
<< "Usage: " << whoami << std::endl
|
|
<< "This program linearizes the first page of in.pdf to out1.pdf,"
|
|
<< " out2.pdf, and"
|
|
<< std::endl
|
|
<< " out3.pdf, each demonstrating a different way to use the"
|
|
<< " QPDFJob API"
|
|
<< std::endl;
|
|
exit(2);
|
|
}
|
|
|
|
int main(int argc, char* argv[])
|
|
{
|
|
whoami = QUtil::getWhoami(argv[0]);
|
|
|
|
// For libtool's sake....
|
|
if (strncmp(whoami, "lt-", 3) == 0)
|
|
{
|
|
whoami += 3;
|
|
}
|
|
|
|
if (argc != 1)
|
|
{
|
|
usage();
|
|
}
|
|
|
|
// The examples below all catch std::exception. Note that
|
|
// QPDFUsage can be caught separately to report on errors in using
|
|
// the API itself. For CLI, this is command-line usage. For JSON
|
|
// or the API, it would be errors from the equivalent invocation.
|
|
|
|
// Note that staticId is used for testing only.
|
|
|
|
try
|
|
{
|
|
// Use the config API
|
|
QPDFJob j;
|
|
j.config()
|
|
->inputFile("in.pdf")
|
|
->outputFile("out1.pdf")
|
|
->pages()
|
|
->pageSpec(".", "1")
|
|
->endPages()
|
|
->linearize()
|
|
->staticId() // for testing only
|
|
->checkConfiguration();
|
|
j.run();
|
|
std::cout << "out1 status: " << j.getExitCode() << std::endl;
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
return 2;
|
|
}
|
|
|
|
try
|
|
{
|
|
char const* new_argv[] = {
|
|
whoami,
|
|
"in.pdf",
|
|
"out2.pdf",
|
|
"--linearize",
|
|
"--pages",
|
|
".",
|
|
"1",
|
|
"--",
|
|
"--static-id",
|
|
nullptr
|
|
};
|
|
QPDFJob j;
|
|
j.initializeFromArgv(new_argv);
|
|
j.run();
|
|
std::cout << "out2 status: " << j.getExitCode() << std::endl;
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
return 2;
|
|
}
|
|
|
|
try
|
|
{
|
|
// Use the JSON API
|
|
QPDFJob j;
|
|
j.initializeFromJson(R"({
|
|
"inputFile": "in.pdf",
|
|
"outputFile": "out3.pdf",
|
|
"staticId": "",
|
|
"linearize": "",
|
|
"pages": [
|
|
{
|
|
"file": ".",
|
|
"range": "1"
|
|
}
|
|
]
|
|
}
|
|
)");
|
|
j.run();
|
|
std::cout << "out3 status: " << j.getExitCode() << std::endl;
|
|
}
|
|
catch (std::exception& e)
|
|
{
|
|
std::cerr << "exception: " << e.what() << std::endl;
|
|
return 2;
|
|
}
|
|
|
|
return 0;
|
|
}
|