Skip to content
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

Preserve table of contents when editing PDF #226

Open
NiklasKappel opened this issue Feb 7, 2022 · 1 comment
Open

Preserve table of contents when editing PDF #226

NiklasKappel opened this issue Feb 7, 2022 · 1 comment

Comments

@NiklasKappel
Copy link

I use the following simple combination of PdfReader and PdfWriter to edit PDF files.

import pdfrw


def main():
    pdf = pdfrw.PdfReader(open("input.pdf", "rb"))
    writer = pdfrw.PdfWriter()
    for pageNumber in range(pdf.numPages):
        page = pdf.pages[pageNumber]
        # Work on page.
        writer.addpage(page)
    writer.write("output.pdf")


if __name__ == "__main__":
    main()

Where # Work on page. is a placeholder for code that sometimes does nothing to the page and sometimes merges the page with a different pre-prepared page.

If input.pdf contains a table of contents (in the form of metadata that can be displayed in the sidebar of pdf viewers and used to navigate the document), the table of contents is missing in output.pdf.

Is it possible to preserve the table of contents in this example using pdfrw? E.g. is it possible to extract the table of contents from input.pdf and paste it into output.pdf?

@sl2c
Copy link

sl2c commented Sep 20, 2023

Hi!

The way you do it you are creating a blank PDF document and adding pages to it. However, if you want to edit an existing document a much simpler way would be to omit the call to addPage altogether, edit the pages in pdf in-place, just as your code does, and in the end simply write the result out using:

PdfWriter('output.pdf', trailer=pdf).write()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants