Implement listing of page info in JSON

This commit is contained in:
John Whitington 2023-10-31 16:51:57 +00:00
parent 442acf71cd
commit 122285db59
3 changed files with 38 additions and 16 deletions

View File

@ -3489,7 +3489,7 @@ let go () =
| (_, pagespec, _, _, _, _)::_, _ -> | (_, pagespec, _, _, _, _)::_, _ ->
let pdf = get_single_pdf args.op true in let pdf = get_single_pdf args.op true in
let range = parse_pagespec_allow_empty pdf pagespec in let range = parse_pagespec_allow_empty pdf pagespec in
Cpdfpage.output_page_info pdf range Cpdfpage.output_page_info ~json:args.format_json pdf range
| _ -> error "list-bookmarks: bad command line" | _ -> error "list-bookmarks: bad command line"
end end
| Some Metadata -> | Some Metadata ->

View File

@ -154,7 +154,7 @@ 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 pdf range = let output_page_info ?(json=false) pdf range =
let pages = Pdfpage.pages_of_pagetree pdf let pages = Pdfpage.pages_of_pagetree pdf
and labels = Pdfpagelabels.read pdf in and labels = Pdfpagelabels.read pdf in
let getbox page box = let getbox page box =
@ -173,6 +173,28 @@ let output_page_info pdf range =
and rotation page = and rotation page =
Pdfpage.int_of_rotation page.Pdfpage.rotate Pdfpage.int_of_rotation page.Pdfpage.rotate
in in
let json_entry_of_pnum pnum =
let getbox_json page box =
match getbox page box with
| "" -> `Null
| s ->
let a, b, c, d = Cpdfcoord.parse_rectangle (Pdf.empty ()) s in
`List [`Float a; `Float b; `Float c; `Float d]
in
let page = select pnum pages in
`Assoc
[("Page", `Int pnum);
("Label", (`String (try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> "")));
("MediaBox", getbox_json page "/MediaBox");
("CropBox", getbox_json page "/CropBox");
("BleedBox", getbox_json page "/BleedBox");
("TrimBox", getbox_json page "/TrimBox");
("ArtBox", getbox_json page "/ArtBox");
("Rotation", `Int (rotation page))]
in
if json then
flprint (Cpdfyojson.Safe.pretty_to_string (`List (map json_entry_of_pnum range)))
else
iter iter
(fun pnum -> (fun pnum ->
let page = select pnum pages in let page = select pnum pages in

View File

@ -1,7 +1,7 @@
(** 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 : Pdf.t -> int list -> unit val output_page_info : ?json:bool -> Pdf.t -> int list -> unit
(** 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. *)