Remove /ParentTreeNextKey in -table-of-contents

This commit is contained in:
John Whitington 2025-03-07 15:47:33 +00:00
parent 41bd26ea31
commit 0a1be154ad
2 changed files with 18 additions and 0 deletions

View File

@ -707,6 +707,8 @@ let make_structure_tree pdf items =
in
make_structure_tree pageobjnums (ref 0, ref standard_namespace, null_hash ()) pdf items
(* TODO When we allow drawing on PDFs preserving the structure tree, we must remove /ParentTreeNextKey. *)
(* Write such a structure tree to a PDF. *)
let write_structure_tree pdf st =
let parentmap = ref [] in

View File

@ -111,6 +111,21 @@ let prepend_structitems pdf items =
Pdf.replace_chain pdf ["/Root"; "/StructTreeRoot"; "/K"] (Pdf.Array (items @ [Pdf.Dictionary d]))
| _ -> ()
(* FIXME Would be better with a Pdf.remove_chain *)
let remove_parent_tree_next_key pdf =
match Pdf.lookup_obj pdf pdf.Pdf.root with
| Pdf.Dictionary d ->
begin match lookup "/StructTreeRoot" d with
| Some (Pdf.Indirect i) ->
Pdf.addobj_given_num pdf (i, Pdf.remove_dict_entry (Pdf.lookup_obj pdf i) "/ParentTreeNextKey")
| Some (Pdf.Dictionary d2) ->
let newstroot = Pdf.remove_dict_entry (Pdf.Dictionary d2) "/ParentTreeNextKey" in
let newroot = Pdf.add_dict_entry (Pdf.Dictionary d) "/StructTreeRoot" newstroot in
Pdf.addobj_given_num pdf (pdf.Pdf.root, newroot)
| _ -> ()
end
| _ -> ()
(* FIXME Again relies upon there being an existing structure tree. Fix. *)
(* FIXME Again, replace_chain would be much better here if it could deal with a final indirect. *)
let add_to_parent_tree pdf p =
@ -309,6 +324,7 @@ let typeset_table_of_contents ~font ~fontsize ~title ~bookmark ~dotleader ~proce
Pdf.addobj_given_num pdf (o, Pdf.add_dict_entry page "/StructParents" (Pdf.Integer ptn)))
toc_pageobjnums
toc_structure_items_per_page;
remove_parent_tree_next_key pdf;
begin match Pdf.lookup_chain pdf pdf.Pdf.trailerdict ["/Root"; "/StructTreeRoot"; "/K"] with
| Some (Pdf.Array a) ->
Pdf.replace_chain pdf ["/Root"; "/StructTreeRoot"; "/K"] (Pdf.Array (prepending_structitems @ a))