2

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?

Jayant
  • 181
  • 4
  • 9
  • Have you found a solution for this? – Joe Oct 01 '18 at 07:28
  • No, I am unable to append new elements, however if I append above string values to the last node of the '/Contents' dictionary, it is working. kinda crude hack. Are you facing similar issue during pdf writing? – Jayant Oct 06 '18 at 03:01
  • I might have been seeing a different issue, but I found a solution, see [here](https://stackoverflow.com/a/52587747/7919597). – Joe Oct 06 '18 at 06:12
  • I am indeed storing the pageRef into the output object using output.addPage(pageRef) method. however during the last step of saving the whole object as a output pdf, at object writing step it throws en error. Overall steps are like: 1 - fetch every page object from the input object stream. 2- apply changes to the content of certain page objects. 3 - add those modified page objects along with rest of page objects to an outputstream object. 4- write the objectstream as a new pdf file. – Jayant Oct 09 '18 at 22:03

0 Answers0