unidoc / unipdf

Golang PDF library for creating and processing PDF files (pure go)
https://unidoc.io
Other
2.5k stars 249 forks source link

[BUG]Search and replace text would lost table of contents #365

Closed xssworm closed 3 years ago

xssworm commented 4 years ago

Description

Can successfully replace text, but will lose the table of contents.

Expected Behavior

replace the text '取扱説明書' in page 1 use this example: https://github.com/unidoc/unipdf-examples/blob/development/text/pdf_search_replace_advanced.go

Actual Behavior

Steps to reproduce the behavior: $ go run pdf_search_replace_advanced.go quietcontrol-30_ja.pdf output.pdf "取扱説明書" "xxxxxx" Successfully created output.pdf The text was successfully replaced, but the table of contents was lost, and the text becomes invisible.

before:

before

after:

after

Attachments

quietcontrol-30_ja.pdf

003
gunnsth commented 3 years ago

You need to copy the Outlines (which store what you call "Table of contents" or sometimes "Bookmarks") from the original document over. Outlines are stored separately, need something like:

outlineTree := reader.GetOutlineTree()
writer.AddOutlineTree(outlineTree)