From d6d36b6ced682318b47d489bfb7ba56a1db382de Mon Sep 17 00:00:00 2001 From: Jay Berkenbilt Date: Sun, 27 Sep 2009 20:59:16 +0000 Subject: C linearization example git-svn-id: svn+q:///qpdf/trunk@739 71b93d88-0707-0410-a8cf-f5a4172ac649 --- examples/build.mk | 14 +++++-- examples/pdf-linearize.c | 74 +++++++++++++++++++++++++++++++++++ examples/qtest/linearize.test | 32 +++++++++++++++ examples/qtest/linearize/check.out | 5 +++ examples/qtest/linearize/input.pdf | 79 ++++++++++++++++++++++++++++++++++++++ qpdf/build.mk | 2 +- 6 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 examples/pdf-linearize.c create mode 100644 examples/qtest/linearize.test create mode 100644 examples/qtest/linearize/check.out create mode 100644 examples/qtest/linearize/input.pdf diff --git a/examples/build.mk b/examples/build.mk index eab2744c..66c498e1 100644 --- a/examples/build.mk +++ b/examples/build.mk @@ -1,6 +1,7 @@ BINS_examples = pdf-bookmarks pdf-mod-info pdf-npages +CBINS_examples = pdf-linearize -TARGETS_examples = $(foreach B,$(BINS_examples),examples/$(OUTPUT_DIR)/$(B)) +TARGETS_examples = $(foreach B,$(BINS_examples) $(CBINS_examples),examples/$(OUTPUT_DIR)/$(B)) $(TARGETS_examples): $(TARGETS_qpdf) @@ -13,14 +14,21 @@ TC_SRCS_examples = $(wildcard examples/*.cc) $(foreach B,$(BINS_examples),$(eval \ OBJS_$(B) = $(call src_to_obj,examples/$(B).cc))) +$(foreach B,$(CBINS_examples),$(eval \ + OBJS_$(B) = $(call c_src_to_obj,examples/$(B).c))) + ifeq ($(GENDEPS),1) --include $(foreach B,$(BINS_examples),$(call obj_to_dep,$(OBJS_$(B)))) +-include $(foreach B,$(BINS_examples) $(CBINS_examples),$(call obj_to_dep,$(OBJS_$(B)))) endif $(foreach B,$(BINS_examples),$(eval \ $(OBJS_$(B)): examples/$(OUTPUT_DIR)/%.o: examples/$(B).cc ; \ $(call compile,examples/$(B).cc,$(INCLUDES_examples)))) -$(foreach B,$(BINS_examples),$(eval \ +$(foreach B,$(CBINS_examples),$(eval \ + $(OBJS_$(B)): examples/$(OUTPUT_DIR)/%.o: examples/$(B).c ; \ + $(call c_compile,examples/$(B).c,$(INCLUDES_examples)))) + +$(foreach B,$(BINS_examples) $(CBINS_examples),$(eval \ examples/$(OUTPUT_DIR)/$(B): $(OBJS_$(B)) ; \ $(call makebin,$(OBJS_$(B)),$$@))) diff --git a/examples/pdf-linearize.c b/examples/pdf-linearize.c new file mode 100644 index 00000000..ca49a4d9 --- /dev/null +++ b/examples/pdf-linearize.c @@ -0,0 +1,74 @@ +/* + * This is an example program to linearize a PDF file using the C API. + */ + +#include +#include +#include +#include + +static char const* whoami = 0; + +static void usage() +{ + fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami); + exit(2); +} + +int main(int argc, char* argv[]) +{ + char* infile = argv[1]; + char* password = argv[2]; + char* outfile = argv[3]; + qpdf_data qpdf = qpdf_init(); + int warnings = 0; + int errors = 0; + char* p = 0; + + if ((p = strrchr(argv[0], '/')) != NULL) + { + whoami = p + 1; + } + else if ((p = strrchr(argv[0], '\\')) != NULL) + { + whoami = p + 1; + } + else + { + whoami = argv[0]; + } + + if (argc != 4) + { + usage(); + } + + if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) && + ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0)) + { + qpdf_set_static_ID(qpdf, QPDF_TRUE); + qpdf_set_linearization(qpdf, QPDF_TRUE); + qpdf_write(qpdf); + } + while (qpdf_more_warnings(qpdf)) + { + warnings = 1; + printf("warning: %s\n", qpdf_next_warning(qpdf)); + } + while (qpdf_more_errors(qpdf)) + { + errors = 1; + printf("error: %s\n", qpdf_next_error(qpdf)); + } + qpdf_cleanup(&qpdf); + if (errors) + { + return 2; + } + else if (warnings) + { + return 3; + } + + return 0; +} diff --git a/examples/qtest/linearize.test b/examples/qtest/linearize.test new file mode 100644 index 00000000..700cdca3 --- /dev/null +++ b/examples/qtest/linearize.test @@ -0,0 +1,32 @@ +#!/usr/bin/env perl +require 5.008; +BEGIN { $^W = 1; } +use strict; + +chdir("linearize") or die "chdir testdir failed: $!\n"; + +require TestDriver; + +cleanup(); + +my $td = new TestDriver('linearize'); + +my $qpdf = $ENV{'QPDF_BIN'} or die; + +$td->runtest("linearize", + {$td->COMMAND => "pdf-linearize input.pdf '' a.pdf"}, + {$td->STRING => "", $td->EXIT_STATUS => 0}); + +$td->runtest("check", + {$td->COMMAND => "$qpdf --check a.pdf"}, + {$td->FILE => "check.out", $td->EXIT_STATUS => 0}, + $td->NORMALIZE_NEWLINES); + +cleanup(); + +$td->report(2); + +sub cleanup +{ + unlink "a.pdf"; +} diff --git a/examples/qtest/linearize/check.out b/examples/qtest/linearize/check.out new file mode 100644 index 00000000..c2063a41 --- /dev/null +++ b/examples/qtest/linearize/check.out @@ -0,0 +1,5 @@ +checking a.pdf +PDF Version: 1.3 +File is not encrypted +File is linearized +No errors found diff --git a/examples/qtest/linearize/input.pdf b/examples/qtest/linearize/input.pdf new file mode 100644 index 00000000..a7e01f91 --- /dev/null +++ b/examples/qtest/linearize/input.pdf @@ -0,0 +1,79 @@ +%PDF-1.3 +1 0 obj +<< + /Type /Catalog + /Pages 2 0 R +>> +endobj + +2 0 obj +<< + /Type /Pages + /Kids [ + 3 0 R + ] + /Count 1 +>> +endobj + +3 0 obj +<< + /Type /Page + /Parent 2 0 R + /MediaBox [0 0 612 792] + /Contents 4 0 R + /Resources << + /ProcSet 5 0 R + /Font << + /F1 6 0 R + >> + >> +>> +endobj + +4 0 obj +<< + /Length 44 +>> +stream +BT + /F1 24 Tf + 72 720 Td + (Potato) Tj +ET +endstream +endobj + +5 0 obj +[ + /PDF + /Text +] +endobj + +6 0 obj +<< + /Type /Font + /Subtype /Type1 + /Name /F1 + /BaseFont /Helvetica + /Encoding /WinAnsiEncoding +>> +endobj + +xref +0 7 +0000000000 65535 f +0000000009 00000 n +0000000063 00000 n +0000000135 00000 n +0000000307 00000 n +0000000403 00000 n +0000000438 00000 n +trailer << + /Size 7 + /Root 1 0 R +>> +startxref +556 +%%EOF diff --git a/qpdf/build.mk b/qpdf/build.mk index 1f439bb4..6e748c26 100644 --- a/qpdf/build.mk +++ b/qpdf/build.mk @@ -17,7 +17,7 @@ $(foreach B,$(CBINS_qpdf),$(eval \ OBJS_$(B) = $(call c_src_to_obj,qpdf/$(B).c))) ifeq ($(GENDEPS),1) --include $(foreach B,$(BINS_qpdf),$(call obj_to_dep,$(OBJS_$(B)))) +-include $(foreach B,$(BINS_qpdf) $(CBINS_qpdf),$(call obj_to_dep,$(OBJS_$(B)))) endif $(foreach B,$(BINS_qpdf),$(eval \ -- cgit v1.2.3-54-g00ecf