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
(
doc: 'Document',
toc: list,
collapse: int = 1,
)
| 7125 | doc._set_page_labels(create_nums(labels)) |
| 7126 | |
| 7127 | def set_toc( |
| 7128 | doc: 'Document', |
| 7129 | toc: list, |
| 7130 | collapse: int = 1, |
| 7131 | ) -> int: |
| 7132 | """Create new outline tree (table of contents, TOC). |
| 7133 | |
| 7134 | Args: |
| 7135 | toc: (list, tuple) each entry must contain level, title, page and |
| 7136 | optionally top margin on the page. None or '()' remove the TOC. |
| 7137 | collapse: (int) collapses entries beyond this level. Zero or None |
| 7138 | shows all entries unfolded. |
| 7139 | Returns: |
| 7140 | the number of inserted items, or the number of removed items respectively. |
| 7141 | """ |
| 7142 | if doc.is_closed or doc.is_encrypted: |
| 7143 | raise ValueError("document closed or encrypted") |
| 7144 | if not doc.is_pdf: |
| 7145 | raise ValueError("is no PDF") |
| 7146 | if not toc: # remove all entries |
| 7147 | return len(doc._delToC()) |
| 7148 | |
| 7149 | # validity checks -------------------------------------------------------- |
| 7150 | if type(toc) not in (list, tuple): |
| 7151 | raise ValueError("'toc' must be list or tuple") |
| 7152 | toclen = len(toc) |
| 7153 | page_count = doc.page_count |
| 7154 | t0 = toc[0] |
| 7155 | if type(t0) not in (list, tuple): |
| 7156 | raise ValueError("items must be sequences of 3 or 4 items") |
| 7157 | if t0[0] != 1: |
| 7158 | raise ValueError("hierarchy level of item 0 must be 1") |
| 7159 | for i in list(range(toclen - 1)): |
| 7160 | t1 = toc[i] |
| 7161 | t2 = toc[i + 1] |
| 7162 | if not -1 <= t1[2] <= page_count: |
| 7163 | raise ValueError(f"row {i}: page number out of range") |
| 7164 | if (type(t2) not in (list, tuple)) or len(t2) not in (3, 4): |
| 7165 | raise ValueError(f"bad row {(i + 1)}") |
| 7166 | if (type(t2[0]) is not int) or t2[0] < 1: |
| 7167 | raise ValueError(f"bad hierarchy level in row {(i + 1)}") |
| 7168 | if t2[0] > t1[0] + 1: |
| 7169 | raise ValueError(f"bad hierarchy level in row {(i + 1)}") |
| 7170 | # no formal errors in toc -------------------------------------------------- |
| 7171 | |
| 7172 | # -------------------------------------------------------------------------- |
| 7173 | # make a list of xref numbers, which we can use for our TOC entries |
| 7174 | # -------------------------------------------------------------------------- |
| 7175 | old_xrefs = doc._delToC() # del old outlines, get their xref numbers |
| 7176 | |
| 7177 | # prepare table of xrefs for new bookmarks |
| 7178 | old_xrefs = [] |
| 7179 | xref = [0] + old_xrefs |
| 7180 | xref[0] = doc._getOLRootNumber() # entry zero is outline root xref number |
| 7181 | if toclen > len(old_xrefs): # too few old xrefs? |
| 7182 | for i in range((toclen - len(old_xrefs))): |
| 7183 | xref.append(doc.get_new_xref()) # acquire new ones |
| 7184 |