From 6adba8744b5e9a902d32ab3ad58135d10736c45b Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 4 Mar 2025 14:39:44 +0000 Subject: [PATCH] First stab at link annotations --- cpdftoc.ml | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/cpdftoc.ml b/cpdftoc.ml index 5e3f784..2ccbea8 100644 --- a/cpdftoc.ml +++ b/cpdftoc.ml @@ -231,10 +231,30 @@ let typeset_table_of_contents ~font ~fontsize ~title ~bookmark ~dotleader ~proce let p_struct_elem_first_page = Pdf.addobj pdf (Pdf.Dictionary [("/Type", Pdf.Name "/StructElem"); ("/S", Pdf.Name "/P")]) in + let mcid = ref 1 in let link_struct_elems_for_each_page = - map - (Pdf.addobj pdf) - [Pdf.Dictionary [("/Type", Pdf.Name "/StructElem"); ("/S", Pdf.Name "/Link"); ("/K", Pdf.Array [Pdf.Integer 1; Pdf.Indirect 2])]] + flatten + (map + (fun page -> + let annot_objnums = + match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with + | Some (Pdf.Array a) -> map (function Pdf.Indirect i -> i | _ -> 0) a + | _ -> [] + in + let r = map + (fun annot_i -> + let r = + let objr = Pdf.addobj pdf (Pdf.Dictionary [("/Type", Pdf.Name "/ObjR"); ("/Obj", Pdf.Indirect annot_i)]) in + Pdf.addobj pdf + (Pdf.Dictionary [("/Type", Pdf.Name "/StructElem"); + ("/S", Pdf.Name "/Link"); + ("/K", Pdf.Array [Pdf.Integer !mcid; Pdf.Indirect objr])]) + in + incr mcid; r) + annot_objnums + in + mcid := 0; r) + toc_pages) in let prepending_structitems = map (fun x -> Pdf.Indirect x) (p_struct_elem_first_page::link_struct_elems_for_each_page)