diff --git a/Changes b/Changes index e40f7ff..1a753b4 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,6 @@ 2.5 (Upcoming 2022) o Output annotations in JSON form with -list-annotations-json -o Load annotations in JSON form with -add-annotations-json o Basic merging of AcroForms o Output bookmarks in JSON format with -list-bookmarks-json o Load bookmarks in JSON format with -add-bookmarks-json diff --git a/cpdf.ml b/cpdf.ml index d5dfac2..d41d65f 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -2621,9 +2621,22 @@ let list_page_annotations encoding pdf num page = iter (print_annotation encoding pdf num) (map (Pdf.direct pdf) annots) | _ -> () -let list_annotations encoding pdf = +let annotations_json_page pdf page = + match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with + | Some (Pdf.Array annots) -> + map (Cpdfjson.json_of_object pdf (fun _ -> ()) false false) (map (Pdf.direct pdf) annots) + | _ -> [] + +let list_annotations_json pdf = + let module J = Cpdfyojson.Safe in + let json = `List (flatten (map (annotations_json_page pdf) (Pdfpage.pages_of_pagetree pdf))) in + J.pretty_to_channel stdout json + +let list_annotations ~json encoding pdf = let range = Cpdfpagespec.parse_pagespec pdf "all" in - iter_pages (list_page_annotations encoding pdf) pdf range + if json + then list_annotations_json pdf + else iter_pages (list_page_annotations encoding pdf) pdf range let get_annotations encoding pdf = let pages = Pdfpage.pages_of_pagetree pdf in diff --git a/cpdf.mli b/cpdf.mli index 5e21c7b..4a8c2cb 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -301,7 +301,7 @@ val padmultiple : int -> Pdf.t -> Pdf.t (** {2 Annotations} *) (** List the annotations to standard output in a given encoding. See cpdfmanual.pdf for the format details. *) -val list_annotations : encoding -> Pdf.t -> unit +val list_annotations : json:bool -> encoding -> Pdf.t -> unit (** Return the annotations as a (pagenumber, content) list *) val get_annotations : encoding -> Pdf.t -> (int * string) list diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 840edf4..4a894dc 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1071,7 +1071,7 @@ let setlistbookmarksjson () = args.format_json <- true let setlistannotationsjson () = - setop ListBookmarks (); + setop ListAnnotations (); args.format_json <- true let setstampon f = @@ -3839,7 +3839,7 @@ let go () = | _ -> error "copy-annotations: No input file specified" end | Some ListAnnotations -> - Cpdf.list_annotations args.encoding (get_single_pdf args.op true) + Cpdf.list_annotations ~json:args.format_json args.encoding (get_single_pdf args.op true) | Some Shift -> let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in diff --git a/cpdfmanual.tex b/cpdfmanual.tex index 32481ed..a70c2f0 100644 --- a/cpdfmanual.tex +++ b/cpdfmanual.tex @@ -1,9 +1,9 @@ -%Document the new JSON format, -j and new -output-json options +%Document the new JSON format, -j and new -output-json options including UTF8 %Document -decrypt-force %Document -collate %Document -impose and friends (inc. 0-w, 0-h for long ones, how lines scale etc., undefined if pages different sizes) %Document -bookmarks-json including mentioning UTF8 -%Document -list-annotations-json and -add-annotations-json +%Document -list-annotations-json \documentclass{book} % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf etc.