This commit is contained in:
John Whitington 2023-01-13 15:40:37 +08:00
parent 01ff49808c
commit 64c0ad71b6
1 changed files with 11 additions and 3 deletions

View File

@ -24,13 +24,19 @@ let list_page_annotations encoding pdf num page =
let rewrite_destination calculate_pagenumber d = let rewrite_destination calculate_pagenumber d =
match d with match d with
| Pdf.Array (Pdf.Indirect i::r) -> | Pdf.Array (Pdf.Indirect i::r) ->
Pdf.Array (Pdf.Indirect (calculate_pagenumber (Pdfdest.Fit (Pdfdest.PageObject i)))::r) Pdf.Array (Pdf.Integer (calculate_pagenumber (Pdfdest.Fit (Pdfdest.PageObject i)))::r)
| x -> x | x -> x
let rewrite_destinations pdf annot = let rewrite_destinations pdf annot =
let refnums = Pdf.page_reference_numbers pdf in let refnums = Pdf.page_reference_numbers pdf in
let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in let fastrefnums = hashtable_of_dictionary (combine refnums (indx refnums)) in
let calculate_pagenumber = Pdfpage.pagenumber_of_target ~fastrefnums pdf in let calculate_pagenumber = Pdfpage.pagenumber_of_target ~fastrefnums pdf in
(* Deal with /P in annotation *)
let annot =
match Pdf.indirect_number pdf "/P" annot with
| Some i -> Pdf.add_dict_entry annot "/P" (Pdf.Integer (calculate_pagenumber (Pdfdest.Fit (Pdfdest.PageObject i))))
| None -> annot
in
(* Deal with /Dest in annotation *) (* Deal with /Dest in annotation *)
match Pdf.lookup_direct pdf "/Dest" annot with match Pdf.lookup_direct pdf "/Dest" annot with
| Some d -> Pdf.add_dict_entry annot "/Dest" (rewrite_destination calculate_pagenumber d) | Some d -> Pdf.add_dict_entry annot "/Dest" (rewrite_destination calculate_pagenumber d)
@ -53,8 +59,9 @@ let annotations_json_page pdf page pagenum =
| Some (Pdf.Array annots) -> | Some (Pdf.Array annots) ->
map map
(fun annot -> (fun annot ->
extra := Pdf.objects_referenced [] [] pdf annot @ !extra; let annot = rewrite_destinations pdf annot in
`List [`Int pagenum; Cpdfjson.json_of_object ~clean_strings:true pdf (fun _ -> ()) false false annot]) extra := Pdf.objects_referenced [] [] pdf annot @ !extra;
`List [`Int pagenum; Cpdfjson.json_of_object ~clean_strings:true pdf (fun _ -> ()) false false annot])
(map (Pdf.direct pdf) annots) (map (Pdf.direct pdf) annots)
| _ -> [] | _ -> []
@ -69,6 +76,7 @@ let list_annotations_json range pdf =
let json = `List (flatten (map2 (annotations_json_page pdf) pages pagenums)) in let json = `List (flatten (map2 (annotations_json_page pdf) pages pagenums)) in
let extra = setify !extra in let extra = setify !extra in
Printf.printf "%i extra objects needed\n" (length extra); Printf.printf "%i extra objects needed\n" (length extra);
iter (fun i -> Printf.printf "Object %i = %s\n" i (Pdfwrite.string_of_pdf (Pdf.lookup_obj pdf i))) extra;
J.pretty_to_channel stdout json J.pretty_to_channel stdout json
let list_annotations ~json range encoding pdf = let list_annotations ~json range encoding pdf =