Skip to content

How can I merge two PDF, including their outlines? #1184

Answered by mtd91429
sant527 asked this question in Q&A
Discussion options

You must be logged in to vote

In my humble opinion, this post more appropriately belongs on a forum such as StackOverflow rather than an issue on GitHub.

Nonetheless, the functionality the poster is seeking is best accomplished using the PdfMerger class. Using the updated nomenclature (#1156)

from PyPDF2 import PdfReader, PdfMerger

doc1 = PdfReader("'first.pdf')
doc2 = PdfReader("second.pdf")

merger = PdfMerger()
merger.append(doc1, import_outline=True)
merger.append(doc2, import_outline=True)

merger.write("first_then_second.pdf")

Verify the output by opening the new document in your favorite commercial PDF rendering software. Or, you can inspect the results directly via PyPDF2:

print(merger.outline)

or

combined_doc =

Replies: 5 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@MartinThoma
Comment options

@fhg-isi
Comment options

@stefan6419846
Comment options

@fhg-isi
Comment options

Answer selected by MartinThoma
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
PdfMerger The PdfMerger component is affected is-feature A feature request
6 participants
Converted from issue

This discussion was converted from issue #577 on July 30, 2022 06:55.