我运行了您的代码(在其下方添加文本以写出 pdf)并发现实际上已创建了一个书签。
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
检查结果中的书签面板。拥有书签不会自动导致 PDF 打开到书签面板。
要使用 PyPDF2 打开书签面板,请添加一行:
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
output.setPageMode("/UseOutlines") #This is what tells the PDF to open to bookmarks
outputStream = file('result.pdf','wb') #creating result pdf JCT
output.write(outputStream) #writing to result pdf JCT
outputStream.close() #closing result JCT