Convert the PDF's destination names into a Python dict. The only parameter is the pymupdf.Document. All names found in the catalog under keys "/Dests" and "/Names/Dests" are being included. Returns: A dcitionary with the following layout: - k
(self)
| 6311 | self.load_page(i).recolor(components) |
| 6312 | |
| 6313 | def resolve_names(self): |
| 6314 | """Convert the PDF's destination names into a Python dict. |
| 6315 | |
| 6316 | The only parameter is the pymupdf.Document. |
| 6317 | All names found in the catalog under keys "/Dests" and "/Names/Dests" are |
| 6318 | being included. |
| 6319 | |
| 6320 | Returns: |
| 6321 | A dcitionary with the following layout: |
| 6322 | - key: (str) the name |
| 6323 | - value: (dict) with the following layout: |
| 6324 | * "page": target page number (0-based). If no page number found -1. |
| 6325 | * "to": (x, y) target point on page - currently in PDF coordinates, |
| 6326 | i.e. point (0,0) is the bottom-left of the page. |
| 6327 | * "zoom": (float) the zoom factor |
| 6328 | * "dest": (str) only occurs if the target location on the page has |
| 6329 | not been provided as "/XYZ" or if no page number was found. |
| 6330 | Examples: |
| 6331 | {'__bookmark_1': {'page': 0, 'to': (0.0, 541.0), 'zoom': 0.0}, |
| 6332 | '__bookmark_2': {'page': 0, 'to': (0.0, 481.45), 'zoom': 0.0}} |
| 6333 | |
| 6334 | or |
| 6335 | |
| 6336 | '21154a7c20684ceb91f9c9adc3b677c40': {'page': -1, 'dest': '/XYZ 15.75 1486 0'}, ... |
| 6337 | """ |
| 6338 | if hasattr(self, "_resolved_names"): # do not execute multiple times! |
| 6339 | return self._resolved_names |
| 6340 | # this is a backward listing of page xref to page number |
| 6341 | page_xrefs = {self.page_xref(i): i for i in range(self.page_count)} |
| 6342 | |
| 6343 | def obj_string(obj): |
| 6344 | """Return string version of a PDF object definition.""" |
| 6345 | buffer = mupdf.fz_new_buffer(512) |
| 6346 | output = mupdf.FzOutput(buffer) |
| 6347 | mupdf.pdf_print_obj(output, obj, 1, 0) |
| 6348 | output.fz_close_output() |
| 6349 | return JM_UnicodeFromBuffer(buffer) |
| 6350 | |
| 6351 | def get_array(val): |
| 6352 | """Generate value of one item of the names dictionary.""" |
| 6353 | templ_dict = {"page": -1, "dest": ""} # value template |
| 6354 | if val.pdf_is_indirect(): |
| 6355 | val = mupdf.pdf_resolve_indirect(val) |
| 6356 | if val.pdf_is_array(): |
| 6357 | array = obj_string(val) |
| 6358 | elif val.pdf_is_dict(): |
| 6359 | array = obj_string(mupdf.pdf_dict_gets(val, "D")) |
| 6360 | else: # if all fails return the empty template |
| 6361 | return templ_dict |
| 6362 | |
| 6363 | # replace PDF "null" by zero, omit the square brackets |
| 6364 | array = array.replace("null", "0")[1:-1] |
| 6365 | |
| 6366 | # find stuff before first "/" |
| 6367 | idx = array.find("/") |
| 6368 | if idx < 1: # this has no target page spec |
| 6369 | templ_dict["dest"] = array # return the orig. string |
| 6370 | return templ_dict |