]> www.fi.muni.cz Git - evince.git/blob - pdf/xpdf/pdfimages.cc
Synched with Xpdf 0.92
[evince.git] / pdf / xpdf / pdfimages.cc
1 //========================================================================
2 //
3 // pdfimages.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 "ImageOutputDev.h"
25 #include "Params.h"
26 #include "Error.h"
27 #include "config.h"
28
29 static int firstPage = 1;
30 static int lastPage = 0;
31 static GBool dumpJPEG = gFalse;
32 static char userPassword[33] = "";
33 static GBool printVersion = gFalse;
34 static GBool printHelp = gFalse;
35
36 static ArgDesc argDesc[] = {
37   {"-f",      argInt,      &firstPage,     0,
38    "first page to convert"},
39   {"-l",      argInt,      &lastPage,      0,
40    "last page to convert"},
41   {"-j",      argFlag,     &dumpJPEG,      0,
42    "write JPEG images as JPEG files"},
43   {"-upw",    argString,   userPassword,   sizeof(userPassword),
44    "user password (for encrypted files)"},
45   {"-q",      argFlag,     &errQuiet,      0,
46    "don't print any messages or errors"},
47   {"-v",      argFlag,     &printVersion,  0,
48    "print copyright and version info"},
49   {"-h",      argFlag,     &printHelp,     0,
50    "print usage information"},
51   {"-help",   argFlag,     &printHelp,     0,
52    "print usage information"},
53   {NULL}
54 };
55
56 int main(int argc, char *argv[]) {
57   PDFDoc *doc;
58   GString *fileName;
59   char *imgRoot;
60   GString *userPW;
61   ImageOutputDev *imgOut;
62   GBool ok;
63
64   // parse args
65   ok = parseArgs(argDesc, &argc, argv);
66   if (!ok || argc != 3 || printVersion || printHelp) {
67     fprintf(stderr, "pdfimages version %s\n", xpdfVersion);
68     fprintf(stderr, "%s\n", xpdfCopyright);
69     if (!printVersion) {
70       printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
71     }
72     exit(1);
73   }
74   fileName = new GString(argv[1]);
75   imgRoot = argv[2];
76
77   // init error file
78   errorInit();
79
80   // read config file
81   initParams(xpdfConfigFile);
82
83   // open PDF file
84   xref = NULL;
85   if (userPassword[0]) {
86     userPW = new GString(userPassword);
87   } else {
88     userPW = NULL;
89   }
90   doc = new PDFDoc(fileName, userPW);
91   if (userPW) {
92     delete userPW;
93   }
94   if (!doc->isOk()) {
95     goto err;
96   }
97
98   // check for copy permission
99   if (!doc->okToCopy()) {
100     error(-1, "Copying of images from this document is not allowed.");
101     goto err;
102   }
103
104   // get page range
105   if (firstPage < 1)
106     firstPage = 1;
107   if (lastPage < 1 || lastPage > doc->getNumPages())
108     lastPage = doc->getNumPages();
109
110   // write image files
111   imgOut = new ImageOutputDev(imgRoot, dumpJPEG);
112   if (imgOut->isOk())
113     doc->displayPages(imgOut, firstPage, lastPage, 72, 0, gFalse);
114   delete imgOut;
115
116   // clean up
117  err:
118   delete doc;
119   freeParams();
120
121   // check for memory leaks
122   Object::memCheck(stderr);
123   gMemReport(stderr);
124
125   return 0;
126 }