Support for JSON page info in cpdflib

This commit is contained in:
John Whitington 2024-02-08 14:33:11 +00:00
parent a4e3ca0bfa
commit 7274a83b2b
2 changed files with 15 additions and 3 deletions

View File

@ -154,7 +154,9 @@ let change_pattern_matrices_page pdf tr page =
{page with Pdfpage.resources = change_pattern_matrices_resources pdf tr page.Pdfpage.resources used} {page with Pdfpage.resources = change_pattern_matrices_resources pdf tr page.Pdfpage.resources used}
(* Output information for each page *) (* Output information for each page *)
let output_page_info ?(json=false) pdf range = exception Exceptjson of Cpdfyojson.Safe.t
let output_page_info ?(json=false) ?(raisejson=false) pdf range =
let pages = Pdfpage.pages_of_pagetree pdf in let pages = Pdfpage.pages_of_pagetree pdf in
let labels = Pdfpagelabels.read pdf in let labels = Pdfpagelabels.read pdf in
let getbox page box = let getbox page box =
@ -200,7 +202,11 @@ let output_page_info ?(json=false) pdf range =
("Annotations", `Int (num_annots page))] ("Annotations", `Int (num_annots page))]
in in
if json then if json then
flprint (Cpdfyojson.Safe.pretty_to_string (`List (map json_entry_of_pnum range))) let thejson = `List (map json_entry_of_pnum range) in
if raisejson then
raise (Exceptjson thejson)
else
flprint (Cpdfyojson.Safe.pretty_to_string thejson)
else else
iter iter
(fun pnum -> (fun pnum ->
@ -217,6 +223,10 @@ let output_page_info ?(json=false) pdf range =
Printf.printf "Annotations: %i\n" (num_annots page)) Printf.printf "Annotations: %i\n" (num_annots page))
range range
let json_page_info pdf range =
try output_page_info ~json:true ~raisejson:true pdf range; `List [] with
Exceptjson j -> j
let process_pages f pdf range = let process_pages f pdf range =
let pages = Pdfpage.pages_of_pagetree pdf in let pages = Pdfpage.pages_of_pagetree pdf in
let pages', pagenumbers, matrices = (* new page objects, page number, matrix *) let pages', pagenumbers, matrices = (* new page objects, page number, matrix *)

View File

@ -1,7 +1,9 @@
(** Working with pages *) (** Working with pages *)
(** Print page info (Mediabox etc) to standard output. *) (** Print page info (Mediabox etc) to standard output. *)
val output_page_info : ?json:bool -> Pdf.t -> int list -> unit val output_page_info : ?json:bool -> ?raisejson:bool -> Pdf.t -> int list -> unit
val json_page_info : Pdf.t -> int list -> Cpdfyojson.Safe.t
(** Given a function from page number and page to page, a document, and a list (** Given a function from page number and page to page, a document, and a list
of page numbers to apply it to, apply the function to all those pages. *) of page numbers to apply it to, apply the function to all those pages. *)