How to get bookmark of PDF and add bookmark to new pdf?

耗尽温柔 提交于 2019-12-25 14:25:17

问题


I am merging one PDf to Other to other PDF, it is working fine, but Bookmark is missing in final PDF.

Following is PDF generation code:

#- 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()

Following is merge code:

#- 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()

but bookmark is missing and also title is missing in metadata.

what are 'bookmarkHorizontal', 'bookmarkHorizontalAbsolute', 'bookmarkPage' method of canvas object?

same question here also How to Add bookmarks to PDF file?


回答1:


I can get Title from following code:

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

set Title to New PDF by setTitle method

#- 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")

Or:

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

BookMark

  1. First get bookmarks from input pdf these code How to get bookmark's page number

  2. following code Add Bookmark to new PDF:

`

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()

`

Now How to handle Nested Bookmarks :)??



来源:https://stackoverflow.com/questions/35197544/how-to-get-bookmark-of-pdf-and-add-bookmark-to-new-pdf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!