2009-09-27 20:59:16 +00:00
|
|
|
/*
|
|
|
|
* This is an example program to linearize a PDF file using the C API.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <qpdf/qpdf-c.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
static char const* whoami = 0;
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami);
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
2020-12-20 19:43:08 +00:00
|
|
|
static void
|
|
|
|
write_progress(int percent, void* data)
|
|
|
|
{
|
|
|
|
printf("%s progress: %d%%\n", (char const*)(data), percent);
|
|
|
|
}
|
|
|
|
|
2009-09-27 20:59:16 +00:00
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2010-10-04 15:16:32 +00:00
|
|
|
char* infile = NULL;
|
|
|
|
char* password = NULL;
|
|
|
|
char* outfile = NULL;
|
2009-09-27 20:59:16 +00:00
|
|
|
qpdf_data qpdf = qpdf_init();
|
|
|
|
int warnings = 0;
|
|
|
|
int errors = 0;
|
|
|
|
char* p = 0;
|
|
|
|
|
|
|
|
if ((p = strrchr(argv[0], '/')) != NULL) {
|
2022-02-08 14:18:08 +00:00
|
|
|
whoami = p + 1;
|
2009-09-27 20:59:16 +00:00
|
|
|
} else if ((p = strrchr(argv[0], '\\')) != NULL) {
|
2022-02-08 14:18:08 +00:00
|
|
|
whoami = p + 1;
|
2009-09-27 20:59:16 +00:00
|
|
|
} else {
|
2022-02-08 14:18:08 +00:00
|
|
|
whoami = argv[0];
|
2009-09-27 20:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (argc != 4) {
|
2022-02-08 14:18:08 +00:00
|
|
|
usage();
|
2009-09-27 20:59:16 +00:00
|
|
|
}
|
|
|
|
|
2010-10-04 15:16:32 +00:00
|
|
|
infile = argv[1];
|
|
|
|
password = argv[2];
|
|
|
|
outfile = argv[3];
|
|
|
|
|
2009-09-27 20:59:16 +00:00
|
|
|
if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) &&
|
2022-02-08 14:18:08 +00:00
|
|
|
((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) {
|
2015-11-01 21:39:15 +00:00
|
|
|
/* Use static ID for testing only. For production, a non-static ID is used. See also
|
|
|
|
* qpdf_set_deterministic_ID. */
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_set_static_ID(qpdf, QPDF_TRUE); /* for testing only */
|
|
|
|
qpdf_set_linearization(qpdf, QPDF_TRUE);
|
2020-12-20 19:43:08 +00:00
|
|
|
qpdf_register_progress_reporter(qpdf, write_progress, infile);
|
2022-02-08 14:18:08 +00:00
|
|
|
qpdf_write(qpdf);
|
2009-09-27 20:59:16 +00:00
|
|
|
}
|
|
|
|
while (qpdf_more_warnings(qpdf)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
warnings = 1;
|
|
|
|
printf("warning: %s\n", qpdf_get_error_full_text(qpdf, qpdf_next_warning(qpdf)));
|
2009-09-27 20:59:16 +00:00
|
|
|
}
|
2009-10-23 15:27:30 +00:00
|
|
|
if (qpdf_has_error(qpdf)) {
|
2022-02-08 14:18:08 +00:00
|
|
|
errors = 1;
|
|
|
|
printf("error: %s\n", qpdf_get_error_full_text(qpdf, qpdf_get_error(qpdf)));
|
2009-09-27 20:59:16 +00:00
|
|
|
}
|
|
|
|
qpdf_cleanup(&qpdf);
|
|
|
|
if (errors) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return 2;
|
2009-09-27 20:59:16 +00:00
|
|
|
} else if (warnings) {
|
2022-02-08 14:18:08 +00:00
|
|
|
return 3;
|
2009-09-27 20:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|