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,19 +173,41 @@ 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
iter let json_entry_of_pnum pnum =
(fun pnum -> let getbox_json page box =
let page = select pnum pages in match getbox page box with
Printf.printf "Page %i:\n" pnum; | "" -> `Null
Printf.printf "Label: %s\n" | s ->
(try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> ""); let a, b, c, d = Cpdfcoord.parse_rectangle (Pdf.empty ()) s in
Printf.printf "MediaBox: %s\n" (getbox page "/MediaBox"); `List [`Float a; `Float b; `Float c; `Float d]
Printf.printf "CropBox: %s\n" (getbox page "/CropBox"); in
Printf.printf "BleedBox: %s\n" (getbox page "/BleedBox"); let page = select pnum pages in
Printf.printf "TrimBox: %s\n" (getbox page "/TrimBox"); `Assoc
Printf.printf "ArtBox: %s\n" (getbox page "/ArtBox"); [("Page", `Int pnum);
Printf.printf "Rotation: %i\n" (rotation page)) ("Label", (`String (try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> "")));
range ("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
(fun pnum ->
let page = select pnum pages in
Printf.printf "Page %i:\n" pnum;
Printf.printf "Label: %s\n"
(try Pdfpagelabels.pagelabeltext_of_pagenumber pnum labels with Not_found -> "");
Printf.printf "MediaBox: %s\n" (getbox page "/MediaBox");
Printf.printf "CropBox: %s\n" (getbox page "/CropBox");
Printf.printf "BleedBox: %s\n" (getbox page "/BleedBox");
Printf.printf "TrimBox: %s\n" (getbox page "/TrimBox");
Printf.printf "ArtBox: %s\n" (getbox page "/ArtBox");
Printf.printf "Rotation: %i\n" (rotation page))
range
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

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. *)