Beginning -add-bookmarks-json

This commit is contained in:
John Whitington 2021-10-27 11:44:30 +01:00
parent 5708d25255
commit e3abffb21c
3 changed files with 20 additions and 5 deletions

21
cpdf.ml
View File

@ -631,6 +631,20 @@ let bookmark_of_data pdf i s i' isopen optionaldest =
Pdfmarks.target = target;
Pdfmarks.isopen = isopen}
let parse_bookmark_file_json verify pdf input =
try
let marks =
[]
in
if verify then
if verify_bookmarks pdf 0 (Pdfpage.endpage pdf) marks then marks else
error "Bad bookmark file (References non-existant pages or is malformed)"
else
marks
with
e ->
error (Printf.sprintf "Malformed JSON bookmark file (%s)" (Printexc.to_string e))
let parse_bookmark_file verify pdf input =
let currline = ref 0 in
try
@ -669,8 +683,9 @@ let parse_bookmark_file verify pdf input =
(Printexc.to_string e))
let add_bookmarks verify input pdf =
let parsed = parse_bookmark_file verify pdf input in
let add_bookmarks ~json verify input pdf =
let parsed =
(if json then parse_bookmark_file_json else parse_bookmark_file) verify pdf input in
(*iter (fun b -> flprint (Pdfmarks.string_of_bookmark b); flprint "\n") parsed;*)
Pdfmarks.add_bookmarks parsed pdf
@ -855,7 +870,7 @@ let output_json_marks ch calculate_page_number pdf fastrefnums marks =
let json = `List (map json_of_mark marks) in
J.pretty_to_channel ch json
(* List the bookmarks, optionally deunicoding the text, in the given range to the given output *)
(* List the bookmarks in the given range to the given output *)
let list_bookmarks ~json encoding range pdf output =
let process_stripped escaped =
let b = Buffer.create 200 in

View File

@ -97,7 +97,7 @@ val parse_bookmark_file : bool -> Pdf.t -> Pdfio.input -> Pdfmarks.t list
(** [add_bookmarks verify input pdf] adds bookmarks from the bookmark file
give. If [verify] is given, bookmarks will be verified to ensure, for example,
that they are not out of the page range. *)
val add_bookmarks : bool -> Pdfio.input -> Pdf.t -> Pdf.t
val add_bookmarks : json:bool -> bool -> Pdfio.input -> Pdf.t -> Pdf.t
(** [list_bookmarks encoding range pdf output] lists the bookmarks to the given
output in the format specified in cpdfmanual.pdf *)

View File

@ -3967,7 +3967,7 @@ let go () =
args.relative_to_cropbox args.underneath range pdf)
| Some (AddBookmarks file) ->
write_pdf false
(Cpdf.add_bookmarks true (Pdfio.input_of_channel (open_in_bin file))
(Cpdf.add_bookmarks ~json:args.bookmarks_json true (Pdfio.input_of_channel (open_in_bin file))
(get_single_pdf args.op false))
| Some RemoveBookmarks ->
write_pdf false (Pdfmarks.remove_bookmarks (get_single_pdf args.op false))