Esempio n. 1
0
def obj_pagelabels(style=None, start=None):
    '''
        object for page label

        Paramters:
            style: string
                specify style of page labels, like Roman or Arabic

            start: optional, None, int
                start page
                if None, use 1
    '''
    # start page
    if start is None:
        start = 1

    # style
    global _map_style
    if style in _map_style:
        style = _map_style[style]
    elif style is None:
        style = _map_style['arabic']

    obj = PDF.DictionaryObject()
    obj.update({PDF.NameObject("/S"): PDF.NameObject(style)})
    obj.update({PDF.NameObject("/St"): PDF.NumberObject(start)})
    return obj
Esempio n. 2
0
def make_dest(pdfw, pg):
    d = PDF.ArrayObject()
    d.append(pdfw.getPage(pg).indirectRef)
    d.append(PDF.NameObject("/XYZ"))
    d.append(PDF.NullObject())
    d.append(PDF.NullObject())
    d.append(PDF.NullObject())
    return d
Esempio n. 3
0
def add_outlines(toc, filename, output):
    build_outlines_btree(toc)
    pdf_out = PdfFileWriter()
    inputFile = open(filename, 'rb')
    pdf_in = PdfFileReader(inputFile)
    for p in pdf_in.pages:
        pdf_out.addPage(p)
    toc_num = len(toc)
    if (toc_num == 0): # Just copy if toc empty
        outputFile = open(output, "wb")
        pdf_out.write(outputFile)
        inputFile.close()
        outputFile.close()
        return
    idoix = len(pdf_out._objects) + 1
    idorefs = [PDF.IndirectObject(x + idoix, 0, pdf_out)
               for x in range(toc_num + 1)]
    ol = PDF.DictionaryObject()
    ol.update({
        PDF.NameObject("/Type"): PDF.NameObject("/Outlines"),
        PDF.NameObject("/First"): idorefs[1],
        PDF.NameObject("/Last"): idorefs[-1],
        PDF.NameObject("/Count"): PDF.NumberObject(toc_num)
    })
    olitems = []
    for t in toc:
        oli = PDF.DictionaryObject()
        oli.update({
            PDF.NameObject("/Title"): PDF.TextStringObject(t["title"].decode("utf-8")),
            PDF.NameObject("/Dest"): make_dest(pdf_out, t["page"])
        })
        opt_keys = {"real_parent": "/Parent", "prev": "/Prev",
                    "next": "/Next", "first": "/First", "last": "/Last"}
        for k, v in opt_keys.items():
            n = getattr(t["node"], k)()
            if n is not None:
                oli.update({
                    PDF.NameObject(v): idorefs[n.index]
                })
        olitems.append(oli)
    pdf_out._addObject(ol)
    for i in olitems:
        pdf_out._addObject(i)
    pdf_out._root_object.update({
        PDF.NameObject("/Outlines"): idorefs[0]
    })
    outputFile = open(output, "wb")
    pdf_out.write(outputFile)
    inputFile.close()
    outputFile.close()
Esempio n. 4
0
def locate_pagelabels_in_writer(writer, add_ifnot=False):
    '''
        locate the object storing page label in writer

        Parameters:
            add_ifnot: bool
                if True, add an empty page labels structure when not exists
    '''
    root_obj = get_root_of_rw(writer)
    if '/PageLabels' not in root_obj:
        if not add_ifnot:
            return None

        nums_array = PDF.ArrayObject()

        nums_dict = PDF.DictionaryObject()
        nums_dict.update({PDF.NameObject('/Nums'): nums_array})

        labels_dict = PDF.DictionaryObject()
        labels_dict.update({PDF.NameObject('/PageLabels'): nums_dict})

        root_obj.update(labels_dict)

    return root_obj['/PageLabels']