1 //========================================================================
5 // Copyright 1996 Derek B. Noonburg
7 //========================================================================
10 #pragma implementation
24 #include "OutputDev.h"
31 //------------------------------------------------------------------------
33 #define headerSearchSize 1024 // read this many bytes at beginning of
34 // file to look for '%PDF'
36 //------------------------------------------------------------------------
38 //------------------------------------------------------------------------
40 PDFDoc::PDFDoc(GString *fileName1, GString *userPassword) {
56 if (!(file = fopen(fileName->getCString(), "rb", "ctx=stm"))) {
57 error(-1, "Couldn't open file '%s'", fileName->getCString());
61 if (!(file = fopen(fileName->getCString(), "rb"))) {
62 fileName2 = fileName->copy();
63 fileName2->lowerCase();
64 if (!(file = fopen(fileName2->getCString(), "rb"))) {
65 fileName2->upperCase();
66 if (!(file = fopen(fileName2->getCString(), "rb"))) {
67 error(-1, "Couldn't open file '%s'", fileName->getCString());
78 str = new FileStream(file, 0, -1, &obj);
80 ok = setup(userPassword);
83 PDFDoc::PDFDoc(BaseStream *str, GString *userPassword) {
91 ok = setup(userPassword);
94 GBool PDFDoc::setup(GString *userPassword) {
101 xref = new XRef(str, userPassword);
103 error(-1, "Couldn't read xref table");
108 catalog = new Catalog(xref->getCatalog(&catObj));
110 if (!catalog->isOk()) {
111 error(-1, "Couldn't read page catalog");
140 // Check for a PDF header on this stream. Skip past some garbage
142 void PDFDoc::checkHeader() {
143 char hdrBuf[headerSearchSize+1];
148 for (i = 0; i < headerSearchSize; ++i) {
149 hdrBuf[i] = str->getChar();
151 hdrBuf[headerSearchSize] = '\0';
152 for (i = 0; i < headerSearchSize - 5; ++i) {
153 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
157 if (i >= headerSearchSize - 5) {
158 error(-1, "May not be a PDF file (continuing anyway)");
162 p = strtok(&hdrBuf[i+5], " \t\n\r");
163 pdfVersion = atof(p);
164 if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
165 pdfVersion > supportedPDFVersionNum + 0.0001) {
166 error(-1, "PDF version %s -- xpdf supports version %s"
167 " (continuing anyway)", p, supportedPDFVersionStr);
171 void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
172 int rotate, GBool doLinks) {
176 printf("***** page %d *****\n", page);
178 p = catalog->getPage(page);
184 p->display(out, zoom, rotate, links, catalog);
186 p->display(out, zoom, rotate, NULL, catalog);
190 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
191 int zoom, int rotate, GBool doLinks) {
194 for (page = firstPage; page <= lastPage; ++page) {
195 displayPage(out, page, zoom, rotate, doLinks);
199 GBool PDFDoc::isLinearized() {
201 Object obj1, obj2, obj3, obj4, obj5;
206 parser = new Parser(new Lexer(str->makeSubStream(str->getStart(),
208 parser->getObj(&obj1);
209 parser->getObj(&obj2);
210 parser->getObj(&obj3);
211 parser->getObj(&obj4);
212 if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
214 obj4.dictLookup("Linearized", &obj5);
215 if (obj5.isNum() && obj5.getNum() > 0) {
228 GBool PDFDoc::saveAs(GString *name) {
232 if (!(f = fopen(name->getCString(), "wb"))) {
233 error(-1, "Couldn't open file '%s'", name->getCString());
237 while ((c = str->getChar()) != EOF) {
245 void PDFDoc::getLinks(Page *page) {
248 links = new Links(page->getAnnots(&obj), catalog->getBaseURI());