Commit d6d36b6ced682318b47d489bfb7ba56a1db382de

Authored by Jay Berkenbilt
1 parent 6bdac263

C linearization example

git-svn-id: svn+q:///qpdf/trunk@739 71b93d88-0707-0410-a8cf-f5a4172ac649
examples/build.mk
1 1 BINS_examples = pdf-bookmarks pdf-mod-info pdf-npages
  2 +CBINS_examples = pdf-linearize
2 3  
3   -TARGETS_examples = $(foreach B,$(BINS_examples),examples/$(OUTPUT_DIR)/$(B))
  4 +TARGETS_examples = $(foreach B,$(BINS_examples) $(CBINS_examples),examples/$(OUTPUT_DIR)/$(B))
4 5  
5 6 $(TARGETS_examples): $(TARGETS_qpdf)
6 7  
... ... @@ -13,14 +14,21 @@ TC_SRCS_examples = $(wildcard examples/*.cc)
13 14 $(foreach B,$(BINS_examples),$(eval \
14 15 OBJS_$(B) = $(call src_to_obj,examples/$(B).cc)))
15 16  
  17 +$(foreach B,$(CBINS_examples),$(eval \
  18 + OBJS_$(B) = $(call c_src_to_obj,examples/$(B).c)))
  19 +
16 20 ifeq ($(GENDEPS),1)
17   --include $(foreach B,$(BINS_examples),$(call obj_to_dep,$(OBJS_$(B))))
  21 +-include $(foreach B,$(BINS_examples) $(CBINS_examples),$(call obj_to_dep,$(OBJS_$(B))))
18 22 endif
19 23  
20 24 $(foreach B,$(BINS_examples),$(eval \
21 25 $(OBJS_$(B)): examples/$(OUTPUT_DIR)/%.o: examples/$(B).cc ; \
22 26 $(call compile,examples/$(B).cc,$(INCLUDES_examples))))
23 27  
24   -$(foreach B,$(BINS_examples),$(eval \
  28 +$(foreach B,$(CBINS_examples),$(eval \
  29 + $(OBJS_$(B)): examples/$(OUTPUT_DIR)/%.o: examples/$(B).c ; \
  30 + $(call c_compile,examples/$(B).c,$(INCLUDES_examples))))
  31 +
  32 +$(foreach B,$(BINS_examples) $(CBINS_examples),$(eval \
25 33 examples/$(OUTPUT_DIR)/$(B): $(OBJS_$(B)) ; \
26 34 $(call makebin,$(OBJS_$(B)),$$@)))
... ...
examples/pdf-linearize.c 0 โ†’ 100644
  1 +/*
  2 + * This is an example program to linearize a PDF file using the C API.
  3 + */
  4 +
  5 +#include <qpdf/qpdf-c.h>
  6 +#include <stdio.h>
  7 +#include <stdlib.h>
  8 +#include <string.h>
  9 +
  10 +static char const* whoami = 0;
  11 +
  12 +static void usage()
  13 +{
  14 + fprintf(stderr, "Usage: %s infile infile-password outfile\n", whoami);
  15 + exit(2);
  16 +}
  17 +
  18 +int main(int argc, char* argv[])
  19 +{
  20 + char* infile = argv[1];
  21 + char* password = argv[2];
  22 + char* outfile = argv[3];
  23 + qpdf_data qpdf = qpdf_init();
  24 + int warnings = 0;
  25 + int errors = 0;
  26 + char* p = 0;
  27 +
  28 + if ((p = strrchr(argv[0], '/')) != NULL)
  29 + {
  30 + whoami = p + 1;
  31 + }
  32 + else if ((p = strrchr(argv[0], '\\')) != NULL)
  33 + {
  34 + whoami = p + 1;
  35 + }
  36 + else
  37 + {
  38 + whoami = argv[0];
  39 + }
  40 +
  41 + if (argc != 4)
  42 + {
  43 + usage();
  44 + }
  45 +
  46 + if (((qpdf_read(qpdf, infile, password) & QPDF_ERRORS) == 0) &&
  47 + ((qpdf_init_write(qpdf, outfile) & QPDF_ERRORS) == 0))
  48 + {
  49 + qpdf_set_static_ID(qpdf, QPDF_TRUE);
  50 + qpdf_set_linearization(qpdf, QPDF_TRUE);
  51 + qpdf_write(qpdf);
  52 + }
  53 + while (qpdf_more_warnings(qpdf))
  54 + {
  55 + warnings = 1;
  56 + printf("warning: %s\n", qpdf_next_warning(qpdf));
  57 + }
  58 + while (qpdf_more_errors(qpdf))
  59 + {
  60 + errors = 1;
  61 + printf("error: %s\n", qpdf_next_error(qpdf));
  62 + }
  63 + qpdf_cleanup(&qpdf);
  64 + if (errors)
  65 + {
  66 + return 2;
  67 + }
  68 + else if (warnings)
  69 + {
  70 + return 3;
  71 + }
  72 +
  73 + return 0;
  74 +}
... ...
examples/qtest/linearize.test 0 โ†’ 100644
  1 +#!/usr/bin/env perl
  2 +require 5.008;
  3 +BEGIN { $^W = 1; }
  4 +use strict;
  5 +
  6 +chdir("linearize") or die "chdir testdir failed: $!\n";
  7 +
  8 +require TestDriver;
  9 +
  10 +cleanup();
  11 +
  12 +my $td = new TestDriver('linearize');
  13 +
  14 +my $qpdf = $ENV{'QPDF_BIN'} or die;
  15 +
  16 +$td->runtest("linearize",
  17 + {$td->COMMAND => "pdf-linearize input.pdf '' a.pdf"},
  18 + {$td->STRING => "", $td->EXIT_STATUS => 0});
  19 +
  20 +$td->runtest("check",
  21 + {$td->COMMAND => "$qpdf --check a.pdf"},
  22 + {$td->FILE => "check.out", $td->EXIT_STATUS => 0},
  23 + $td->NORMALIZE_NEWLINES);
  24 +
  25 +cleanup();
  26 +
  27 +$td->report(2);
  28 +
  29 +sub cleanup
  30 +{
  31 + unlink "a.pdf";
  32 +}
... ...
examples/qtest/linearize/check.out 0 โ†’ 100644
  1 +checking a.pdf
  2 +PDF Version: 1.3
  3 +File is not encrypted
  4 +File is linearized
  5 +No errors found
... ...
examples/qtest/linearize/input.pdf 0 โ†’ 100644
  1 +%PDF-1.3
  2 +1 0 obj
  3 +<<
  4 + /Type /Catalog
  5 + /Pages 2 0 R
  6 +>>
  7 +endobj
  8 +
  9 +2 0 obj
  10 +<<
  11 + /Type /Pages
  12 + /Kids [
  13 + 3 0 R
  14 + ]
  15 + /Count 1
  16 +>>
  17 +endobj
  18 +
  19 +3 0 obj
  20 +<<
  21 + /Type /Page
  22 + /Parent 2 0 R
  23 + /MediaBox [0 0 612 792]
  24 + /Contents 4 0 R
  25 + /Resources <<
  26 + /ProcSet 5 0 R
  27 + /Font <<
  28 + /F1 6 0 R
  29 + >>
  30 + >>
  31 +>>
  32 +endobj
  33 +
  34 +4 0 obj
  35 +<<
  36 + /Length 44
  37 +>>
  38 +stream
  39 +BT
  40 + /F1 24 Tf
  41 + 72 720 Td
  42 + (Potato) Tj
  43 +ET
  44 +endstream
  45 +endobj
  46 +
  47 +5 0 obj
  48 +[
  49 + /PDF
  50 + /Text
  51 +]
  52 +endobj
  53 +
  54 +6 0 obj
  55 +<<
  56 + /Type /Font
  57 + /Subtype /Type1
  58 + /Name /F1
  59 + /BaseFont /Helvetica
  60 + /Encoding /WinAnsiEncoding
  61 +>>
  62 +endobj
  63 +
  64 +xref
  65 +0 7
  66 +0000000000 65535 f
  67 +0000000009 00000 n
  68 +0000000063 00000 n
  69 +0000000135 00000 n
  70 +0000000307 00000 n
  71 +0000000403 00000 n
  72 +0000000438 00000 n
  73 +trailer <<
  74 + /Size 7
  75 + /Root 1 0 R
  76 +>>
  77 +startxref
  78 +556
  79 +%%EOF
... ...
qpdf/build.mk
... ... @@ -17,7 +17,7 @@ $(foreach B,$(CBINS_qpdf),$(eval \
17 17 OBJS_$(B) = $(call c_src_to_obj,qpdf/$(B).c)))
18 18  
19 19 ifeq ($(GENDEPS),1)
20   --include $(foreach B,$(BINS_qpdf),$(call obj_to_dep,$(OBJS_$(B))))
  20 +-include $(foreach B,$(BINS_qpdf) $(CBINS_qpdf),$(call obj_to_dep,$(OBJS_$(B))))
21 21 endif
22 22  
23 23 $(foreach B,$(BINS_qpdf),$(eval \
... ...