1 //========================================================================
5 // Copyright 1996 Derek B. Noonburg
7 //========================================================================
10 #pragma implementation
24 #include "OutputDev.h"
29 //------------------------------------------------------------------------
31 #define headerSearchSize 1024 // read this many bytes at beginning of
32 // file to look for '%PDF'
34 //------------------------------------------------------------------------
36 //------------------------------------------------------------------------
38 PDFDoc::PDFDoc(GString *fileName1) {
51 if (!(file = fopen(fileName->getCString(), "rb", "ctx=stm"))) {
52 error(-1, "Couldn't open file '%s'", fileName->getCString());
56 if (!(file = fopen(fileName->getCString(), "rb"))) {
57 fileName2 = fileName->copy();
58 fileName2->lowerCase();
59 if (!(file = fopen(fileName2->getCString(), "rb"))) {
60 fileName2->upperCase();
61 if (!(file = fopen(fileName2->getCString(), "rb"))) {
62 error(-1, "Couldn't open file '%s'", fileName->getCString());
73 str = new FileStream(file, 0, -1, &obj);
78 PDFDoc::PDFDoc(BaseStream *str) {
86 GBool PDFDoc::setup() {
99 error(-1, "Couldn't read xref table");
104 catalog = new Catalog(xref->getCatalog(&catObj));
106 if (!catalog->isOk()) {
107 error(-1, "Couldn't read page catalog");
136 // Check for a PDF header on this stream. Skip past some garbage
138 void PDFDoc::checkHeader() {
139 char hdrBuf[headerSearchSize+1];
144 for (i = 0; i < headerSearchSize; ++i) {
145 hdrBuf[i] = str->getChar();
147 hdrBuf[headerSearchSize] = '\0';
148 for (i = 0; i < headerSearchSize - 5; ++i) {
149 if (!strncmp(&hdrBuf[i], "%PDF-", 5)) {
153 if (i >= headerSearchSize - 5) {
154 error(-1, "May not be a PDF file (continuing anyway)");
158 p = strtok(&hdrBuf[i+5], " \t\n\r");
160 if (!(hdrBuf[i+5] >= '0' && hdrBuf[i+5] <= '9') ||
161 version > pdfVersionNum + 0.0001) {
162 error(-1, "PDF version %s -- xpdf supports version %s"
163 " (continuing anyway)", p, pdfVersion);
167 void PDFDoc::displayPage(OutputDev *out, int page, int zoom, int rotate,
170 double x1, y1, x2, y2;
175 printf("***** page %d *****\n", page);
176 catalog->getPage(page)->display(out, zoom, rotate);
181 for (i = 0; i < links->getNumLinks(); ++i) {
182 link = links->getLink(i);
183 link->getBorder(&x1, &y1, &x2, &y2, &w);
185 out->drawLinkBorder(x1, y1, x2, y2, w);
191 void PDFDoc::displayPages(OutputDev *out, int firstPage, int lastPage,
192 int zoom, int rotate) {
196 for (page = firstPage; page <= lastPage; ++page) {
198 printf("***** page %d *****\n", page);
199 p = catalog->getPage(page);
200 p->display(out, zoom, rotate);
204 GBool PDFDoc::saveAs(GString *name) {
208 if (!(f = fopen(name->getCString(), "wb"))) {
209 error(-1, "Couldn't open file '%s'", name->getCString());
213 while ((c = str->getChar()) != EOF) {
220 void PDFDoc::getLinks(int page) {
223 links = new Links(catalog->getPage(page)->getAnnots(&obj),
224 catalog->getBaseURI());