filters

pdfimages.cc

00001 //========================================================================
00002 //
00003 // pdfimages.cc
00004 //
00005 // Copyright 1998-2002 Glyph & Cog, LLC
00006 //
00007 //========================================================================
00008 
00009 #include <aconf.h>
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <stddef.h>
00013 #include <string.h>
00014 #include "parseargs.h"
00015 #include "GString.h"
00016 #include "gmem.h"
00017 #include "GlobalParams.h"
00018 #include "Object.h"
00019 #include "Stream.h"
00020 #include "Array.h"
00021 #include "Dict.h"
00022 #include "XRef.h"
00023 #include "Catalog.h"
00024 #include "Page.h"
00025 #include "PDFDoc.h"
00026 #include "ImageOutputDev.h"
00027 #include "Error.h"
00028 #include "config.h"
00029 
00030 static int firstPage = 1;
00031 static int lastPage = 0;
00032 static GBool dumpJPEG = gFalse;
00033 static char ownerPassword[33] = "";
00034 static char userPassword[33] = "";
00035 static GBool quiet = gFalse;
00036 static char cfgFileName[256] = "";
00037 static GBool printVersion = gFalse;
00038 static GBool printHelp = gFalse;
00039 
00040 static ArgDesc argDesc[] = {
00041   {"-f",      argInt,      &firstPage,     0,
00042    "first page to convert"},
00043   {"-l",      argInt,      &lastPage,      0,
00044    "last page to convert"},
00045   {"-j",      argFlag,     &dumpJPEG,      0,
00046    "write JPEG images as JPEG files"},
00047   {"-opw",    argString,   ownerPassword,  sizeof(ownerPassword),
00048    "owner password (for encrypted files)"},
00049   {"-upw",    argString,   userPassword,   sizeof(userPassword),
00050    "user password (for encrypted files)"},
00051   {"-q",      argFlag,     &quiet,         0,
00052    "don't print any messages or errors"},
00053   {"-cfg",        argString,      cfgFileName,    sizeof(cfgFileName),
00054    "configuration file to use in place of .xpdfrc"},
00055   {"-v",      argFlag,     &printVersion,  0,
00056    "print copyright and version info"},
00057   {"-h",      argFlag,     &printHelp,     0,
00058    "print usage information"},
00059   {"-help",   argFlag,     &printHelp,     0,
00060    "print usage information"},
00061   {"--help",  argFlag,     &printHelp,     0,
00062    "print usage information"},
00063   {"-?",      argFlag,     &printHelp,     0,
00064    "print usage information"},
00065   {NULL}
00066 };
00067 
00068 int main(int argc, char *argv[]) {
00069   PDFDoc *doc;
00070   GString *fileName;
00071   char *imgRoot;
00072   GString *ownerPW, *userPW;
00073   ImageOutputDev *imgOut;
00074   GBool ok;
00075   int exitCode;
00076 
00077   exitCode = 99;
00078 
00079   // parse args
00080   ok = parseArgs(argDesc, &argc, argv);
00081   if (!ok || argc != 3 || printVersion || printHelp) {
00082     fprintf(stderr, "pdfimages version %s\n", xpdfVersion);
00083     fprintf(stderr, "%s\n", xpdfCopyright);
00084     if (!printVersion) {
00085       printUsage("pdfimages", "<PDF-file> <image-root>", argDesc);
00086     }
00087     goto err0;
00088   }
00089   fileName = new GString(argv[1]);
00090   imgRoot = argv[2];
00091 
00092   // read config file
00093   globalParams = new GlobalParams(cfgFileName);
00094   if (quiet) {
00095     globalParams->setErrQuiet(quiet);
00096   }
00097 
00098   // open PDF file
00099   if (ownerPassword[0]) {
00100     ownerPW = new GString(ownerPassword);
00101   } else {
00102     ownerPW = NULL;
00103   }
00104   if (userPassword[0]) {
00105     userPW = new GString(userPassword);
00106   } else {
00107     userPW = NULL;
00108   }
00109   doc = new PDFDoc(fileName, ownerPW, userPW);
00110   if (userPW) {
00111     delete userPW;
00112   }
00113   if (ownerPW) {
00114     delete ownerPW;
00115   }
00116   if (!doc->isOk()) {
00117     exitCode = 1;
00118     goto err1;
00119   }
00120 
00121   // check for copy permission
00122   if (!doc->okToCopy()) {
00123     error(-1, "Copying of images from this document is not allowed.");
00124     exitCode = 3;
00125     goto err1;
00126   }
00127 
00128   // get page range
00129   if (firstPage < 1)
00130     firstPage = 1;
00131   if (lastPage < 1 || lastPage > doc->getNumPages())
00132     lastPage = doc->getNumPages();
00133 
00134   // write image files
00135   imgOut = new ImageOutputDev(imgRoot, dumpJPEG);
00136   if (imgOut->isOk()) {
00137     doc->displayPages(imgOut, firstPage, lastPage, 72, 0, gFalse);
00138   }
00139   delete imgOut;
00140 
00141   exitCode = 0;
00142 
00143   // clean up
00144  err1:
00145   delete doc;
00146   delete globalParams;
00147  err0:
00148 
00149   // check for memory leaks
00150   Object::memCheck(stderr);
00151   gMemReport(stderr);
00152 
00153   return exitCode;
00154 }
KDE Home | KDE Accessibility Home | Description of Access Keys