From 7274a83b2bfa00327b7943c19512942ba9d13823 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Thu, 8 Feb 2024 14:33:11 +0000 Subject: [PATCH] Support for JSON page info in cpdflib --- cpdfpage.ml | 14 ++++++++++++-- cpdfpage.mli | 4 +++- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cpdfpage.ml b/cpdfpage.ml index 7781460..2c0fa5a 100644 --- a/cpdfpage.ml +++ b/cpdfpage.ml @@ -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} (* 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 labels = Pdfpagelabels.read pdf in let getbox page box = @@ -200,7 +202,11 @@ let output_page_info ?(json=false) pdf range = ("Annotations", `Int (num_annots page))] in 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 iter (fun pnum -> @@ -217,6 +223,10 @@ let output_page_info ?(json=false) pdf range = Printf.printf "Annotations: %i\n" (num_annots page)) 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 pages = Pdfpage.pages_of_pagetree pdf in let pages', pagenumbers, matrices = (* new page objects, page number, matrix *) diff --git a/cpdfpage.mli b/cpdfpage.mli index 3574afa..cdc9e47 100644 --- a/cpdfpage.mli +++ b/cpdfpage.mli @@ -1,7 +1,9 @@ (** Working with pages *) (** 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 of page numbers to apply it to, apply the function to all those pages. *)