2
1
mirror of https://github.com/qpdf/qpdf.git synced 2025-01-03 07:12:28 +00:00

C linearization example

git-svn-id: svn+q:///qpdf/trunk@739 71b93d88-0707-0410-a8cf-f5a4172ac649
This commit is contained in:
Jay Berkenbilt 2009-09-27 20:59:16 +00:00
parent 6bdac26369
commit d6d36b6ced
6 changed files with 202 additions and 4 deletions

View File

@ -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)),$$@)))

74
examples/pdf-linearize.c Normal file
View File

@ -0,0 +1,74 @@
/*
* 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);
}
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;
}

View File

@ -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";
}

View File

@ -0,0 +1,5 @@
checking a.pdf
PDF Version: 1.3
File is not encrypted
File is linearized
No errors found

View File

@ -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

View File

@ -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 \