| 1 |
/* PDFAnnotExtractor.java |
| 2 |
Copyright (C) 2006-2008, 2011, 2012 Heiko Oberdiek |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU General Public License as published by |
| 6 |
the Free Software Foundation; either version 2 of the License, or |
| 7 |
(at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 17 |
MA 02110-1301 USA |
| 18 |
|
| 19 |
This file is part of PDFAnnotExtractor. See README. |
| 20 |
*/ |
| 21 |
package pax; |
| 22 |
|
| 23 |
import java.io.*; |
| 24 |
import java.text.*; |
| 25 |
import java.util.*; |
| 26 |
|
| 27 |
import org.apache.pdfbox.cos.*; |
| 28 |
import org.apache.pdfbox.pdfparser.*; |
| 29 |
import org.apache.pdfbox.pdmodel.*; |
| 30 |
import org.apache.pdfbox.pdmodel.common.*; |
| 31 |
import org.apache.pdfbox.pdmodel.interactive.action.*; |
| 32 |
import org.apache.pdfbox.pdmodel.interactive.action.type.*; |
| 33 |
import org.apache.pdfbox.pdmodel.interactive.annotation.*; |
| 34 |
import org.apache.pdfbox.pdmodel.interactive.documentnavigation.destination.*; |
| 35 |
|
| 36 |
public class PDFAnnotExtractor implements Constants { |
| 37 |
|
| 38 |
protected File inputFile; |
| 39 |
protected File outputFile; |
| 40 |
protected PDDocument doc; |
| 41 |
protected PDDocumentCatalog catalog; |
| 42 |
protected Entry entry; |
| 43 |
protected int destCount; |
| 44 |
|
| 45 |
protected static final String USAGE = |
| 46 |
"Syntax: java PDFAnnotExtractor <pdffiles[.pdf]>"; |
| 47 |
protected static final String EXT_PDF = ".pdf"; |
| 48 |
protected static final String EXT_PAX = ".pax"; |
| 49 |
|
| 50 |
public static void main(String[] argv) { |
| 51 |
if (argv.length < 1) { |
| 52 |
System.err.println(USAGE); |
| 53 |
System.exit(1); |
| 54 |
} |
| 55 |
for(int i = 0; i < argv.length; i++) { |
| 56 |
processFile(argv[i]); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
public static void processFile(String fileName) { |
| 61 |
File file = new File(fileName); |
| 62 |
if (file.isFile()) { |
| 63 |
fileName = stripFromEnd(fileName, EXT_PDF); |
| 64 |
} |
| 65 |
else { |
| 66 |
File testFile = new File(fileName + EXT_PDF); |
| 67 |
if (testFile.isFile()) { |
| 68 |
file = testFile; |
| 69 |
} |
| 70 |
else { |
| 71 |
System.err.println(USAGE); |
| 72 |
error("PDF file not found: " + fileName, null); |
| 73 |
} |
| 74 |
} |
| 75 |
System.out.println("* Processing file `" + file.toString() + "' ..."); |
| 76 |
PDFAnnotExtractor p = |
| 77 |
new PDFAnnotExtractor(file, new File(fileName + EXT_PAX)); |
| 78 |
p.parse(); |
| 79 |
p.close(); |
| 80 |
} |
| 81 |
|
| 82 |
public PDFAnnotExtractor(File inputFile, File outputFile) { |
| 83 |
this.inputFile = inputFile; |
| 84 |
this.outputFile = outputFile; |
| 85 |
destCount = 0; |
| 86 |
try { |
| 87 |
doc = PDDocument.load(inputFile); |
| 88 |
catalog = doc.getDocumentCatalog(); |
| 89 |
} |
| 90 |
catch (IOException e) { |
| 91 |
error("Loading failed: " + inputFile, e); |
| 92 |
} |
| 93 |
try { |
| 94 |
entry = new Entry(new BufferedWriter(new FileWriter(outputFile))); |
| 95 |
} |
| 96 |
catch (IOException e) { |
| 97 |
error("Cannot open output: " + outputFile, e); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
public void close() { |
| 102 |
try { |
| 103 |
doc.close(); |
| 104 |
} |
| 105 |
catch (IOException e) { |
| 106 |
error("Closing failed: " + inputFile, e); |
| 107 |
} |
| 108 |
try { |
| 109 |
entry.close(); |
| 110 |
} |
| 111 |
catch (EntryWriteException e) { |
| 112 |
error("Closing failed: " + outputFile, e); |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
public void parse() { |
| 117 |
cmd_pax(); |
| 118 |
cmd_file(); |
| 119 |
cmd_pagenum(); |
| 120 |
cmd_baseurl(); |
| 121 |
parse_pages(); |
| 122 |
} |
| 123 |
|
| 124 |
public void cmd_pax() { |
| 125 |
try { |
| 126 |
// PAX version info |
| 127 |
entry.setCmd(CMD_PAX); |
| 128 |
entry.addArg(PAX_VERSION); |
| 129 |
entry.write(); |
| 130 |
} |
| 131 |
catch (Exception e) { handleCmdException(e); } |
| 132 |
} |
| 133 |
|
| 134 |
public void cmd_file() { |
| 135 |
try { |
| 136 |
entry.setCmd(CMD_FILE); |
| 137 |
entry.addArg(formatString(inputFile.toString())); |
| 138 |
long size = inputFile.length(); |
| 139 |
if (size > 0) { |
| 140 |
entry.putKV(KEY_SIZE, "" + size); |
| 141 |
} |
| 142 |
long time = inputFile.lastModified(); |
| 143 |
if (time != 0) { |
| 144 |
Date d = new Date(time); |
| 145 |
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss"); |
| 146 |
String date = "D:" + df.format(d); |
| 147 |
df = new SimpleDateFormat("Z"); |
| 148 |
String zone = df.format(d); |
| 149 |
if (zone.equals("+0000")) { |
| 150 |
zone = "Z"; |
| 151 |
} |
| 152 |
else { |
| 153 |
// ISO format: SIGN DIGIT DIGIT DIGIT DIGIT |
| 154 |
zone = zone.substring(0, 3) |
| 155 |
+ "'" |
| 156 |
+ zone.substring(3, 5) |
| 157 |
+ "'"; |
| 158 |
} |
| 159 |
entry.putKV(KEY_DATE, date + zone); |
| 160 |
} |
| 161 |
entry.write(); |
| 162 |
} |
| 163 |
catch (Exception e) { handleCmdException(e); } |
| 164 |
} |
| 165 |
|
| 166 |
public void cmd_pagenum() { |
| 167 |
try { |
| 168 |
// pagenum |
| 169 |
int pagenum = doc.getNumberOfPages(); |
| 170 |
entry.setCmd(CMD_PAGENUM); |
| 171 |
entry.addArg("" + pagenum); |
| 172 |
entry.write(); |
| 173 |
} |
| 174 |
catch (Exception e) { handleCmdException(e); } |
| 175 |
} |
| 176 |
|
| 177 |
public void cmd_info() { |
| 178 |
try { |
| 179 |
// info |
| 180 |
entry.setCmd(CMD_INFO); |
| 181 |
PDDocumentInformation info = doc.getDocumentInformation(); |
| 182 |
entry.putKV(KEY_AUTHOR, formatString(info.getAuthor())); |
| 183 |
entry.putKV(KEY_CREATOR, formatString(info.getCreator())); |
| 184 |
entry.putKV(KEY_KEYWORDS, formatString(info.getKeywords())); |
| 185 |
entry.putKV(KEY_PRODUCER, formatString(info.getProducer())); |
| 186 |
entry.putKV(KEY_SUBJECT, formatString(info.getSubject())); |
| 187 |
entry.putKV(KEY_TITLE, formatString(info.getTitle())); |
| 188 |
if (!entry.isEmptyKV()) { |
| 189 |
entry.write(); |
| 190 |
} |
| 191 |
} |
| 192 |
catch (Exception e) { handleCmdException(e); } |
| 193 |
} |
| 194 |
|
| 195 |
public void cmd_baseurl() { |
| 196 |
try { |
| 197 |
// baseurl |
| 198 |
entry.setCmd(CMD_BASEURL); |
| 199 |
COSDictionary dict = catalog.getCOSDictionary(); |
| 200 |
dict = (COSDictionary) |
| 201 |
dict.getItem(COSName.getPDFName(PDF_URI)); |
| 202 |
String baseurl = dict.getString(PDF_BASE); |
| 203 |
if (baseurl.length() > 0) { |
| 204 |
entry.addArg(formatString(baseurl)); |
| 205 |
entry.write(); |
| 206 |
} |
| 207 |
} |
| 208 |
catch (EntryWriteException e) { handleCmdException(e); } |
| 209 |
catch (Exception e) {} |
| 210 |
} |
| 211 |
|
| 212 |
public void parse_pages() { |
| 213 |
ListIterator iter = catalog.getAllPages().listIterator(); |
| 214 |
while (iter.hasNext()) { |
| 215 |
// page |
| 216 |
PDPage page = (PDPage)iter.next(); |
| 217 |
int num = iter.nextIndex(); |
| 218 |
parse_page(num, page); |
| 219 |
} |
| 220 |
try { |
| 221 |
entry.flushDelayed(); |
| 222 |
} |
| 223 |
catch (EntryWriteException e) { |
| 224 |
System.err.println("Write error: " + outputFile); |
| 225 |
System.err.println(e.getCause().toString()); |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
public void parse_page(int num, PDPage page) { |
| 230 |
cmd_page(num, page); |
| 231 |
parse_annots(num, page); |
| 232 |
} |
| 233 |
|
| 234 |
public void cmd_page(int num, PDPage page) { |
| 235 |
try { |
| 236 |
entry.setCmd(CMD_PAGE); |
| 237 |
entry.addArg("" + num); |
| 238 |
entry.withKV(); |
| 239 |
|
| 240 |
// rotate entry |
| 241 |
int rot = page.findRotation(); |
| 242 |
if (rot != 0) { |
| 243 |
entry.putKV(KEY_ROTATE, "" + rot); |
| 244 |
} |
| 245 |
|
| 246 |
// box entries |
| 247 |
PDRectangle mediaBox = page.findMediaBox(); |
| 248 |
PDRectangle cropBox = page.findCropBox(); |
| 249 |
PDRectangle bleedBox = page.getBleedBox(); |
| 250 |
PDRectangle trimBox = page.getTrimBox(); |
| 251 |
PDRectangle artBox = page.getArtBox(); |
| 252 |
entry.addArg(formatBox(mediaBox)); |
| 253 |
if (!equals(cropBox, mediaBox)) { |
| 254 |
entry.putKV(KEY_CROP_BOX, formatBox(cropBox)); |
| 255 |
} |
| 256 |
if (!equals(bleedBox, cropBox)) { |
| 257 |
entry.putKV(KEY_BLEED_BOX, formatBox(bleedBox)); |
| 258 |
} |
| 259 |
if (!equals(trimBox, cropBox)) { |
| 260 |
entry.putKV(KEY_TRIM_BOX, formatBox(trimBox)); |
| 261 |
} |
| 262 |
if (!equals(artBox, cropBox)) { |
| 263 |
entry.putKV(KEY_ART_BOX, formatBox(artBox)); |
| 264 |
} |
| 265 |
entry.write(); |
| 266 |
} |
| 267 |
catch (Exception e) { handleCmdException(e); } |
| 268 |
} |
| 269 |
|
| 270 |
public void parse_annots(int num, PDPage page) { |
| 271 |
// get annotations |
| 272 |
COSArray annots = null; |
| 273 |
try { |
| 274 |
COSDictionary page_dict = page.getCOSDictionary(); |
| 275 |
annots = (COSArray)page_dict.getDictionaryObject(COSName.ANNOTS); |
| 276 |
} |
| 277 |
catch (Exception e) {} |
| 278 |
if (annots == null) { |
| 279 |
return; |
| 280 |
} |
| 281 |
|
| 282 |
for (int i = 0; i < annots.size(); i++) { |
| 283 |
try { |
| 284 |
COSDictionary annot = (COSDictionary)annots.getObject(i); |
| 285 |
cmd_annot(num, annot); |
| 286 |
} |
| 287 |
catch (Exception e) {} |
| 288 |
} |
| 289 |
} |
| 290 |
|
| 291 |
public void cmd_annot(int num, COSDictionary annot) { |
| 292 |
try { |
| 293 |
entry.setCmd(CMD_ANNOT); |
| 294 |
entry.addArg("" + num); |
| 295 |
|
| 296 |
// Type |
| 297 |
String type = annot.getNameAsString(COSName.TYPE, PDF_ANNOT); |
| 298 |
if (!type.equals(PDF_ANNOT)) { |
| 299 |
throw new Exception("Wrong annotation type: " + type); |
| 300 |
} |
| 301 |
// Subtype |
| 302 |
String subtype = annot.getNameAsString(COSName.SUBTYPE); |
| 303 |
if (subtype == null) { |
| 304 |
throw new Exception("Missing annotation subtype."); |
| 305 |
} |
| 306 |
if (!subtype.equals(PDF_LINK)) { |
| 307 |
throw new Exception("Unsupported annotation subtype: " |
| 308 |
+ subtype); |
| 309 |
} |
| 310 |
entry.addArg(subtype); |
| 311 |
entry.withKV(); |
| 312 |
// Rect |
| 313 |
COSArray array = (COSArray) |
| 314 |
annot.getDictionaryObject(PDF_RECT); |
| 315 |
PDRectangle rect = new PDRectangle(array); |
| 316 |
entry.addArg(formatBox(rect)); |
| 317 |
// A |
| 318 |
COSDictionary a = (COSDictionary) |
| 319 |
annot.getDictionaryObject(PDF_A); |
| 320 |
if (a == null) { |
| 321 |
COSBase cos = annot.getDictionaryObject(PDF_DEST); |
| 322 |
if (cos == null) { |
| 323 |
throw new Exception("Unsupported link annotation " |
| 324 |
+ "without action."); |
| 325 |
} |
| 326 |
entry.addArg(PDF_GOTO); |
| 327 |
destCount++; |
| 328 |
entry.putKV(KEY_DEST_LABEL, "" + destCount); |
| 329 |
annot_attrs(annot); |
| 330 |
entry.write(); |
| 331 |
add_dest(PDDestination.create(cos)); |
| 332 |
entry.writeDelayed(); |
| 333 |
return; |
| 334 |
} |
| 335 |
PDAction action = PDActionFactory.createAction(a); |
| 336 |
if (action == null) { |
| 337 |
// try named action, unsupported by PDFBox |
| 338 |
if (a.getNameAsString(PDF_S).equals(PDF_NAMED)) { |
| 339 |
entry.addArg(PDF_NAMED); |
| 340 |
String name = a.getNameAsString(PDF_N); |
| 341 |
if (name != null) { |
| 342 |
entry.putKV(KEY_NAME, name); |
| 343 |
annot_attrs(annot); |
| 344 |
entry.write(); |
| 345 |
return; |
| 346 |
} |
| 347 |
} |
| 348 |
throw new Exception("Unsupported link annotation."); |
| 349 |
} |
| 350 |
String actionSubtype = action.getSubType(); |
| 351 |
entry.addArg(actionSubtype); |
| 352 |
if (action instanceof PDActionURI) { |
| 353 |
PDActionURI uri = (PDActionURI)action; |
| 354 |
entry.putKV(KEY_URI, formatString(uri.getURI())); |
| 355 |
if (uri.shouldTrackMousePosition()) { |
| 356 |
entry.putKV(KEY_IS_MAP, null); |
| 357 |
} |
| 358 |
} |
| 359 |
else if (action instanceof PDActionGoTo) { |
| 360 |
destCount++; |
| 361 |
entry.putKV(KEY_DEST_LABEL, "" + destCount); |
| 362 |
annot_attrs(annot); |
| 363 |
entry.write(); |
| 364 |
add_dest(((PDActionGoTo)action).getDestination()); |
| 365 |
entry.writeDelayed(); |
| 366 |
return; |
| 367 |
} |
| 368 |
else if (action instanceof PDActionRemoteGoTo) { |
| 369 |
PDActionRemoteGoTo gotor = (PDActionRemoteGoTo)action; |
| 370 |
String file = gotor.getFile().getFile(); |
| 371 |
if (file == null || file.length() == 0) { |
| 372 |
throw new Exception("GoToR: missing file"); |
| 373 |
} |
| 374 |
entry.putKV(KEY_FILE, formatString(gotor.getFile().getFile())); |
| 375 |
|
| 376 |
/* |
| 377 |
PDDestination d = PDDestination.create(gotor.getD()); |
| 378 |
String view = "/"; |
| 379 |
if (d instanceof PDNamedDestination) { |
| 380 |
entry.putKV(KEY_DEST_NAME, |
| 381 |
((PDNamedDestination)d) |
| 382 |
.getNamedDestination()); |
| 383 |
} |
| 384 |
else { |
| 385 |
if (d instanceof PDPageFitDestination) { |
| 386 |
PDPageFitDestination p = |
| 387 |
(PDPageFitDestination)d; |
| 388 |
view += (p.fitBoundingBox()) ? PDF_FITB : PDF_FIT; |
| 389 |
} |
| 390 |
else if (d instanceof PDPageFitHeightDestination) { |
| 391 |
PDPageFitHeightDestination p = |
| 392 |
(PDPageFitHeightDestination)d; |
| 393 |
view += (p.fitBoundingBox()) ? PDF_FITBV : PDF_FITV; |
| 394 |
view += " "; |
| 395 |
view += (p.getLeft() == -1) ? PDF_NULL : "" + p.getLeft(); |
| 396 |
} |
| 397 |
else if (d instanceof PDPageFitWidthDestination) { |
| 398 |
PDPageFitWidthDestination p = |
| 399 |
(PDPageFitWidthDestination)d; |
| 400 |
view += (p.fitBoundingBox()) ? PDF_FITBH : PDF_FITH; |
| 401 |
view += " "; |
| 402 |
view += (p.getTop() == -1) ? PDF_NULL : "" + p.getTop(); |
| 403 |
} |
| 404 |
else if (d instanceof PDPageXYZDestination) { |
| 405 |
PDPageXYZDestination p = |
| 406 |
(PDPageXYZDestination)d; |
| 407 |
view += PDF_XYZ; |
| 408 |
view += " "; |
| 409 |
view += (p.getLeft() == -1) ? PDF_NULL : "" + p.getLeft(); |
| 410 |
view += " "; |
| 411 |
view += (p.getTop() == -1) ? PDF_NULL : "" + p.getTop(); |
| 412 |
view += " "; |
| 413 |
view += (p.getZoom() == -1) ? PDF_NULL : "" + p.getZoom(); |
| 414 |
} |
| 415 |
else if (d instanceof PDPageFitRectangleDestination) { |
| 416 |
PDPageFitRectangleDestination p = |
| 417 |
(PDPageFitRectangleDestination)d; |
| 418 |
view += PDF_FITR; |
| 419 |
view += " " + p.getLeft() + " " + p.getBottom(); |
| 420 |
view += " " + p.getRight() + " " + p.getTop(); |
| 421 |
} |
| 422 |
else { |
| 423 |
throw new Exception("Unknown destination type"); |
| 424 |
} |
| 425 |
entry.putKV(KEY_DEST_VIEW, view); |
| 426 |
} |
| 427 |
*/ |
| 428 |
|
| 429 |
COSBase d = gotor.getD(); |
| 430 |
if (d instanceof COSString) { |
| 431 |
entry.putKV(KEY_DEST_NAME, |
| 432 |
formatString(((COSString)d).getString())); |
| 433 |
} |
| 434 |
else if (d instanceof COSName) { |
| 435 |
entry.putKV(KEY_DEST_NAME, |
| 436 |
formatString(((COSName)d).getName())); |
| 437 |
} |
| 438 |
else if (d instanceof COSArray) { |
| 439 |
COSArray dest = (COSArray)d; |
| 440 |
int page_num = dest.getInt(0); |
| 441 |
entry.putKV(KEY_DEST_PAGE, "" + page_num); |
| 442 |
String view = dest.getName(1); |
| 443 |
int size = dest.size(); |
| 444 |
if (view.equals(PDF_FIT) || view.equals(PDF_FITB)) { |
| 445 |
} |
| 446 |
else if (view.equals(PDF_FITH) || view.equals(PDF_FITBH) |
| 447 |
|| view.equals(PDF_FITV) || view.equals(PDF_FITBV)) { |
| 448 |
if (size >= 3) { |
| 449 |
try { |
| 450 |
String args = formatNumber( |
| 451 |
((COSNumber)dest.getObject(2)).floatValue()); |
| 452 |
view += " " + args; |
| 453 |
} |
| 454 |
catch (Exception e) { |
| 455 |
size = 0; |
| 456 |
} |
| 457 |
} |
| 458 |
if (size < 3) { |
| 459 |
if (view.equals(PDF_FITH) || view.equals(PDF_FITV)) { |
| 460 |
view = PDF_FIT; |
| 461 |
} |
| 462 |
else { |
| 463 |
view = PDF_FITB; |
| 464 |
} |
| 465 |
} |
| 466 |
} |
| 467 |
else if (view.equals(PDF_XYZ)) { |
| 468 |
COSBase obj; |
| 469 |
view += " "; |
| 470 |
if (size >= 3) { |
| 471 |
obj = dest.getObject(2); |
| 472 |
if (obj instanceof COSNumber) { |
| 473 |
view += formatNumber(((COSNumber)obj).floatValue()); |
| 474 |
} |
| 475 |
else { |
| 476 |
view += PDF_NULL; |
| 477 |
} |
| 478 |
} |
| 479 |
else { |
| 480 |
view += PDF_NULL; |
| 481 |
} |
| 482 |
view += " "; |
| 483 |
if (size >= 4) { |
| 484 |
obj = dest.getObject(3); |
| 485 |
if (obj instanceof COSNumber) { |
| 486 |
view += formatNumber(((COSNumber)obj).floatValue()); |
| 487 |
} |
| 488 |
else { |
| 489 |
view += PDF_NULL; |
| 490 |
} |
| 491 |
} |
| 492 |
else { |
| 493 |
view += PDF_NULL; |
| 494 |
} |
| 495 |
view += " "; |
| 496 |
if (size >= 5) { |
| 497 |
obj = dest.getObject(4); |
| 498 |
if (obj instanceof COSNumber) { |
| 499 |
view += formatNumber(((COSNumber)obj).floatValue()); |
| 500 |
} |
| 501 |
else { |
| 502 |
view += PDF_NULL; |
| 503 |
} |
| 504 |
} |
| 505 |
else { |
| 506 |
view += PDF_NULL; |
| 507 |
} |
| 508 |
} |
| 509 |
else if (view.equals(PDF_FITR)) { |
| 510 |
view += " " |
| 511 |
+ formatNumber(((COSNumber)dest.getObject(2)).floatValue()) |
| 512 |
+ " " |
| 513 |
+ formatNumber(((COSNumber)dest.getObject(3)).floatValue()) |
| 514 |
+ " " |
| 515 |
+ formatNumber(((COSNumber)dest.getObject(4)).floatValue()) |
| 516 |
+ " " |
| 517 |
+ formatNumber(((COSNumber)dest.getObject(5)).floatValue()); |
| 518 |
} |
| 519 |
else { |
| 520 |
throw new Exception("Unknown destination view type"); |
| 521 |
} |
| 522 |
entry.putKV(KEY_DEST_VIEW, "/" + view); |
| 523 |
} |
| 524 |
else { |
| 525 |
throw new Exception("GoToR: unknown dest type"); |
| 526 |
} |
| 527 |
} |
| 528 |
else { |
| 529 |
throw new Exception("Unsupported link annotation type: " |
| 530 |
+ actionSubtype); |
| 531 |
} |
| 532 |
annot_attrs(annot); |
| 533 |
entry.write(); |
| 534 |
} |
| 535 |
catch (EntryWriteException e) { handleCmdException(e); } |
| 536 |
catch (Exception e) { |
| 537 |
System.err.println("!!! Warning: Annotation on page " + num |
| 538 |
+ " not recognized!"); |
| 539 |
System.err.println(" " + e.toString()); |
| 540 |
String msg = e.getMessage(); |
| 541 |
Throwable cause = e.getCause(); |
| 542 |
if (cause != null) { |
| 543 |
System.err.println(" " + cause.toString()); |
| 544 |
} |
| 545 |
if (e instanceof ClassCastException) { |
| 546 |
e.printStackTrace(); |
| 547 |
} |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
private void add_dest(PDDestination dest) throws Exception { |
| 552 |
entry.setCmd(CMD_DEST); |
| 553 |
entry.withKV(); |
| 554 |
if (dest instanceof PDNamedDestination) { |
| 555 |
String name = ((PDNamedDestination)dest).getNamedDestination(); |
| 556 |
Object obj = catalog.getNames().getDests().getValue(name); |
| 557 |
if (obj instanceof PDDestination) { |
| 558 |
dest = (PDDestination)obj; |
| 559 |
} |
| 560 |
} |
| 561 |
if (dest instanceof PDPageDestination) { |
| 562 |
PDPageDestination pd = (PDPageDestination)dest; |
| 563 |
PDPage page = pd.getPage(); |
| 564 |
int pagenum = catalog.getAllPages().indexOf(page); |
| 565 |
if (pagenum < 0) { |
| 566 |
throw new Exception("Link to unknown page"); |
| 567 |
} |
| 568 |
entry.addArg("" + (pagenum + 1)); |
| 569 |
entry.addArg("" + destCount); |
| 570 |
if (pd instanceof PDPageFitDestination) { |
| 571 |
PDPageFitDestination d = (PDPageFitDestination)pd; |
| 572 |
entry.addArg(d.fitBoundingBox() ? PDF_FITB : PDF_FIT); |
| 573 |
return; |
| 574 |
} |
| 575 |
if (pd instanceof PDPageFitHeightDestination) { |
| 576 |
PDPageFitHeightDestination d = (PDPageFitHeightDestination)pd; |
| 577 |
entry.addArg(d.fitBoundingBox() ? PDF_FITBV : PDF_FITV); |
| 578 |
COSArray a = d.getCOSArray(); |
| 579 |
if (a.size() > 2) { |
| 580 |
COSNumber n = (COSNumber)a.getObject(2); |
| 581 |
if (n != null) { |
| 582 |
entry.putKV(KEY_DEST_X, formatNumber(n)); |
| 583 |
} |
| 584 |
} |
| 585 |
return; |
| 586 |
} |
| 587 |
if (pd instanceof PDPageFitWidthDestination) { |
| 588 |
PDPageFitWidthDestination d = (PDPageFitWidthDestination)pd; |
| 589 |
entry.addArg(d.fitBoundingBox() ? PDF_FITBH : PDF_FITH); |
| 590 |
COSArray a = d.getCOSArray(); |
| 591 |
if (a.size() > 2) { |
| 592 |
COSNumber n = (COSNumber)a.getObject(2); |
| 593 |
if (n != null) { |
| 594 |
entry.putKV(KEY_DEST_Y, formatNumber(n)); |
| 595 |
} |
| 596 |
} |
| 597 |
return; |
| 598 |
} |
| 599 |
if (pd instanceof PDPageFitRectangleDestination) { |
| 600 |
PDPageFitRectangleDestination d = (PDPageFitRectangleDestination)pd; |
| 601 |
entry.addArg(PDF_FITR); |
| 602 |
COSArray a = d.getCOSArray(); |
| 603 |
int size = a.size(); |
| 604 |
if (size != 6) { |
| 605 |
throw new Exception("Rectangle destination without" |
| 606 |
+ " correct number of parameters"); |
| 607 |
} |
| 608 |
entry.putKV(KEY_DEST_RECT, |
| 609 |
formatNumber((COSNumber)a.getObject(2)) + " " |
| 610 |
+ formatNumber((COSNumber)a.getObject(3)) + " " |
| 611 |
+ formatNumber((COSNumber)a.getObject(4)) + " " |
| 612 |
+ formatNumber((COSNumber)a.getObject(5))); |
| 613 |
return; |
| 614 |
} |
| 615 |
if (pd instanceof PDPageXYZDestination) { |
| 616 |
PDPageXYZDestination d = (PDPageXYZDestination)pd; |
| 617 |
entry.addArg(PDF_XYZ); |
| 618 |
COSArray a = d.getCOSArray(); |
| 619 |
int size = a.size(); |
| 620 |
COSNumber n; |
| 621 |
if (size > 2) { |
| 622 |
n = (COSNumber)a.getObject(2); |
| 623 |
if (n != null) { |
| 624 |
entry.putKV(KEY_DEST_X, formatNumber(n)); |
| 625 |
} |
| 626 |
} |
| 627 |
if (size > 3) { |
| 628 |
n = (COSNumber)a.getObject(3); |
| 629 |
if (n != null) { |
| 630 |
entry.putKV(KEY_DEST_Y, formatNumber(n)); |
| 631 |
} |
| 632 |
} |
| 633 |
if (size > 4) { |
| 634 |
n = (COSNumber)a.getObject(4); |
| 635 |
if (n != null) { |
| 636 |
entry.putKV(KEY_DEST_ZOOM, formatNumber(n)); |
| 637 |
} |
| 638 |
} |
| 639 |
return; |
| 640 |
} |
| 641 |
} |
| 642 |
throw new Exception("Unknown destination type"); |
| 643 |
} |
| 644 |
|
| 645 |
protected void annot_attrs(COSDictionary annot) { |
| 646 |
put_attr(annot, PDF_C, KEY_C); |
| 647 |
put_attr(annot, PDF_BORDER, KEY_BORDER); |
| 648 |
put_attr(annot, PDF_BS, KEY_BS); |
| 649 |
put_attr(annot, PDF_H, KEY_H); |
| 650 |
} |
| 651 |
|
| 652 |
protected void put_attr(COSDictionary annot, |
| 653 |
String pdfKey, String entryKey) { |
| 654 |
try { |
| 655 |
COSBase value = annot.getDictionaryObject(pdfKey); |
| 656 |
if (value == null) { |
| 657 |
return; |
| 658 |
} |
| 659 |
String str = value.accept(new StringVisitor()).toString(); |
| 660 |
if (str == null || str.length() == 0) { |
| 661 |
return; |
| 662 |
} |
| 663 |
entry.putKV(entryKey, str); |
| 664 |
} |
| 665 |
catch (Exception e) {} |
| 666 |
} |
| 667 |
|
| 668 |
protected boolean equals(PDRectangle a, PDRectangle b) { |
| 669 |
return a.getLowerLeftX() == b.getLowerLeftX() |
| 670 |
&& a.getLowerLeftY() == b.getLowerLeftY() |
| 671 |
&& a.getUpperRightX() == b.getUpperRightX() |
| 672 |
&& a.getUpperRightY() == b.getUpperRightY(); |
| 673 |
} |
| 674 |
|
| 675 |
protected String formatBox(PDRectangle r) { |
| 676 |
return formatNumber(r.getLowerLeftX()) + ' ' |
| 677 |
+ formatNumber(r.getLowerLeftY()) + ' ' |
| 678 |
+ formatNumber(r.getUpperRightX()) + ' ' |
| 679 |
+ formatNumber(r.getUpperRightY()); |
| 680 |
} |
| 681 |
|
| 682 |
protected String formatString(String s) { |
| 683 |
if (s == null || s.length() == 0) { |
| 684 |
return s; |
| 685 |
} |
| 686 |
return HEX_STR_BEG |
| 687 |
+ (new COSString(s)).getHexString() |
| 688 |
+ HEX_STR_END; |
| 689 |
} |
| 690 |
|
| 691 |
protected String formatNumber(float f) { |
| 692 |
String s = "" + f; |
| 693 |
return stripFromEnd(s, ".0"); |
| 694 |
} |
| 695 |
protected String formatNumber(COSNumber obj) { |
| 696 |
return formatNumber(obj.floatValue()); |
| 697 |
} |
| 698 |
|
| 699 |
protected static String stripFromEnd(String str, String ext) { |
| 700 |
if (str.endsWith(ext)) { |
| 701 |
return str.substring(0, str.length() - ext.length()); |
| 702 |
} |
| 703 |
return str; |
| 704 |
} |
| 705 |
|
| 706 |
protected static void error(String msg, Exception e, boolean stackTrace) { |
| 707 |
System.err.println("!!! Error: " + msg); |
| 708 |
if (e != null) { |
| 709 |
if (stackTrace) { |
| 710 |
e.printStackTrace(); |
| 711 |
} |
| 712 |
else { |
| 713 |
System.err.println(e.toString()); |
| 714 |
} |
| 715 |
} |
| 716 |
System.exit(1); |
| 717 |
} |
| 718 |
protected static void error(String msg, Exception e) { |
| 719 |
error(msg, e, false); |
| 720 |
} |
| 721 |
|
| 722 |
protected void handleCmdException(Exception e) { |
| 723 |
System.err.println("!!! Error during write of entry `" |
| 724 |
+ entry.getCmd() + "'!\n"); |
| 725 |
if (e instanceof EntryWriteException) { |
| 726 |
System.err.println("Write error: " + outputFile); |
| 727 |
System.err.println(((EntryWriteException)e).getCause().toString()); |
| 728 |
} |
| 729 |
else { |
| 730 |
e.printStackTrace(); |
| 731 |
} |
| 732 |
} |
| 733 |
} |