2

I am trying to create bookmark in pdf based on keyword present in pdf file. I can create the bookmark for pages in pdf using pypdf2's addbookmark function. but it is page based, not keyword based. For example, if pdf contains 'metadata' as a word, I would like to create bookmark for 'metadata' keyword.

I have referred Add a bookmark to a PDF with PyPDF2 which creates bookmark for pages.

output = PdfFileWriter() # open output
input = PdfFileReader(open('test.pdf', 'rb')) # open input
output.addPage(input.getPage(0)) # insert page
output.addBookmark('Hello, World Bookmark', 0, parent=None) # add bookmark
outputStream = file('result.pdf','wb') #creating result pdf JCT
output.write(outputStream) #writing to result pdf JCT
outputStream.close() #closing result JCT

Nitesh Selkari
  • 67
  • 1
  • 2
  • 8

1 Answers1

1

As I think that there is no specific type of bookmark that is what you call a keyword bookmark, perhaps you mean to add a bookmark to every page where the keyword appears. For that you will need to loop over the pages, and if the page contains the keyword, add a bookmark to that page.

Dan D.
  • 73,243
  • 15
  • 104
  • 123
  • Yes, by this approach I can achieve the end result. but was looking for any library or in build function by which this can be achieved :) – Nitesh Selkari Mar 19 '19 at 04:59