如何获取PDF书签并将书签添加到新pdf?

时间:2016-02-04 09:46:17

标签: python pdf reportlab pypdf

我正在将一个PDf与其他PDF合并到其他PDF,它工作正常,但最终PDF中缺少书签。

以下是PDF生成代码:

#- Create One Page PDF with some text
from reportlab.pdfgen import canvas as canx
c = canx.Canvas('transparent.pdf')
c.setStrokeColor((1, 0, 0))
transparentwhite = canx.Color(255, 255, 255, alpha = 0.0)
c.setFillColor(transparentwhite)
t = c.beginText()
t.setTextRenderMode(2)
c._code.append(t.getCode())
c.setFont('Helvetica', 48)
c.saveState()
c.translate(100, 100)
c.rotate(45)
c.drawCentredString(500, 100, 'DRAFT')
c.save()

以下是合并代码:

#- Merge PDF.
transparentbannerfile = open('transparent.pdf', 'rb')
testpagefile = open('NOID_body20160202T133650.pdf', 'rb')
outputfile = open('mergedtransparent.pdf', 'wb')
import PyPDF2 as pdf
readerbanner = pdf.PdfFileReader(transparentbannerfile)
readertestpages = pdf.PdfFileReader(testpagefile)
bannerpage = readerbanner.getPage(0)
writeroutput = pdf.PdfFileWriter()
for x in xrange(readertestpages.getNumPages()):
  pagex = readertestpages.getPage(x)
  pagex.mergePage(bannerpage)
  writeroutput.addPage(pagex)

writeroutput.write(outputfile)
outputfile.close()
transparentbannerfile.close()
testpagefile.close()

但缺少书签,元数据中也缺少标题。

'bookmarkHorizontal', 'bookmarkHorizontalAbsolute', 'bookmarkPage'对象的canvas方法是什么?

同样的问题也在How to Add bookmarks to PDF file?

1 个答案:

答案 0 :(得分:-1)

我可以通过以下代码获得标题:

from pyPdf import PdfFileReader
pdf_toread = PdfFileReader(open('NOID_body20160202T133650.pdf', "rb"))
pdf_info = pdf_toread.getDocumentInfo()
print pdf_info

setTitle方法

将标题设置为新PDF
#- Create One Page PDF with some text
from reportlab.pdfgen import canvas as canx
c = canx.Canvas('transparent.pdf')
c.setTitle("Test to set Title")

或者:

import PyPDF2 as pdf
writeroutput = pdf.PdfFileWriter()
writeroutput.addMetadata({'/Title': u'Print Page Sizes'})

<强>书签

  1. 首先从输入pdf这些代码How to get bookmark's page number

  2. 获取书签
  3. 以下代码将书签添加到新PDF:

  4. `

    transparentbannerfile = open('transparent.pdf', 'rb')
    testpagefile = open('NOID_body20160202T133650.pdf', 'rb')
    outputfile = open('mergedtransparent112.pdf', 'wb')
    import PyPDF2 as pdf
    from collections import OrderedDict
    readerbanner = pdf.PdfFileReader(transparentbannerfile)
    readertestpages = pdf.PdfFileReader(testpagefile)
    bannerpage = readerbanner.getPage(0)
    writeroutput = pdf.PdfFileWriter()
    for x in xrange(readertestpages.getNumPages()):
        pagex = readertestpages.getPage(x)
        pagex.mergePage(bannerpage)
        writeroutput.addPage(pagex)
    
    a = OrderedDict([(u'SIDDHARTHA', {'top': 750, 'left': 0, 'page': 1, 'title': u'SIDDHARTHA'}), (u'Chapter 01', {'top': 750, 'left': 0, 'page': 3, 'title': u'Chapter 01'}), (u'Chapter 02', {'top': 503, 'left': 0, 'page': 6, 'title': u'Chapter 02'}), (u'Chapter 03', {'top': 340, 'left': 0, 'page': 11, 'title': u'Chapter 03'}), (u'Chapter 04', {'top': 231, 'left': 0, 'page': 17, 'title': u'Chapter 04'}), (u'Chapter 05', {'top': 909, 'left': 0, 'page': 30, 'title': u'Chapter 05'}), (u'Chapter 06', {'top': 614, 'left': 0, 'page': 32, 'title': u'Chapter 06'}), (u'Chapter 07', {'top': 417, 'left': 0, 'page': 35, 'title': u'Chapter 07'}), (u'Chapter 08', {'top': 289, 'left': 0, 'page': 41, 'title': u'Chapter 08'})])
    for i in a:
        writeroutput.addBookmark(i, a[i]["page"]-1, a[i])
    
    writeroutput.write(outputfile)
    outputfile.close()
    transparentbannerfile.close()
    testpagefile.close()
    

    `

    现在如何处理嵌套书签:)??