def setToC(doc, toc):
if doc.isClosed or doc.isEncrypted:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if not doc.isPDF:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>toclen = len(toc)<EOL>if type(toc) is not list:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if toclen == <NUM_LIT:0>:<EOL><INDENT>return len(doc._delToC())<EOL><DEDENT>pageCount = len(doc)<EOL>t0 = toc[<NUM_LIT:0>]<EOL>if type(t0) is not list:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if t0[<NUM_LIT:0>] != <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>for i in list(range(toclen-<NUM_LIT:1>)):<EOL><INDENT>t1 = toc[i]<EOL>t2 = toc[i+<NUM_LIT:1>]<EOL>if not -<NUM_LIT:1> <= t1[<NUM_LIT:2>] <= pageCount:<EOL><INDENT>raise ValueError("<STR_LIT>" % i)<EOL><DEDENT>if (type(t2) is not list) or len(t2) < <NUM_LIT:3> or len(t2) > <NUM_LIT:4>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if (type(t2[<NUM_LIT:0>]) is not int) or t2[<NUM_LIT:0>] < <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>")<EOL><DEDENT>if t2[<NUM_LIT:0>] > t1[<NUM_LIT:0>] + <NUM_LIT:1>:<EOL><INDENT>raise ValueError("<STR_LIT>" % i)<EOL><DEDENT><DEDENT>old_xrefs = doc._delToC()          <EOL>old_xrefs = []                     <EOL>xref = [<NUM_LIT:0>] + old_xrefs<EOL>xref[<NUM_LIT:0>] = doc._getOLRootNumber()        <EOL>if toclen > len(old_xrefs):             <EOL><INDENT>for i in range((toclen - len(old_xrefs))):<EOL><INDENT>xref.append(doc._getNewXref())  <EOL><DEDENT><DEDENT>lvltab = {<NUM_LIT:0>:<NUM_LIT:0>}                     <EOL>===========================================================================<EOL>ntains new outline objects as strings - first one is outline root<EOL>===========================================================================<EOL>olitems = [{"<STR_LIT:count>":<NUM_LIT:0>, "<STR_LIT>":-<NUM_LIT:1>, "<STR_LIT>":-<NUM_LIT:1>, "<STR_LIT>":xref[<NUM_LIT:0>]}]<EOL>===========================================================================<EOL>ild olitems as a list of PDF-like connnected dictionaries<EOL>===========================================================================<EOL>for i in range(toclen):<EOL><INDENT>o = toc[i]<EOL>lvl = o[<NUM_LIT:0>] <EOL>title = getPDFstr(o[<NUM_LIT:1>]) <EOL>pno = min(doc.pageCount - <NUM_LIT:1>, max(<NUM_LIT:0>, o[<NUM_LIT:2>] - <NUM_LIT:1>)) <EOL>top = <NUM_LIT:0><EOL>if len(o) < <NUM_LIT:4>:<EOL><INDENT>p = doc.loadPage(pno)<EOL>top = int(round(p.bound().y1) - <NUM_LIT>)  <EOL>p = None                             <EOL><DEDENT>top1 = top + <NUM_LIT:0>                        <EOL>dest_dict = {}<EOL>if len(o) > <NUM_LIT:3>:<EOL><INDENT>if type(o[<NUM_LIT:3>]) is int or type(o[<NUM_LIT:3>]) is float:<EOL><INDENT>top1 = int(round(o[<NUM_LIT:3>]))<EOL>dest_dict = o[<NUM_LIT:3>]<EOL><DEDENT>else:<EOL><INDENT>dest_dict = o[<NUM_LIT:3>] if type(o[<NUM_LIT:3>]) is dict else {}<EOL>try:<EOL><INDENT>top1 = int(round(o[<NUM_LIT:3>]["<STR_LIT:to>"].y)) <EOL><DEDENT>except: pass<EOL><DEDENT><DEDENT>else:<EOL><INDENT>dest_dict = top<EOL><DEDENT>if  <NUM_LIT:0> <= top1 <= top + <NUM_LIT>:<EOL><INDENT>top = top1<EOL><DEDENT>d = {}<EOL>d["<STR_LIT>"] = -<NUM_LIT:1><EOL>d["<STR_LIT:count>"] = <NUM_LIT:0><EOL>d["<STR_LIT>"]  = -<NUM_LIT:1><EOL>d["<STR_LIT>"]  = -<NUM_LIT:1><EOL>d["<STR_LIT>"]  = -<NUM_LIT:1><EOL>d["<STR_LIT>"]  = getDestStr(doc._getPageObjNumber(pno), dest_dict)<EOL>d["<STR_LIT>"]   = top<EOL>d["<STR_LIT:title>"] = title<EOL>d["<STR_LIT>"] = lvltab[lvl-<NUM_LIT:1>]<EOL>d["<STR_LIT>"] = xref[i+<NUM_LIT:1>]<EOL>lvltab[lvl] = i+<NUM_LIT:1><EOL>parent = olitems[lvltab[lvl-<NUM_LIT:1>]]<EOL>parent["<STR_LIT:count>"] += <NUM_LIT:1><EOL>if parent["<STR_LIT>"] == -<NUM_LIT:1>:<EOL><INDENT>parent["<STR_LIT>"] = i+<NUM_LIT:1><EOL>parent["<STR_LIT>"] = i+<NUM_LIT:1><EOL><DEDENT>else:<EOL><INDENT>d["<STR_LIT>"] = parent["<STR_LIT>"]<EOL>prev = olitems[parent["<STR_LIT>"]]<EOL>prev["<STR_LIT>"]   = i+<NUM_LIT:1><EOL>parent["<STR_LIT>"] = i+<NUM_LIT:1><EOL><DEDENT>olitems.append(d)<EOL><DEDENT>===========================================================================<EOL>w create each ol item as a string and insert it in the PDF<EOL>===========================================================================<EOL>for i, ol in enumerate(olitems):<EOL><INDENT>txt = "<STR_LIT>"<EOL>if ol["<STR_LIT:count>"] > <NUM_LIT:0>:<EOL><INDENT>if i > <NUM_LIT:0>:<EOL><INDENT>txt += "<STR_LIT>" % ol["<STR_LIT:count>"]<EOL><DEDENT>else:<EOL><INDENT>txt += "<STR_LIT>" % ol["<STR_LIT:count>"]<EOL><DEDENT><DEDENT>try:<EOL><INDENT>txt += ol["<STR_LIT>"]<EOL><DEDENT>except: pass<EOL>try:<EOL><INDENT>if ol["<STR_LIT>"] > -<NUM_LIT:1>:<EOL><INDENT>txt += "<STR_LIT>" % xref[ol["<STR_LIT>"]]<EOL><DEDENT><DEDENT>except: pass<EOL>try:<EOL><INDENT>if ol["<STR_LIT>"] > -<NUM_LIT:1>:<EOL><INDENT>txt += "<STR_LIT>" % xref[ol["<STR_LIT>"]]<EOL><DEDENT><DEDENT>except: pass<EOL>try:<EOL><INDENT>if ol["<STR_LIT>"] > -<NUM_LIT:1>:<EOL><INDENT>txt += "<STR_LIT>" % xref[ol["<STR_LIT>"]]<EOL><DEDENT><DEDENT>except: pass<EOL>try:<EOL><INDENT>if ol["<STR_LIT>"] > -<NUM_LIT:1>:<EOL><INDENT>txt += "<STR_LIT>" % xref[ol["<STR_LIT>"]]<EOL><DEDENT><DEDENT>except: pass<EOL>try:<EOL><INDENT>if ol["<STR_LIT>"] > -<NUM_LIT:1>:<EOL><INDENT>txt += "<STR_LIT>" % xref[ol["<STR_LIT>"]]<EOL><DEDENT><DEDENT>except: pass<EOL>try:<EOL><INDENT>txt += "<STR_LIT>" + ol["<STR_LIT:title>"]<EOL><DEDENT>except: pass<EOL>if i == <NUM_LIT:0>:           <EOL><INDENT>txt += "<STR_LIT>"<EOL><DEDENT>txt += "<STR_LIT>"<EOL>doc._updateObject(xref[i], txt)     <EOL><DEDENT>doc.initData()<EOL>return toclen<EOL>
Create new outline tree (table of contents)\ntoc: a Python list of lists. Each entry must contain level, title, page and optionally top margin on the page.