1 //========================================================================
5 // Copyright 1996-2002 Glyph & Cog, LLC
7 //========================================================================
10 #pragma implementation
26 #include "OutputDev.h"
28 #include "ErrorCodes.h"
33 //------------------------------------------------------------------------
35 #define headerSearchSize 1024 // read this many bytes at beginning of
36 // file to look for '%PDF'
38 //------------------------------------------------------------------------
40 //------------------------------------------------------------------------
42 PDFDoc::PDFDoc(GString *fileNameA, GString *ownerPassword,
43 GString *userPassword, GBool printCommandsA) {
55 printCommands = printCommandsA;
61 if (!(file = fopen(fileName->getCString(), "rb", "ctx=stm"))) {
62 error(-1, "Couldn't open file '%s'", fileName->getCString());
63 errCode = errOpenFile;
67 if (!(file = fopen(fileName->getCString(), "rb"))) {
68 fileName2 = fileName->copy();
69 fileName2->lowerCase();
70 if (!(file = fopen(fileName2->getCString(), "rb"))) {
71 fileName2->upperCase();
72 if (!(file = fopen(fileName2->getCString(), "rb"))) {
73 error(-1, "Couldn't open file '%s'", fileName->getCString());
75 errCode = errOpenFile;
85 str = new FileStream(file, 0, gFalse, 0, &obj);
87 ok = setup(ownerPassword, userPassword);
90 PDFDoc::PDFDoc(BaseStream *strA, GString *ownerPassword,
91 GString *userPassword, GBool printCommandsA) {
100 printCommands = printCommandsA;
101 ok = setup(ownerPassword, userPassword);
104 GBool PDFDoc::setup(GString *ownerPassword, GString *userPassword) {
109 xref = new XRef(str, ownerPassword, userPassword);
111 error(-1, "Couldn't read xref table");
112 errCode = xref->getErrorCode();
117 catalog = new Catalog(xref, printCommands);
118 if (!catalog->isOk()) {
119 error(-1, "Couldn't read page catalog");
120 errCode = errBadCatalog;
149 // Check for a PDF header on this stream. Skip past some garbage
151 void PDFDoc::checkHeader() {
152 char hdrBuf[headerSearchSize+1];
157 for (i = 0; i < headerSearchSize; ++i) {
158 hdrBuf[i] = str->getChar();
160 hdrBuf[headerSearchSize] = '\0';
161 for (i = 0; i < headerSearchSize - 5; ++i) {
162 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
166 if (i >= headerSearchSize - 5) {
167 error(-1, "May not be a PDF file (continuing anyway)");
171 p = strtok(&hdrBuf[i+5], " \t\n\r");
173 char *theLocale = setlocale(LC_NUMERIC, "C");
174 pdfVersion = atof(p);
175 setlocale(LC_NUMERIC, theLocale);
177 if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
178 pdfVersion > supportedPDFVersionNum + 0.0001) {
179 error(-1, "PDF version %s -- xpdf supports version %s"
180 " (continuing anyway)", p, supportedPDFVersionStr);
184 void PDFDoc::displayPage(OutputDev *out, int page, double zoom,
185 int rotate, GBool doLinks) {
189 printf("***** page %d *****\n", page);
191 p = catalog->getPage(page);
198 p->display(out, zoom, rotate, links, catalog);
200 p->display(out, zoom, rotate, NULL, catalog);
204 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
205 int zoom, int rotate, GBool doLinks) {
208 for (page = firstPage; page <= lastPage; ++page) {
209 displayPage(out, page, zoom, rotate, doLinks);
213 GBool PDFDoc::isLinearized() {
215 Object obj1, obj2, obj3, obj4, obj5;
220 parser = new Parser(xref,
222 str->makeSubStream(str->getStart(), gFalse, 0, &obj1)));
223 parser->getObj(&obj1);
224 parser->getObj(&obj2);
225 parser->getObj(&obj3);
226 parser->getObj(&obj4);
227 if (obj1.isInt() && obj2.isInt() && obj3.isCmd("obj") &&
229 obj4.dictLookup("Linearized", &obj5);
230 if (obj5.isNum() && obj5.getNum() > 0) {
243 GBool PDFDoc::saveAs(GString *name) {
247 if (!(f = fopen(name->getCString(), "wb"))) {
248 error(-1, "Couldn't open file '%s'", name->getCString());
252 while ((c = str->getChar()) != EOF) {
260 void PDFDoc::getLinks(Page *page) {
263 links = new Links(page->getAnnots(&obj), catalog->getBaseURI());