-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PdfFileMerger.addBookmark() should return the newly added bookmark #40
Comments
Hello, |
Hi, Sample code that concatenates a number of one-page PDFs and creates a two-level bookmark structure: from PyPDF2 import PdfFileMerger
merged = PdfFileMerger()
s = open("1.pdf", "rb")
merged.append(s, pages = (0,1))
parent = merged.addBookmark("One", 0, None)
s.close()
s = open("2.pdf", "rb")
merged.append(s, pages = (0,1))
merged.addBookmark("One / A", 1, parent)
s.close()
s = open("3.pdf", "rb")
merged.append(s, pages = (0,1))
merged.addBookmark("One / B", 2, parent)
s.close()
s = open("4.pdf", "rb")
merged.append(s, pages = (0,1))
parent = merged.addBookmark("Two", 3, None)
s.close()
s = open("5.pdf", "rb")
merged.append(s, pages = (0,1))
merged.addBookmark("Two / A", 4, parent)
s.close()
d = open("out_pdfmerge.pdf", "wb")
merged.write(d)
d.close() Resulting traceback triggered by
For comparison, the following equivalent code using from PyPDF2 import PdfFileWriter, PdfFileReader
output = PdfFileWriter()
s1 = PdfFileReader(open("1.pdf", "rb"))
output.addPage(s1.getPage(0))
parent = output.addBookmark("One", 0, None)
s2 = PdfFileReader(open("2.pdf", "rb"))
output.addPage(s2.getPage(0))
output.addBookmark("One / A", 1, parent)
s3 = PdfFileReader(open("3.pdf", "rb"))
output.addPage(s3.getPage(0))
output.addBookmark("One / B", 2, parent)
s4 = PdfFileReader(open("4.pdf", "rb"))
output.addPage(s4.getPage(0))
parent = output.addBookmark("Two", 3, None)
s5 = PdfFileReader(open("5.pdf", "rb"))
output.addPage(s5.getPage(0))
output.addBookmark("Two / A", 4, parent)
d = open("out_pdfwrite.pdf", "wb")
output.write(d)
d.close() The reason I'd rather use |
Handle nested bookmarks properly (fixes later issue in #40)
…339) People stumbled over this inconsistency: * #40 * https://stackoverflow.com/a/42991101/562769 This was also tested with: https://stackoverflow.com/questions/42941742/pypdf2-nested-bookmarks-with-same-name-not-working/42991101#comment73249244_42991101
…y-pdf#339) People stumbled over this inconsistency: * py-pdf#40 * https://stackoverflow.com/a/42991101/562769 This was also tested with: https://stackoverflow.com/questions/42941742/pypdf2-nested-bookmarks-with-same-name-not-working/42991101#comment73249244_42991101
PdfFileWriter.addBookmark() returns the newly added bookmark, so it can be used as the parent in subsequent addBookmark() calls in order to create nested bookmarks.
For consistency, PdfFileMerger.addBookmark() should function similarly, however it does not, as it doesn't return anything, thus making it impossible to create nested bookmarks with PdfFileMerger.
The text was updated successfully, but these errors were encountered: