20#include "xpdf_config.h"
21#include "GlobalParams.h"
29#include "ErrorCodes.h"
32#ifndef DISABLE_OUTLINE
39#define headerSearchSize 1024
46PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
47 GString *userPassword) {
49 GString *fileName1, *fileName2;
59#ifndef DISABLE_OUTLINE
70 if (!(file = fopen(fileName1->getCString(), "rb", "ctx=stm"))) {
71 error(-1, "Couldn't open file '%s'", fileName1->getCString());
72 errCode = errOpenFile;
76 if (!(file = fopen(fileName1->getCString(), "rb"))) {
77 fileName2 = fileName->copy();
78 fileName2->lowerCase();
79 if (!(file = fopen(fileName2->getCString(), "rb"))) {
80 fileName2->upperCase();
81 if (!(file = fopen(fileName2->getCString(), "rb"))) {
82 error(-1, "Couldn't open file '%s'", fileName->getCString());
84 errCode = errOpenFile;
94 str = new FileStream(file, 0, gFalse, 0, &obj);
96 ok = setup(ownerPassword, userPassword);
99PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
100 GString *userPassword) {
109#ifndef DISABLE_OUTLINE
112 ok = setup(ownerPassword, userPassword);
115GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
120 xref = new XRef(str, ownerPassword, userPassword);
122 error(-1, "Couldn't read xref table");
123 errCode = xref->getErrorCode();
128 catalog = new Catalog(xref);
129 if (!catalog->isOk()) {
130 error(-1, "Couldn't read page catalog");
131 errCode = errBadCatalog;
135#ifndef DISABLE_OUTLINE
137 outline = new Outline(catalog->getOutline(), xref);
145#ifndef DISABLE_OUTLINE
160void PDFDoc::checkHeader() {
161 char hdrBuf[headerSearchSize+1];
166 for (i = 0; i < headerSearchSize; ++i) {
167 hdrBuf[i] = str->getChar();
169 hdrBuf[headerSearchSize] = '\0';
170 for (i = 0; i < headerSearchSize - 5; ++i) {
171 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
175 if (i >= headerSearchSize - 5) {
176 error(-1, "May not be a PDF file (continuing anyway)");
180 p = strtok(&hdrBuf[i+5], " \t\n\r");
181 pdfVersion = atof(p);
182 if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
183 pdfVersion > supportedPDFVersionNum + 0.0001) {
184 error(-1, "PDF version %s -- xpdf supports version %s"
185 " (continuing anyway)", p, supportedPDFVersionStr);
189void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
190 int rotate, GBool doLinks,
191 GBool (*abortCheckCbk)( void *data),
192 void *abortCheckCbkData) {
195 if (globalParams->getPrintCommands()) {
196 printf( "***** page %d *****\n", page);
198 p = catalog->getPage(page);
204 p->display(out, zoom, rotate, links, catalog,
205 abortCheckCbk, abortCheckCbkData);
207 p->display(out, zoom, rotate, NULL, catalog,
208 abortCheckCbk, abortCheckCbkData);
212void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
213 int zoom, int rotate, GBool doLinks,
214 GBool (*abortCheckCbk)( void *data),
215 void *abortCheckCbkData) {
218 for (page = firstPage; page <= lastPage; ++page) {
219 displayPage(out, page, zoom, rotate, doLinks,
220 abortCheckCbk, abortCheckCbkData);
224void PDFDoc::displayPageSlice(OutputDev *out, int page, double zoom,
225 int rotate, int sliceX, int sliceY,
226 int sliceW, int sliceH,
227 GBool (*abortCheckCbk)( void *data),
228 void *abortCheckCbkData) {
231 p = catalog->getPage(page);
232 p->displaySlice(out, zoom, rotate, sliceX, sliceY, sliceW, sliceH,
233 NULL, catalog, abortCheckCbk, abortCheckCbkData);
236GBool PDFDoc::isLinearized() {
238 Object obj1, obj2, obj3, obj4, obj5;
243 parser = new Parser(xref,
245 str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
246 parser->getObj(&obj1);
247 parser->getObj(&obj2);
248 parser->getObj(&obj3);
249 parser->getObj(&obj4);
250 if (obj1.isInt() && obj2.isInt() && obj3.isCmd( "obj") &&
252 obj4.dictLookup( "Linearized", &obj5);
253 if (obj5.isNum() && obj5.getNum() > 0) {
266GBool PDFDoc::saveAs(GString *name) {
270 if (!(f = fopen(name->getCString(), "wb"))) {
271 error(-1, "Couldn't open file '%s'", name->getCString());
275 while ((c = str->getChar()) != EOF) {
283void PDFDoc::getLinks(Page *page) {
286 links = new Links(page->getAnnots(&obj), catalog->getBaseURI());
|