1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
|
// --------------------------------------------------------------------
// Pdftoipe: convert PDF file to editable Ipe XML file
// --------------------------------------------------------------------
#include <stdio.h>
#include "ocfile.h"
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include "parseargs.h"
#include "gstring.h"
#include "gmem.h"
#include "globalparams.h"
#include "object.h"
#include "stream.h"
#include "array.h"
#include "dict.h"
#include "xref.h"
#include "catalog.h"
#include "page.h"
#include "pdfdoc.h"
#include "xmloutputdev.h"
#include "error.h"
#ifdef POWERPOINT
#include "xmlppoutputdev.h"
#define XML_OUTPUT_DEV XmlPPOutputDev
#else
#define XML_OUTPUT_DEV XmlOutputDev
#endif
static int firstPage = 1;
static int lastPage = 0;
static int mergeLevel = 0;
static char ownerPassword[33] = "";
static char userPassword[33] = "";
static GBool quiet = gFalse;
static GBool printHelp = gFalse;
static GBool math = gFalse;
static GBool literal = gFalse;
static GBool notext = gFalse;
static GBool cyberbit = gFalse;
static ArgDesc argDesc[] = {
{"-f", argInt, &firstPage, 0,
"first page to convert"},
{"-l", argInt, &lastPage, 0,
"last page to convert"},
{"-opw", argString, ownerPassword, sizeof(ownerPassword),
"owner password (for encrypted files)"},
{"-upw", argString, userPassword, sizeof(userPassword),
"user password (for encrypted files)"},
{"-q", argFlag, &quiet, 0,
"don't print any messages or errors"},
{"-math", argFlag, &math, 0,
"turn all text objects into math formulas"},
{"-literal", argFlag, &literal, 0,
"allow math mode in input text objects"},
{"-notext", argFlag, ¬ext, 0,
"discard all text objects"},
{"-merge", argInt, &mergeLevel, 0,
"how eagerly should consecutive text be merged"},
{"-cyberbit", argFlag, &cyberbit, 0,
"include use of Cyberbit font in preamble"},
{"-h", argFlag, &printHelp, 0,
"print usage information"},
{"-help", argFlag, &printHelp, 0,
"print usage information"},
{"--help", argFlag, &printHelp, 0,
"print usage information"},
{"-?", argFlag, &printHelp, 0,
"print usage information"},
{NULL, argFlag, 0, 0, 0}
};
int main(int argc, char *argv[])
{
// parse args
GBool ok = parseArgs(argDesc, &argc, argv);
if (!ok || argc < 2 || argc > 3 || printHelp) {
fprintf(stderr, "pdftoipe version %s\n", PDFTOIPE_VERSION);
printUsage("pdftoipe", "<PDF-file> [<XML-file>]", argDesc);
return 1;
}
GString *fileName = new GString(argv[1]);
// read config file
globalParams = new GlobalParams(0);
if (quiet) {
globalParams->setErrQuiet(quiet);
}
GString *ownerPW, *userPW;
if (ownerPassword[0]) {
ownerPW = new GString(ownerPassword);
} else {
ownerPW = 0;
}
if (userPassword[0]) {
userPW = new GString(userPassword);
} else {
userPW = 0;
}
// open PDF file
PDFDoc *doc = new PDFDoc(fileName, ownerPW, userPW);
delete userPW;
delete ownerPW;
if (!doc->isOk())
return 1;
// check for print permission
if (!doc->okToPrint()) {
error(-1, "Printing this document is not allowed.");
return 3;
}
// construct XML file name
GString *xmlFileName;
if (argc == 3) {
xmlFileName = new GString(argv[2]);
} else {
char *p = fileName->getCString() + fileName->getLength() - 4;
if (!strcmp(p, ".pdf") || !strcmp(p, ".PDF")) {
xmlFileName = new GString(fileName->getCString(),
fileName->getLength() - 4);
} else {
xmlFileName = fileName->copy();
}
xmlFileName->append(".xml");
}
// get page range
if (firstPage < 1) {
firstPage = 1;
}
if (lastPage < 1 || lastPage > doc->getNumPages()) {
lastPage = doc->getNumPages();
}
// write XML file
XmlOutputDev *xmlOut =
new XML_OUTPUT_DEV(xmlFileName->getCString(), doc->getXRef(),
doc->getCatalog(), firstPage, lastPage);
// tell output device about text handling
xmlOut->setTextHandling(math, notext, literal, mergeLevel);
// include style sheet(s)
xmlOut->includeStyleSheets(cyberbit);
int exitCode = 2;
if (xmlOut->isOk()) {
doc->displayPages(xmlOut, firstPage, lastPage, 72, 0, gFalse);
exitCode = 0;
}
if (xmlOut->hasUnicode()) {
fprintf(stderr,
"The generated XML file contains Unicode (non-ASCII) text.\n");
if (!cyberbit) {
fprintf(stderr,
"See Chapter 12 in the Ipe manual on how to handle this.\n"
"If your document is in a Western language, it may be "
"sufficient to add this line:\n\n"
"\\usepackage{ucs}\n\n"
"to the documents Latex preamble "
"(Edit->Document properties).\n");
}
}
// clean up
delete xmlOut;
delete xmlFileName;
delete doc;
delete globalParams;
// check for memory leaks
Object::memCheck(stderr);
gMemReport(stderr);
return exitCode;
}
// --------------------------------------------------------------------
|