1
//========================================================================
5
// Copyright 1996-2002 Glyph & Cog, LLC
7
//========================================================================
11
#ifdef USE_GCC_PRAGMAS
12
#pragma implementation
22
#include "globalparams.h"
28
#include "outputdev.h"
30
#include "errorcodes.h"
33
#ifndef DISABLE_OUTLINE
38
//------------------------------------------------------------------------
40
#define headerSearchSize 1024 // read this many bytes at beginning of
41
// file to look for '%PDF'
43
//------------------------------------------------------------------------
45
//------------------------------------------------------------------------
47
PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
48
GString *userPassword) {
50
GString *fileName1, *fileName2;
60
#ifndef DISABLE_OUTLINE
71
if (!(file = ocfopen(fileName1->getCString(), "rb", "ctx=stm"))) {
72
error(-1, "Couldn't open file '%s'", fileName1->getCString());
73
errCode = errOpenFile;
77
if (!(file = ocfopen(fileName1->getCString(), "rb"))) {
78
fileName2 = fileName->copy();
79
fileName2->lowerCase();
80
if (!(file = ocfopen(fileName2->getCString(), "rb"))) {
81
fileName2->upperCase();
82
if (!(file = ocfopen(fileName2->getCString(), "rb"))) {
83
error(-1, "Couldn't open file '%s'", fileName->getCString());
85
errCode = errOpenFile;
95
str = new FileStream(file, 0, gFalse, 0, &obj);
97
ok = setup(ownerPassword, userPassword);
100
PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
101
GString *userPassword) {
110
#ifndef DISABLE_OUTLINE
113
ok = setup(ownerPassword, userPassword);
116
GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
121
xref = new XRef(str, ownerPassword, userPassword);
123
error(-1, "Couldn't read xref table");
124
errCode = xref->getErrorCode();
129
catalog = new Catalog(xref);
130
if (!catalog->isOk()) {
131
error(-1, "Couldn't read page catalog");
132
errCode = errBadCatalog;
136
#ifndef DISABLE_OUTLINE
138
outline = new Outline(catalog->getOutline(), xref);
146
#ifndef DISABLE_OUTLINE
171
// Check for a PDF header on this stream. Skip past some garbage
173
void PDFDoc::checkHeader() {
174
char hdrBuf[headerSearchSize+1];
179
for (i = 0; i < headerSearchSize; ++i) {
180
hdrBuf[i] = str->getChar();
182
hdrBuf[headerSearchSize] = '\0';
183
for (i = 0; i < headerSearchSize - 5; ++i) {
184
if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
188
if (i >= headerSearchSize - 5) {
189
error(-1, "May not be a PDF file (continuing anyway)");
193
p = strtok(&hdrBuf[i+5], " \t\n\r");
194
pdfVersion = atof(p);
195
if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
196
pdfVersion > supportedPDFVersionNum + 0.0001) {
197
error(-1, "PDF version %s -- xpdf supports version %s"
198
" (continuing anyway)", p, supportedPDFVersionStr);
202
void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
203
int rotate, GBool doLinks,
204
GBool (*abortCheckCbk)(void *data),
205
void *abortCheckCbkData) {
208
if (globalParams->getPrintCommands()) {
209
printf("***** page %d *****\n", page);
211
p = catalog->getPage(page);
217
p->display(out, zoom, rotate, links, catalog,
218
abortCheckCbk, abortCheckCbkData);
220
p->display(out, zoom, rotate, NULL, catalog,
221
abortCheckCbk, abortCheckCbkData);
225
void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
226
int zoom, int rotate, GBool doLinks,
227
GBool (*abortCheckCbk)(void *data),
228
void *abortCheckCbkData) {
231
for (page = firstPage; page <= lastPage; ++page) {
232
displayPage(out, page, zoom, rotate, doLinks,
233
abortCheckCbk, abortCheckCbkData);
237
void PDFDoc::displayPageSlice(OutputDev *out, int page, double zoom,
238
int rotate, int sliceX, int sliceY,
239
int sliceW, int sliceH,
240
GBool (*abortCheckCbk)(void *data),
241
void *abortCheckCbkData) {
244
p = catalog->getPage(page);
245
p->displaySlice(out, zoom, rotate, sliceX, sliceY, sliceW, sliceH,
246
NULL, catalog, abortCheckCbk, abortCheckCbkData);
249
GBool PDFDoc::isLinearized() {
251
Object obj1, obj2, obj3, obj4, obj5;
256
parser = new Parser(xref,
258
str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
259
parser->getObj(&obj1);
260
parser->getObj(&obj2);
261
parser->getObj(&obj3);
262
parser->getObj(&obj4);
263
if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
265
obj4.dictLookup("Linearized", &obj5);
266
if (obj5.isNum() && obj5.getNum() > 0) {
279
GBool PDFDoc::saveAs(GString *name) {
283
if (!(f = ocfopen(name->getCString(), "wb"))) {
284
error(-1, "Couldn't open file '%s'", name->getCString());
288
while ((c = str->getChar()) != EOF) {
296
void PDFDoc::getLinks(Page *page) {
299
links = new Links(page->getAnnots(&obj), catalog->getBaseURI());