[Python] Add text in the content pages of the pdf using PyPDF2

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to add page number in the lower side of the page using pypdf2. I am able to add the relevant texts and syntax using the below code:

    def addText(page, text, position, ignoreByteStringObject=False, debug=False):
    """
    Add user given text to a page content
    :page is the page on which text has to be added
    :text is the text string
    :position is the place where it has to be added, examples "top-right", "top-left", "bottom-right", "bottom-left", "bottom-center", "top-center"
    :param bool ignoreByteStringObject: optional parameter
    to ignore ByteString Objects.
    """
    pageRef = page
    content = pageRef['/Contents'].getObject()
    print content
    # if not isinstance(content, ContentStream()):
    content = ContentStream(content, pageRef)#creating contentstream class instance
    if debug:
    print content.operations

    # append this line ([], 'BT'), ([1, 0, 0, 1, 52, 34.5], 'Tm'), (['/F1', 9], 'Tf'), ([0, 0, 0], 'rg'), ([u'Study: Classified #1'], 'Tj'), ([0], 'g'), ([], 'ET'), ([], 'BT'),
    # ([1, 0, 0, 1, 336.1, 34.5], 'Tm'), (['/F1', 9], 'Tf'), ([0, 0, 0], 'rg'), ([u'This document is Classified.'], 'Tj'),
    # ([0], 'g'), ([], 'ET'), ([], 'BT'), ([1, 0, 0, 1, 633.34, 34.5], 'Tm'), (['/F1', 9], 'Tf'), ([0, 0, 0], 'rg'), ([TextStringObject(text)], 'Tj'), ([0], 'g'), ([], 'ET')
    if position == "bottom-center":
    if not ignoreByteStringObject:
    operands = ArrayObject([])
    operator = NameObject("BT")
    content.operations.append((operands, operator))
    operands = ArrayObject([1, 0, 0, 1, 52, 34.5])
    operator = NameObject("Tm")
    content.operations.append((operands, operator))
    operands = ArrayObject(['/F1', 9])
    operator = NameObject("Tf")
    content.operations.append((operands, operator))
    operands = ArrayObject([0, 0, 0])
    operator = NameObject("rg")
    content.operations.append((operands, operator))
    operands = ArrayObject([TextStringObject('Study: Classified #1')])
    operator = NameObject("Tj")
    content.operations.append((operands, operator))
    operands = ArrayObject([0])
    operator = NameObject("g")
    content.operations.append((operands, operator))
    operands = ArrayObject([])
    operator = NameObject("ET")
    content.operations.append((operands, operator))

    operands = ArrayObject([])
    operator = NameObject("BT")
    content.operations.append((operands, operator))
    operands = ArrayObject([1, 0, 0, 1, 336.1, 34.5])
    operator = NameObject("Tm")
    content.operations.append((operands, operator))
    operands = ArrayObject(['/F1', 9])
    operator = NameObject("Tf")
    content.operations.append((operands, operator))
    operands = ArrayObject([0, 0, 0])
    operator = NameObject("rg")
    content.operations.append((operands, operator))
    operands = ArrayObject([TextStringObject('This document is Classified.')])
    operator = NameObject("Tj")
    content.operations.append((operands, operator))
    operands = ArrayObject([0])
    operator = NameObject("g")
    content.operations.append((operands, operator))
    operands = ArrayObject([])
    operator = NameObject("ET")
    content.operations.append((operands, operator))

    operands = ArrayObject([])
    operator = NameObject("BT")
    content.operations.append((operands, operator))
    operands = ArrayObject([1, 0, 0, 1, 633.34, 34.5])
    operator = NameObject("Tm")
    content.operations.append((operands, operator))
    operands = ArrayObject(['/F1', 9])
    operator = NameObject("Tf")
    content.operations.append((operands, operator))
    operands = ArrayObject([0, 0, 0])
    operator = NameObject("rg")
    content.operations.append((operands, operator))
    operands = ArrayObject([TextStringObject(text)])
    operator = NameObject("Tj")
    content.operations.append((operands, operator))
    operands = ArrayObject([0])
    operator = NameObject("g")
    content.operations.append((operands, operator))
    operands = ArrayObject([])
    operator = NameObject("ET")
    content.operations.append((operands, operator))

    else:
    pass
    if debug:
    print content.operations

    pageRef.__setitem__(NameObject('/Contents'), content)
    return pageRef


    Above code successfully appends the '/Content', but during the writing step it fails and shows following error:

    Traceback (most recent call last):
    File "main.py", line 593, in <module>
    output.write(outputStream)
    File "C:\Users\jsolanki\Unknown\pdfeditor\src\PyPDF2\pdf.py", line 500, in write
    obj.writeToStream(stream, key)
    File "C:\Users\jsolanki\Unknown\pdfeditor\src\PyPDF2\generic.py", line 783, in writeToStream
    self[NameObject("/Length")] = NumberObject(len(self._data))
    File "C:\Users\jsolanki\Unknown\pdfeditor\src\PyPDF2\pdf.py", line 3028, in _getData
    op.writeToStream(newdata, None)
    AttributeError: 'int' object has no attribute 'writeToStream'


    I checked the appended content, it looked fine, am I missing something?

    Continue reading...

Compartilhe esta Página