1 | //======================================================================== |
2 | // |
3 | // pdftotext.cc |
4 | // |
5 | // Copyright 1997-2003 Glyph & Cog, LLC |
6 | // |
7 | // Modified for Debian by Hamish Moffatt, 22 May 2002. |
8 | // |
9 | //======================================================================== |
10 | |
11 | //======================================================================== |
12 | // |
13 | // Modified under the Poppler project - http://poppler.freedesktop.org |
14 | // |
15 | // All changes made under the Poppler project to this file are licensed |
16 | // under GPL version 2 or later |
17 | // |
18 | // Copyright (C) 2006 Dominic Lachowicz <cinamod@hotmail.com> |
19 | // Copyright (C) 2007-2008, 2010, 2011, 2017-2022 Albert Astals Cid <aacid@kde.org> |
20 | // Copyright (C) 2009 Jan Jockusch <jan@jockusch.de> |
21 | // Copyright (C) 2010, 2013 Hib Eris <hib@hiberis.nl> |
22 | // Copyright (C) 2010 Kenneth Berland <ken@hero.com> |
23 | // Copyright (C) 2011 Tom Gleason <tom@buildadam.com> |
24 | // Copyright (C) 2011 Steven Murdoch <Steven.Murdoch@cl.cam.ac.uk> |
25 | // Copyright (C) 2013 Yury G. Kudryashov <urkud.urkud@gmail.com> |
26 | // Copyright (C) 2013 Suzuki Toshiya <mpsuzuki@hiroshima-u.ac.jp> |
27 | // Copyright (C) 2015 Jeremy Echols <jechols@uoregon.edu> |
28 | // Copyright (C) 2017 Adrian Johnson <ajohnson@redneon.com> |
29 | // Copyright (C) 2018 Klarälvdalens Datakonsult AB, a KDAB Group company, <info@kdab.com>. Work sponsored by the LiMux project of the city of Munich |
30 | // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de> |
31 | // Copyright (C) 2018 Sanchit Anand <sanxchit@gmail.com> |
32 | // Copyright (C) 2019 Dan Shea <dan.shea@logical-innovations.com> |
33 | // Copyright (C) 2019, 2021 Oliver Sander <oliver.sander@tu-dresden.de> |
34 | // Copyright (C) 2021 William Bader <williambader@hotmail.com> |
35 | // Copyright (C) 2022 kVdNi <kVdNi@waqa.eu> |
36 | // |
37 | // To see a description of the changes please see the Changelog file that |
38 | // came with your tarball or type make ChangeLog if you are building from git |
39 | // |
40 | //======================================================================== |
41 | |
42 | #include "config.h" |
43 | #include <poppler-config.h> |
44 | #include <cstdio> |
45 | #include <cstdlib> |
46 | #include <cstddef> |
47 | #include <cstring> |
48 | #include "parseargs.h" |
49 | #include "printencodings.h" |
50 | #include "goo/GooString.h" |
51 | #include "goo/gmem.h" |
52 | #include "GlobalParams.h" |
53 | #include "Object.h" |
54 | #include "Stream.h" |
55 | #include "Array.h" |
56 | #include "Dict.h" |
57 | #include "XRef.h" |
58 | #include "Catalog.h" |
59 | #include "Page.h" |
60 | #include "PDFDoc.h" |
61 | #include "PDFDocFactory.h" |
62 | #include "TextOutputDev.h" |
63 | #include "CharTypes.h" |
64 | #include "UnicodeMap.h" |
65 | #include "PDFDocEncoding.h" |
66 | #include "Error.h" |
67 | #include <string> |
68 | #include <sstream> |
69 | #include <iomanip> |
70 | #include "Win32Console.h" |
71 | #include "DateInfo.h" |
72 | #include <cfloat> |
73 | |
74 | static void printInfoString(FILE *f, Dict *infoDict, const char *key, const char *text1, const char *text2, const UnicodeMap *uMap); |
75 | static void printInfoDate(FILE *f, Dict *infoDict, const char *key, const char *text1, const char *text2); |
76 | void printDocBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last); |
77 | void printWordBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last); |
78 | void printTSVBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last); |
79 | |
80 | static int firstPage = 1; |
81 | static int lastPage = 0; |
82 | static double resolution = 72.0; |
83 | static int x = 0; |
84 | static int y = 0; |
85 | static int w = 0; |
86 | static int h = 0; |
87 | static bool bbox = false; |
88 | static bool bboxLayout = false; |
89 | static bool physLayout = false; |
90 | static bool useCropBox = false; |
91 | static double colspacing = TextOutputDev::minColSpacing1_default; |
92 | static double fixedPitch = 0; |
93 | static bool rawOrder = false; |
94 | static bool discardDiag = false; |
95 | static bool htmlMeta = false; |
96 | static char textEncName[128] = "" ; |
97 | static char textEOLStr[16] = "" ; |
98 | static bool noPageBreaks = false; |
99 | static char ownerPassword[33] = "\001" ; |
100 | static char userPassword[33] = "\001" ; |
101 | static bool quiet = false; |
102 | static bool printVersion = false; |
103 | static bool printHelp = false; |
104 | static bool printEnc = false; |
105 | static bool tsvMode = false; |
106 | |
107 | static const ArgDesc argDesc[] = { { .arg: "-f" , .kind: argInt, .val: &firstPage, .size: 0, .usage: "first page to convert" }, |
108 | { .arg: "-l" , .kind: argInt, .val: &lastPage, .size: 0, .usage: "last page to convert" }, |
109 | { .arg: "-r" , .kind: argFP, .val: &resolution, .size: 0, .usage: "resolution, in DPI (default is 72)" }, |
110 | { .arg: "-x" , .kind: argInt, .val: &x, .size: 0, .usage: "x-coordinate of the crop area top left corner" }, |
111 | { .arg: "-y" , .kind: argInt, .val: &y, .size: 0, .usage: "y-coordinate of the crop area top left corner" }, |
112 | { .arg: "-W" , .kind: argInt, .val: &w, .size: 0, .usage: "width of crop area in pixels (default is 0)" }, |
113 | { .arg: "-H" , .kind: argInt, .val: &h, .size: 0, .usage: "height of crop area in pixels (default is 0)" }, |
114 | { .arg: "-layout" , .kind: argFlag, .val: &physLayout, .size: 0, .usage: "maintain original physical layout" }, |
115 | { .arg: "-fixed" , .kind: argFP, .val: &fixedPitch, .size: 0, .usage: "assume fixed-pitch (or tabular) text" }, |
116 | { .arg: "-raw" , .kind: argFlag, .val: &rawOrder, .size: 0, .usage: "keep strings in content stream order" }, |
117 | { .arg: "-nodiag" , .kind: argFlag, .val: &discardDiag, .size: 0, .usage: "discard diagonal text" }, |
118 | { .arg: "-htmlmeta" , .kind: argFlag, .val: &htmlMeta, .size: 0, .usage: "generate a simple HTML file, including the meta information" }, |
119 | { .arg: "-tsv" , .kind: argFlag, .val: &tsvMode, .size: 0, .usage: "generate a simple TSV file, including the meta information for bounding boxes" }, |
120 | { .arg: "-enc" , .kind: argString, .val: textEncName, .size: sizeof(textEncName), .usage: "output text encoding name" }, |
121 | { .arg: "-listenc" , .kind: argFlag, .val: &printEnc, .size: 0, .usage: "list available encodings" }, |
122 | { .arg: "-eol" , .kind: argString, .val: textEOLStr, .size: sizeof(textEOLStr), .usage: "output end-of-line convention (unix, dos, or mac)" }, |
123 | { .arg: "-nopgbrk" , .kind: argFlag, .val: &noPageBreaks, .size: 0, .usage: "don't insert page breaks between pages" }, |
124 | { .arg: "-bbox" , .kind: argFlag, .val: &bbox, .size: 0, .usage: "output bounding box for each word and page size to html. Sets -htmlmeta" }, |
125 | { .arg: "-bbox-layout" , .kind: argFlag, .val: &bboxLayout, .size: 0, .usage: "like -bbox but with extra layout bounding box data. Sets -htmlmeta" }, |
126 | { .arg: "-cropbox" , .kind: argFlag, .val: &useCropBox, .size: 0, .usage: "use the crop box rather than media box" }, |
127 | { .arg: "-colspacing" , .kind: argFP, .val: &colspacing, .size: 0, |
128 | .usage: "how much spacing we allow after a word before considering adjacent text to be a new column, as a fraction of the font size (default is 0.7, old releases had a 0.3 default)" }, |
129 | { .arg: "-opw" , .kind: argString, .val: ownerPassword, .size: sizeof(ownerPassword), .usage: "owner password (for encrypted files)" }, |
130 | { .arg: "-upw" , .kind: argString, .val: userPassword, .size: sizeof(userPassword), .usage: "user password (for encrypted files)" }, |
131 | { .arg: "-q" , .kind: argFlag, .val: &quiet, .size: 0, .usage: "don't print any messages or errors" }, |
132 | { .arg: "-v" , .kind: argFlag, .val: &printVersion, .size: 0, .usage: "print copyright and version info" }, |
133 | { .arg: "-h" , .kind: argFlag, .val: &printHelp, .size: 0, .usage: "print usage information" }, |
134 | { .arg: "-help" , .kind: argFlag, .val: &printHelp, .size: 0, .usage: "print usage information" }, |
135 | { .arg: "--help" , .kind: argFlag, .val: &printHelp, .size: 0, .usage: "print usage information" }, |
136 | { .arg: "-?" , .kind: argFlag, .val: &printHelp, .size: 0, .usage: "print usage information" }, |
137 | {} }; |
138 | |
139 | static std::string myStringReplace(const std::string &inString, const std::string &oldToken, const std::string &newToken) |
140 | { |
141 | std::string result = inString; |
142 | size_t foundLoc; |
143 | int advance = 0; |
144 | do { |
145 | foundLoc = result.find(str: oldToken, pos: advance); |
146 | if (foundLoc != std::string::npos) { |
147 | result.replace(pos: foundLoc, n: oldToken.length(), str: newToken); |
148 | advance = foundLoc + newToken.length(); |
149 | } |
150 | } while (foundLoc != std::string::npos); |
151 | return result; |
152 | } |
153 | |
154 | static std::string myXmlTokenReplace(const char *inString) |
155 | { |
156 | std::string myString(inString); |
157 | myString = myStringReplace(inString: myString, oldToken: "&" , newToken: "&" ); |
158 | myString = myStringReplace(inString: myString, oldToken: "'" , newToken: "'" ); |
159 | myString = myStringReplace(inString: myString, oldToken: "\"" , newToken: """ ); |
160 | myString = myStringReplace(inString: myString, oldToken: "<" , newToken: "<" ); |
161 | myString = myStringReplace(inString: myString, oldToken: ">" , newToken: ">" ); |
162 | return myString; |
163 | } |
164 | |
165 | int main(int argc, char *argv[]) |
166 | { |
167 | std::unique_ptr<PDFDoc> doc; |
168 | std::unique_ptr<GooString> textFileName; |
169 | std::optional<GooString> ownerPW, userPW; |
170 | FILE *f; |
171 | const UnicodeMap *uMap; |
172 | Object info; |
173 | bool ok; |
174 | EndOfLineKind textEOL = TextOutputDev::defaultEndOfLine(); |
175 | |
176 | Win32Console win32Console(&argc, &argv); |
177 | |
178 | // parse args |
179 | ok = parseArgs(args: argDesc, argc: &argc, argv); |
180 | if (bboxLayout) { |
181 | bbox = true; |
182 | } |
183 | if (bbox) { |
184 | htmlMeta = true; |
185 | } |
186 | if (colspacing <= 0 || colspacing > 10) { |
187 | error(category: errCommandLine, pos: -1, msg: "Bogus value provided for -colspacing" ); |
188 | return 99; |
189 | } |
190 | if (!ok || (argc < 2 && !printEnc) || argc > 3 || printVersion || printHelp) { |
191 | fprintf(stderr, format: "pdftotext version %s\n" , PACKAGE_VERSION); |
192 | fprintf(stderr, format: "%s\n" , popplerCopyright); |
193 | fprintf(stderr, format: "%s\n" , xpdfCopyright); |
194 | if (!printVersion) { |
195 | printUsage(program: "pdftotext" , otherArgs: "<PDF-file> [<text-file>]" , args: argDesc); |
196 | } |
197 | if (printVersion || printHelp) { |
198 | return 0; |
199 | } |
200 | return 99; |
201 | } |
202 | |
203 | // read config file |
204 | globalParams = std::make_unique<GlobalParams>(); |
205 | |
206 | if (printEnc) { |
207 | printEncodings(); |
208 | return 0; |
209 | } |
210 | |
211 | GooString fileName(argv[1]); |
212 | if (fixedPitch) { |
213 | physLayout = true; |
214 | } |
215 | |
216 | if (textEncName[0]) { |
217 | globalParams->setTextEncoding(textEncName); |
218 | } |
219 | if (textEOLStr[0]) { |
220 | if (!strcmp(s1: textEOLStr, s2: "unix" )) { |
221 | textEOL = eolUnix; |
222 | } else if (!strcmp(s1: textEOLStr, s2: "dos" )) { |
223 | textEOL = eolDOS; |
224 | } else if (!strcmp(s1: textEOLStr, s2: "mac" )) { |
225 | textEOL = eolMac; |
226 | } else { |
227 | fprintf(stderr, format: "Bad '-eol' value on command line\n" ); |
228 | } |
229 | } |
230 | if (quiet) { |
231 | globalParams->setErrQuiet(quiet); |
232 | } |
233 | |
234 | // get mapping to output encoding |
235 | if (!(uMap = globalParams->getTextEncoding())) { |
236 | error(category: errCommandLine, pos: -1, msg: "Couldn't get text encoding" ); |
237 | return 99; |
238 | } |
239 | |
240 | // open PDF file |
241 | if (ownerPassword[0] != '\001') { |
242 | ownerPW = GooString(ownerPassword); |
243 | } |
244 | if (userPassword[0] != '\001') { |
245 | userPW = GooString(userPassword); |
246 | } |
247 | |
248 | if (fileName.cmp(sA: "-" ) == 0) { |
249 | fileName = GooString("fd://0" ); |
250 | } |
251 | |
252 | doc = PDFDocFactory().createPDFDoc(uri: fileName, ownerPassword: ownerPW, userPassword: userPW); |
253 | |
254 | if (!doc->isOk()) { |
255 | return 1; |
256 | } |
257 | |
258 | #ifdef ENFORCE_PERMISSIONS |
259 | // check for copy permission |
260 | if (!doc->okToCopy()) { |
261 | error(errNotAllowed, -1, "Copying of text from this document is not allowed." ); |
262 | return 3; |
263 | } |
264 | #endif |
265 | |
266 | // construct text file name |
267 | if (argc == 3) { |
268 | textFileName = std::make_unique<GooString>(args&: argv[2]); |
269 | } else if (fileName.cmp(sA: "fd://0" ) == 0) { |
270 | error(category: errCommandLine, pos: -1, msg: "You have to provide an output filename when reading from stdin." ); |
271 | return 99; |
272 | } else { |
273 | const char *p = fileName.c_str() + fileName.getLength() - 4; |
274 | if (!strcmp(s1: p, s2: ".pdf" ) || !strcmp(s1: p, s2: ".PDF" )) { |
275 | textFileName = std::make_unique<GooString>(args: fileName.c_str(), args: fileName.getLength() - 4); |
276 | } else { |
277 | textFileName.reset(p: fileName.copy()); |
278 | } |
279 | textFileName->append(str: htmlMeta ? ".html" : ".txt" ); |
280 | } |
281 | |
282 | // get page range |
283 | if (firstPage < 1) { |
284 | firstPage = 1; |
285 | } |
286 | if (lastPage < 1 || lastPage > doc->getNumPages()) { |
287 | lastPage = doc->getNumPages(); |
288 | } |
289 | if (lastPage < firstPage) { |
290 | error(category: errCommandLine, pos: -1, msg: "Wrong page range given: the first page ({0:d}) can not be after the last page ({1:d})." , firstPage, lastPage); |
291 | return 99; |
292 | } |
293 | |
294 | // write HTML header |
295 | if (htmlMeta) { |
296 | if (!textFileName->cmp(sA: "-" )) { |
297 | f = stdout; |
298 | } else { |
299 | if (!(f = fopen(filename: textFileName->c_str(), modes: "wb" ))) { |
300 | error(category: errIO, pos: -1, msg: "Couldn't open text file '{0:t}'" , textFileName.get()); |
301 | return 2; |
302 | } |
303 | } |
304 | fputs(s: "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" , stream: f); |
305 | fputs(s: "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n" , stream: f); |
306 | fputs(s: "<head>\n" , stream: f); |
307 | info = doc->getDocInfo(); |
308 | if (info.isDict()) { |
309 | Object obj = info.getDict()->lookup(key: "Title" ); |
310 | if (obj.isString()) { |
311 | printInfoString(f, infoDict: info.getDict(), key: "Title" , text1: "<title>" , text2: "</title>\n" , uMap); |
312 | } else { |
313 | fputs(s: "<title></title>\n" , stream: f); |
314 | } |
315 | printInfoString(f, infoDict: info.getDict(), key: "Subject" , text1: "<meta name=\"Subject\" content=\"" , text2: "\"/>\n" , uMap); |
316 | printInfoString(f, infoDict: info.getDict(), key: "Keywords" , text1: "<meta name=\"Keywords\" content=\"" , text2: "\"/>\n" , uMap); |
317 | printInfoString(f, infoDict: info.getDict(), key: "Author" , text1: "<meta name=\"Author\" content=\"" , text2: "\"/>\n" , uMap); |
318 | printInfoString(f, infoDict: info.getDict(), key: "Creator" , text1: "<meta name=\"Creator\" content=\"" , text2: "\"/>\n" , uMap); |
319 | printInfoString(f, infoDict: info.getDict(), key: "Producer" , text1: "<meta name=\"Producer\" content=\"" , text2: "\"/>\n" , uMap); |
320 | printInfoDate(f, infoDict: info.getDict(), key: "CreationDate" , text1: "<meta name=\"CreationDate\" content=\"" , text2: "\"/>\n" ); |
321 | printInfoDate(f, infoDict: info.getDict(), key: "ModDate" , text1: "<meta name=\"ModDate\" content=\"" , text2: "\"/>\n" ); |
322 | } |
323 | fputs(s: "</head>\n" , stream: f); |
324 | fputs(s: "<body>\n" , stream: f); |
325 | if (!bbox) { |
326 | fputs(s: "<pre>\n" , stream: f); |
327 | if (f != stdout) { |
328 | fclose(stream: f); |
329 | } |
330 | } |
331 | } |
332 | |
333 | // write text file |
334 | if (htmlMeta && bbox) { // htmlMeta && is superfluous but makes gcc happier |
335 | TextOutputDev textOut(nullptr, physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag); |
336 | |
337 | if (textOut.isOk()) { |
338 | textOut.setTextEOL(textEOL); |
339 | textOut.setMinColSpacing1(colspacing); |
340 | if (noPageBreaks) { |
341 | textOut.setTextPageBreaks(false); |
342 | } |
343 | if (bboxLayout) { |
344 | printDocBBox(f, doc: doc.get(), textOut: &textOut, first: firstPage, last: lastPage); |
345 | } else { |
346 | printWordBBox(f, doc: doc.get(), textOut: &textOut, first: firstPage, last: lastPage); |
347 | } |
348 | } |
349 | if (f != stdout) { |
350 | fclose(stream: f); |
351 | } |
352 | } else { |
353 | |
354 | if (tsvMode) { |
355 | TextOutputDev textOut(nullptr, physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag); |
356 | if (!textFileName->cmp(sA: "-" )) { |
357 | f = stdout; |
358 | } else { |
359 | if (!(f = fopen(filename: textFileName->c_str(), modes: "wb" ))) { |
360 | error(category: errIO, pos: -1, msg: "Couldn't open text file '{0:t}'" , textFileName.get()); |
361 | return 2; |
362 | } |
363 | } |
364 | printTSVBBox(f, doc: doc.get(), textOut: &textOut, first: firstPage, last: lastPage); |
365 | if (f != stdout) { |
366 | fclose(stream: f); |
367 | } |
368 | } else { |
369 | TextOutputDev textOut(textFileName->c_str(), physLayout, fixedPitch, rawOrder, htmlMeta, discardDiag); |
370 | if (textOut.isOk()) { |
371 | textOut.setTextEOL(textEOL); |
372 | textOut.setMinColSpacing1(colspacing); |
373 | if (noPageBreaks) { |
374 | textOut.setTextPageBreaks(false); |
375 | } |
376 | |
377 | if ((w == 0) && (h == 0) && (x == 0) && (y == 0)) { |
378 | doc->displayPages(out: &textOut, firstPage, lastPage, hDPI: resolution, vDPI: resolution, rotate: 0, useMediaBox: true, crop: false, printing: false); |
379 | } else { |
380 | |
381 | for (int page = firstPage; page <= lastPage; ++page) { |
382 | doc->displayPageSlice(out: &textOut, page, hDPI: resolution, vDPI: resolution, rotate: 0, useMediaBox: true, crop: false, printing: false, sliceX: x, sliceY: y, sliceW: w, sliceH: h); |
383 | } |
384 | } |
385 | |
386 | } else { |
387 | return 2; |
388 | } |
389 | } |
390 | } |
391 | |
392 | // write end of HTML file |
393 | if (htmlMeta) { |
394 | if (!textFileName->cmp(sA: "-" )) { |
395 | f = stdout; |
396 | } else { |
397 | if (!(f = fopen(filename: textFileName->c_str(), modes: "ab" ))) { |
398 | error(category: errIO, pos: -1, msg: "Couldn't open text file '{0:t}'" , textFileName.get()); |
399 | return 2; |
400 | } |
401 | } |
402 | if (!bbox) { |
403 | fputs(s: "</pre>\n" , stream: f); |
404 | } |
405 | fputs(s: "</body>\n" , stream: f); |
406 | fputs(s: "</html>\n" , stream: f); |
407 | if (f != stdout) { |
408 | fclose(stream: f); |
409 | } |
410 | } |
411 | |
412 | return 0; |
413 | } |
414 | |
415 | static void printInfoString(FILE *f, Dict *infoDict, const char *key, const char *text1, const char *text2, const UnicodeMap *uMap) |
416 | { |
417 | const GooString *s1; |
418 | bool isUnicode; |
419 | Unicode u; |
420 | char buf[9]; |
421 | int i, n; |
422 | |
423 | Object obj = infoDict->lookup(key); |
424 | if (obj.isString()) { |
425 | fputs(s: text1, stream: f); |
426 | s1 = obj.getString(); |
427 | if ((s1->getChar(i: 0) & 0xff) == 0xfe && (s1->getChar(i: 1) & 0xff) == 0xff) { |
428 | isUnicode = true; |
429 | i = 2; |
430 | } else { |
431 | isUnicode = false; |
432 | i = 0; |
433 | } |
434 | while (i < obj.getString()->getLength()) { |
435 | if (isUnicode) { |
436 | u = ((s1->getChar(i) & 0xff) << 8) | (s1->getChar(i: i + 1) & 0xff); |
437 | i += 2; |
438 | } else { |
439 | u = pdfDocEncoding[s1->getChar(i) & 0xff]; |
440 | ++i; |
441 | } |
442 | n = uMap->mapUnicode(u, buf, bufSize: sizeof(buf)); |
443 | buf[n] = '\0'; |
444 | const std::string myString = myXmlTokenReplace(inString: buf); |
445 | fputs(s: myString.c_str(), stream: f); |
446 | } |
447 | fputs(s: text2, stream: f); |
448 | } |
449 | } |
450 | |
451 | static void printInfoDate(FILE *f, Dict *infoDict, const char *key, const char *text1, const char *text2) |
452 | { |
453 | int year, mon, day, hour, min, sec, tz_hour, tz_minute; |
454 | char tz; |
455 | |
456 | Object obj = infoDict->lookup(key); |
457 | if (obj.isString()) { |
458 | const GooString *s = obj.getString(); |
459 | if (parseDateString(date: s, year: &year, month: &mon, day: &day, hour: &hour, minute: &min, second: &sec, tz: &tz, tzHour: &tz_hour, tzMinute: &tz_minute)) { |
460 | fputs(s: text1, stream: f); |
461 | fprintf(stream: f, format: "%04d-%02d-%02dT%02d:%02d:%02d" , year, mon, day, hour, min, sec); |
462 | if (tz_hour == 0 && tz_minute == 0) { |
463 | fprintf(stream: f, format: "Z" ); |
464 | } else { |
465 | fprintf(stream: f, format: "%c%02d" , tz, tz_hour); |
466 | if (tz_minute) { |
467 | fprintf(stream: f, format: ":%02d" , tz_minute); |
468 | } |
469 | } |
470 | fputs(s: text2, stream: f); |
471 | } |
472 | } |
473 | } |
474 | |
475 | static void printLine(FILE *f, const TextLine *line) |
476 | { |
477 | double xMin, yMin, xMax, yMax; |
478 | double lineXMin = 0, lineYMin = 0, lineXMax = 0, lineYMax = 0; |
479 | const TextWord *word; |
480 | std::stringstream wordXML; |
481 | wordXML << std::fixed << std::setprecision(6); |
482 | |
483 | for (word = line->getWords(); word; word = word->getNext()) { |
484 | word->getBBox(xMinA: &xMin, yMinA: &yMin, xMaxA: &xMax, yMaxA: &yMax); |
485 | |
486 | if (lineXMin == 0 || lineXMin > xMin) { |
487 | lineXMin = xMin; |
488 | } |
489 | if (lineYMin == 0 || lineYMin > yMin) { |
490 | lineYMin = yMin; |
491 | } |
492 | if (lineXMax < xMax) { |
493 | lineXMax = xMax; |
494 | } |
495 | if (lineYMax < yMax) { |
496 | lineYMax = yMax; |
497 | } |
498 | |
499 | GooString *wordText = word->getText(); |
500 | const std::string myString = myXmlTokenReplace(inString: wordText->c_str()); |
501 | wordXML << " <word xMin=\"" << xMin << "\" yMin=\"" << yMin << "\" xMax=\"" << xMax << "\" yMax=\"" << yMax << "\">" << myString << "</word>\n" ; |
502 | delete wordText; |
503 | } |
504 | fprintf(stream: f, format: " <line xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">\n" , lineXMin, lineYMin, lineXMax, lineYMax); |
505 | fputs(s: wordXML.str().c_str(), stream: f); |
506 | fputs(s: " </line>\n" , stream: f); |
507 | } |
508 | |
509 | void printDocBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last) |
510 | { |
511 | double xMin, yMin, xMax, yMax; |
512 | const TextFlow *flow; |
513 | const TextBlock *blk; |
514 | const TextLine *line; |
515 | |
516 | fprintf(stream: f, format: "<doc>\n" ); |
517 | for (int page = first; page <= last; ++page) { |
518 | const double wid = useCropBox ? doc->getPageCropWidth(page) : doc->getPageMediaWidth(page); |
519 | const double hgt = useCropBox ? doc->getPageCropHeight(page) : doc->getPageMediaHeight(page); |
520 | fprintf(stream: f, format: " <page width=\"%f\" height=\"%f\">\n" , wid, hgt); |
521 | doc->displayPage(out: textOut, page, hDPI: resolution, vDPI: resolution, rotate: 0, useMediaBox: !useCropBox, crop: useCropBox, printing: false); |
522 | for (flow = textOut->getFlows(); flow; flow = flow->getNext()) { |
523 | fprintf(stream: f, format: " <flow>\n" ); |
524 | for (blk = flow->getBlocks(); blk; blk = blk->getNext()) { |
525 | blk->getBBox(xMinA: &xMin, yMinA: &yMin, xMaxA: &xMax, yMaxA: &yMax); |
526 | fprintf(stream: f, format: " <block xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">\n" , xMin, yMin, xMax, yMax); |
527 | for (line = blk->getLines(); line; line = line->getNext()) { |
528 | printLine(f, line); |
529 | } |
530 | fprintf(stream: f, format: " </block>\n" ); |
531 | } |
532 | fprintf(stream: f, format: " </flow>\n" ); |
533 | } |
534 | fprintf(stream: f, format: " </page>\n" ); |
535 | } |
536 | fprintf(stream: f, format: "</doc>\n" ); |
537 | } |
538 | |
539 | void printTSVBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last) |
540 | { |
541 | double xMin = 0, yMin = 0, xMax = 0, yMax = 0; |
542 | const TextFlow *flow; |
543 | const TextBlock *blk; |
544 | const TextLine *line; |
545 | const TextWord *word; |
546 | int blockNum = 0; |
547 | int lineNum = 0; |
548 | int flowNum = 0; |
549 | int wordNum = 0; |
550 | const int pageLevel = 1; |
551 | const int blockLevel = 3; |
552 | const int lineLevel = 4; |
553 | const int wordLevel = 5; |
554 | const int metaConf = -1; |
555 | const int wordConf = 100; |
556 | |
557 | fputs(s: "level\tpage_num\tpar_num\tblock_num\tline_num\tword_num\tleft\ttop\twidth\theight\tconf\ttext\n" , stream: f); |
558 | |
559 | for (int page = first; page <= last; ++page) { |
560 | const double wid = useCropBox ? doc->getPageCropWidth(page) : doc->getPageMediaWidth(page); |
561 | const double hgt = useCropBox ? doc->getPageCropHeight(page) : doc->getPageMediaHeight(page); |
562 | |
563 | fprintf(stream: f, format: "%d\t%d\t%d\t%d\t%d\t%d\t%f\t%f\t%f\t%f\t%d\t###PAGE###\n" , pageLevel, page, flowNum, blockNum, lineNum, wordNum, xMin, yMin, wid, hgt, metaConf); |
564 | doc->displayPage(out: textOut, page, hDPI: resolution, vDPI: resolution, rotate: 0, useMediaBox: !useCropBox, crop: useCropBox, printing: false); |
565 | |
566 | for (flow = textOut->getFlows(); flow; flow = flow->getNext()) { |
567 | // flow->getBBox(&xMin, &yMin, &xMax, &yMax); |
568 | // fprintf(f, "%d\t%d\t%d\t%d\t%d\t%f\t%f\t%f\t%f\t\n", page,flowNum,blockNum,lineNum,wordNum,xMin,yMin,wid, hgt); |
569 | |
570 | for (blk = flow->getBlocks(); blk; blk = blk->getNext()) { |
571 | blk->getBBox(xMinA: &xMin, yMinA: &yMin, xMaxA: &xMax, yMaxA: &yMax); |
572 | fprintf(stream: f, format: "%d\t%d\t%d\t%d\t%d\t%d\t%f\t%f\t%f\t%f\t%d\t###FLOW###\n" , blockLevel, page, flowNum, blockNum, lineNum, wordNum, xMin, yMin, xMax - xMin, yMax - yMin, metaConf); |
573 | |
574 | for (line = blk->getLines(); line; line = line->getNext()) { |
575 | |
576 | double lxMin = 1E+37, lyMin = 1E+37; |
577 | double lxMax = 0, lyMax = 0; |
578 | GooString *lineWordsBuffer = new GooString(); |
579 | |
580 | for (word = line->getWords(); word; word = word->getNext()) { |
581 | word->getBBox(xMinA: &xMin, yMinA: &yMin, xMaxA: &xMax, yMaxA: &yMax); |
582 | if (lxMin > xMin) { |
583 | lxMin = xMin; |
584 | } |
585 | if (lxMax < xMax) { |
586 | lxMax = xMax; |
587 | } |
588 | if (lyMin > yMin) { |
589 | lyMin = yMin; |
590 | } |
591 | if (lyMax < yMax) { |
592 | lyMax = yMax; |
593 | } |
594 | |
595 | lineWordsBuffer->appendf(fmt: "{0:d}\t{1:d}\t{2:d}\t{3:d}\t{4:d}\t{5:d}\t{6:.2f}\t{7:.2f}\t{8:.2f}\t{9:.2f}\t{10:d}\t{11:t}\n" , wordLevel, page, flowNum, blockNum, lineNum, wordNum, xMin, yMin, xMax - xMin, yMax - yMin, |
596 | wordConf, word->getText()); |
597 | wordNum++; |
598 | } |
599 | |
600 | // Print Link Bounding Box info |
601 | fprintf(stream: f, format: "%d\t%d\t%d\t%d\t%d\t%d\t%f\t%f\t%f\t%f\t%d\t###LINE###\n" , lineLevel, page, flowNum, blockNum, lineNum, 0, lxMin, lyMin, lxMax - lxMin, lyMax - lyMin, metaConf); |
602 | fprintf(stream: f, format: "%s" , lineWordsBuffer->c_str()); |
603 | delete lineWordsBuffer; |
604 | wordNum = 0; |
605 | lineNum++; |
606 | } |
607 | lineNum = 0; |
608 | blockNum++; |
609 | } |
610 | blockNum = 0; |
611 | flowNum++; |
612 | } |
613 | flowNum = 0; |
614 | } |
615 | } |
616 | |
617 | void printWordBBox(FILE *f, PDFDoc *doc, TextOutputDev *textOut, int first, int last) |
618 | { |
619 | fprintf(stream: f, format: "<doc>\n" ); |
620 | for (int page = first; page <= last; ++page) { |
621 | double wid = useCropBox ? doc->getPageCropWidth(page) : doc->getPageMediaWidth(page); |
622 | double hgt = useCropBox ? doc->getPageCropHeight(page) : doc->getPageMediaHeight(page); |
623 | fprintf(stream: f, format: " <page width=\"%f\" height=\"%f\">\n" , wid, hgt); |
624 | doc->displayPage(out: textOut, page, hDPI: resolution, vDPI: resolution, rotate: 0, useMediaBox: !useCropBox, crop: useCropBox, printing: false); |
625 | std::unique_ptr<TextWordList> wordlist = textOut->makeWordList(); |
626 | const int word_length = wordlist != nullptr ? wordlist->getLength() : 0; |
627 | TextWord *word; |
628 | double xMinA, yMinA, xMaxA, yMaxA; |
629 | if (word_length == 0) { |
630 | fprintf(stderr, format: "no word list\n" ); |
631 | } |
632 | |
633 | for (int i = 0; i < word_length; ++i) { |
634 | word = wordlist->get(idx: i); |
635 | word->getBBox(xMinA: &xMinA, yMinA: &yMinA, xMaxA: &xMaxA, yMaxA: &yMaxA); |
636 | const std::string myString = myXmlTokenReplace(inString: word->getText()->c_str()); |
637 | fprintf(stream: f, format: " <word xMin=\"%f\" yMin=\"%f\" xMax=\"%f\" yMax=\"%f\">%s</word>\n" , xMinA, yMinA, xMaxA, yMaxA, myString.c_str()); |
638 | } |
639 | fprintf(stream: f, format: " </page>\n" ); |
640 | } |
641 | fprintf(stream: f, format: "</doc>\n" ); |
642 | } |
643 | |