]>
git.ipfire.org Git - thirdparty/cups.git/blob - pdftops/PDFDoc.cxx
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
*fileNameA
, GString
*ownerPassword
,
41 GString
*userPassword
, GBool printCommandsA
) {
52 printCommands
= printCommandsA
;
58 if (!(file
= fopen(fileName
->getCString(), "rb", "ctx=stm"))) {
59 error(-1, "Couldn't open file '%s'", fileName
->getCString());
63 if (!(file
= fopen(fileName
->getCString(), "rb"))) {
64 fileName2
= fileName
->copy();
65 fileName2
->lowerCase();
66 if (!(file
= fopen(fileName2
->getCString(), "rb"))) {
67 fileName2
->upperCase();
68 if (!(file
= fopen(fileName2
->getCString(), "rb"))) {
69 error(-1, "Couldn't open file '%s'", fileName
->getCString());
80 str
= new FileStream(file
, 0, -1, &obj
);
82 ok
= setup(ownerPassword
, userPassword
);
85 PDFDoc::PDFDoc(BaseStream
*strA
, GString
*ownerPassword
,
86 GString
*userPassword
, GBool printCommandsA
) {
94 printCommands
= printCommandsA
;
95 ok
= setup(ownerPassword
, userPassword
);
98 GBool
PDFDoc::setup(GString
*ownerPassword
, GString
*userPassword
) {
103 xref
= new XRef(str
, ownerPassword
, userPassword
);
105 error(-1, "Couldn't read xref table");
110 catalog
= new Catalog(xref
, printCommands
);
111 if (!catalog
->isOk()) {
112 error(-1, "Couldn't read page catalog");
141 // Check for a PDF header on this stream. Skip past some garbage
143 void PDFDoc::checkHeader() {
144 char hdrBuf
[headerSearchSize
+1];
149 for (i
= 0; i
< headerSearchSize
; ++i
) {
150 hdrBuf
[i
] = str
->getChar();
152 hdrBuf
[headerSearchSize
] = '\0';
153 for (i
= 0; i
< headerSearchSize
- 5; ++i
) {
154 if (!strncmp(&hdrBuf
[i
], "%PDF-", 5)) {
158 if (i
>= headerSearchSize
- 5) {
159 error(-1, "May not be a PDF file (continuing anyway)");
163 p
= strtok(&hdrBuf
[i
+5], " \t\n\r");
164 pdfVersion
= atof(p
);
165 if (!(hdrBuf
[i
+5] >= '0' && hdrBuf
[i
+5] <= '9') ||
166 pdfVersion
> supportedPDFVersionNum
+ 0.0001) {
167 error(-1, "PDF version %s -- xpdf supports version %s"
168 " (continuing anyway)", p
, supportedPDFVersionStr
);
172 void PDFDoc::displayPage(OutputDev
*out
, int page
, double zoom
,
173 int rotate
, GBool doLinks
) {
177 printf("***** page %d *****\n", page
);
179 p
= catalog
->getPage(page
);
185 p
->display(out
, zoom
, rotate
, links
, catalog
);
187 p
->display(out
, zoom
, rotate
, NULL
, catalog
);
191 void PDFDoc::displayPages(OutputDev
*out
, int firstPage
, int lastPage
,
192 int zoom
, int rotate
, GBool doLinks
) {
195 for (page
= firstPage
; page
<= lastPage
; ++page
) {
196 displayPage(out
, page
, zoom
, rotate
, doLinks
);
200 GBool
PDFDoc::isLinearized() {
202 Object obj1
, obj2
, obj3
, obj4
, obj5
;
207 parser
= new Parser(xref
, new Lexer(xref
, str
->makeSubStream(str
->getStart(),
209 parser
->getObj(&obj1
);
210 parser
->getObj(&obj2
);
211 parser
->getObj(&obj3
);
212 parser
->getObj(&obj4
);
213 if (obj1
.isInt() && obj2
.isInt() && obj3
.isCmd("obj") &&
215 obj4
.dictLookup("Linearized", &obj5
);
216 if (obj5
.isNum() && obj5
.getNum() > 0) {
229 GBool
PDFDoc::saveAs(GString
*name
) {
233 if (!(f
= fopen(name
->getCString(), "wb"))) {
234 error(-1, "Couldn't open file '%s'", name
->getCString());
238 while ((c
= str
->getChar()) != EOF
) {
246 void PDFDoc::getLinks(Page
*page
) {
249 links
= new Links(page
->getAnnots(&obj
), catalog
->getBaseURI());