]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdfinfo.cc
d45599bd9c8f90c3a79eb302cdb601ad989f2fc2
[evince.git] / pdf / xpdf / pdfinfo.cc
1 //========================================================================
2 //
3 // pdfinfo.cc
4 //
5 // Copyright 1998 Derek B. Noonburg
6 //
7 //========================================================================
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stddef.h>
12 #include <string.h>
13 #include "parseargs.h"
14 #include "GString.h"
15 #include "gmem.h"
16 #include "Object.h"
17 #include "Stream.h"
18 #include "Array.h"
19 #include "Dict.h"
20 #include "XRef.h"
21 #include "Catalog.h"
22 #include "Page.h"
23 #include "PDFDoc.h"
24 #include "Params.h"
25 #include "Error.h"
26 #include "config.h"
27
28 static void printInfoString(Dict *infoDict, char *key, char *fmt);
29 static void printInfoDate(Dict *infoDict, char *key, char *fmt);
30
31 static char userPassword[33] = "";
32 static GBool printVersion = gFalse;
33 static GBool printHelp = gFalse;
34
35 static ArgDesc argDesc[] = {
36   {"-upw",    argString,   userPassword,   sizeof(userPassword),
37    "user password (for encrypted files)"},
38   {"-v",      argFlag,     &printVersion,  0,
39    "print copyright and version info"},
40   {"-h",      argFlag,     &printHelp,     0,
41    "print usage information"},
42   {"-help",   argFlag,     &printHelp,     0,
43    "print usage information"},
44   {NULL}
45 };
46
47 int main(int argc, char *argv[]) {
48   PDFDoc *doc;
49   GString *fileName;
50   GString *userPW;
51   Object info;
52   GBool ok;
53
54   // parse args
55   ok = parseArgs(argDesc, &argc, argv);
56   if (!ok || argc != 2 || printVersion || printHelp) {
57     fprintf(stderr, "pdfinfo version %s\n", xpdfVersion);
58     fprintf(stderr, "%s\n", xpdfCopyright);
59     if (!printVersion) {
60       printUsage("pdfinfo", "<PDF-file>", argDesc);
61     }
62     exit(1);
63   }
64   fileName = new GString(argv[1]);
65
66   // init error file
67   errorInit();
68
69   // read config file
70   initParams(xpdfConfigFile);
71
72   // open PDF file
73   xref = NULL;
74   if (userPassword[0]) {
75     userPW = new GString(userPassword);
76   } else {
77     userPW = NULL;
78   }
79   doc = new PDFDoc(fileName, userPW);
80   if (userPW) {
81     delete userPW;
82   }
83   if (!doc->isOk()) {
84     exit(1);
85   }
86
87   // print doc info
88   doc->getDocInfo(&info);
89   if (info.isDict()) {
90     printInfoString(info.getDict(), "Title",        "Title:        %s\n");
91     printInfoString(info.getDict(), "Subject",      "Subject:      %s\n");
92     printInfoString(info.getDict(), "Keywords",     "Keywords:     %s\n");
93     printInfoString(info.getDict(), "Author",       "Author:       %s\n");
94     printInfoString(info.getDict(), "Creator",      "Creator:      %s\n");
95     printInfoString(info.getDict(), "Producer",     "Producer:     %s\n");
96     printInfoDate(info.getDict(),   "CreationDate", "CreationDate: %s\n");
97     printInfoDate(info.getDict(),   "ModDate",      "ModDate:      %s\n");
98   }
99   info.free();
100
101   // print page count
102   printf("Pages:        %d\n", doc->getNumPages());
103
104   // print encryption info
105   printf("Encrypted:    ");
106   if (doc->isEncrypted()) {
107     printf("yes (print:%s copy:%s change:%s addNotes:%s)\n",
108            doc->okToPrint() ? "yes" : "no",
109            doc->okToCopy() ? "yes" : "no",
110            doc->okToChange() ? "yes" : "no",
111            doc->okToAddNotes() ? "yes" : "no");
112   } else {
113     printf("no\n");
114   }
115
116   // print linearization info
117   printf("Linearized:   %s\n", doc->isLinearized() ? "yes" : "no");
118
119   // clean up
120   delete doc;
121   freeParams();
122
123   // check for memory leaks
124   Object::memCheck(stderr);
125   gMemReport(stderr);
126
127   return 0;
128 }
129
130 static void printInfoString(Dict *infoDict, char *key, char *fmt) {
131   Object obj;
132   GString *s1, *s2;
133   int i;
134
135   if (infoDict->lookup(key, &obj)->isString()) {
136     s1 = obj.getString();
137     if ((s1->getChar(0) & 0xff) == 0xfe &&
138         (s1->getChar(1) & 0xff) == 0xff) {
139       s2 = new GString();
140       for (i = 2; i < obj.getString()->getLength(); i += 2) {
141         if (s1->getChar(i) == '\0') {
142           s2->append(s1->getChar(i+1));
143         } else {
144           delete s2;
145           s2 = new GString("<unicode>");
146           break;
147         }
148       }
149       printf(fmt, s2->getCString());
150       delete s2;
151     } else {
152       printf(fmt, s1->getCString());
153     }
154   }
155   obj.free();
156 }
157
158 static void printInfoDate(Dict *infoDict, char *key, char *fmt) {
159   Object obj;
160   char *s;
161
162   if (infoDict->lookup(key, &obj)->isString()) {
163     s = obj.getString()->getCString();
164     if (s[0] == 'D' && s[1] == ':') {
165       s += 2;
166     }
167     printf(fmt, s);
168   }
169   obj.free();
170 }