Я пытаюсь добавить номер страницы в нижней части страницы, используя pypdf2. Я могу добавить соответствующие тексты и синтаксис, используя следующий код:
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
Над кодом успешно добавляется «/ Content», но на этапе записи происходит сбой, и отображается следующая ошибка:
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'
Я проверил добавленный контент, все выглядело нормально, я что-то упустил?