Create new outline tree (table of contents, TOC). Args: toc: (list, tuple) each entry must contain level, title, page and optionally top margin on the page. None or '()' remove the TOC. collapse: (int) collapses entries beyond this level. Zero or None sho
(
doc: Document,
toc: list,
collapse: int = 1,
)
| 1314 | |
| 1315 | |
| 1316 | def set_toc( |
| 1317 | doc: Document, |
| 1318 | toc: list, |
| 1319 | collapse: int = 1, |
| 1320 | ) -> int: |
| 1321 | """Create new outline tree (table of contents, TOC). |
| 1322 | |
| 1323 | Args: |
| 1324 | toc: (list, tuple) each entry must contain level, title, page and |
| 1325 | optionally top margin on the page. None or '()' remove the TOC. |
| 1326 | collapse: (int) collapses entries beyond this level. Zero or None |
| 1327 | shows all entries unfolded. |
| 1328 | Returns: |
| 1329 | the number of inserted items, or the number of removed items respectively. |
| 1330 | """ |
| 1331 | if doc.is_closed or doc.is_encrypted: |
| 1332 | raise ValueError("document closed or encrypted") |
| 1333 | if not doc.is_pdf: |
| 1334 | raise ValueError("is no PDF") |
| 1335 | if not toc: # remove all entries |
| 1336 | return len(doc._delToC()) |
| 1337 | |
| 1338 | # validity checks -------------------------------------------------------- |
| 1339 | if type(toc) not in (list, tuple): |
| 1340 | raise ValueError("'toc' must be list or tuple") |
| 1341 | toclen = len(toc) |
| 1342 | page_count = doc.page_count |
| 1343 | t0 = toc[0] |
| 1344 | if type(t0) not in (list, tuple): |
| 1345 | raise ValueError("items must be sequences of 3 or 4 items") |
| 1346 | if t0[0] != 1: |
| 1347 | raise ValueError("hierarchy level of item 0 must be 1") |
| 1348 | for i in list(range(toclen - 1)): |
| 1349 | t1 = toc[i] |
| 1350 | t2 = toc[i + 1] |
| 1351 | if not -1 <= t1[2] <= page_count: |
| 1352 | raise ValueError("row %i: page number out of range" % i) |
| 1353 | if (type(t2) not in (list, tuple)) or len(t2) not in (3, 4): |
| 1354 | raise ValueError("bad row %i" % (i + 1)) |
| 1355 | if (type(t2[0]) is not int) or t2[0] < 1: |
| 1356 | raise ValueError("bad hierarchy level in row %i" % (i + 1)) |
| 1357 | if t2[0] > t1[0] + 1: |
| 1358 | raise ValueError("bad hierarchy level in row %i" % (i + 1)) |
| 1359 | # no formal errors in toc -------------------------------------------------- |
| 1360 | |
| 1361 | # -------------------------------------------------------------------------- |
| 1362 | # make a list of xref numbers, which we can use for our TOC entries |
| 1363 | # -------------------------------------------------------------------------- |
| 1364 | old_xrefs = doc._delToC() # del old outlines, get their xref numbers |
| 1365 | |
| 1366 | # prepare table of xrefs for new bookmarks |
| 1367 | old_xrefs = [] |
| 1368 | xref = [0] + old_xrefs |
| 1369 | xref[0] = doc._getOLRootNumber() # entry zero is outline root xref number |
| 1370 | if toclen > len(old_xrefs): # too few old xrefs? |
| 1371 | for i in range((toclen - len(old_xrefs))): |
| 1372 | xref.append(doc.get_new_xref()) # acquire new ones |
| 1373 |
nothing calls this directly
no test coverage detected
searching dependent graphs…