This commit is contained in:
John Whitington 2021-10-28 15:34:03 +01:00
parent e918630ea4
commit 3ed74079c8
5 changed files with 20 additions and 8 deletions

View File

@ -1,7 +1,6 @@
2.5 (Upcoming 2022) 2.5 (Upcoming 2022)
o Output annotations in JSON form with -list-annotations-json 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 Basic merging of AcroForms
o Output bookmarks in JSON format with -list-bookmarks-json o Output bookmarks in JSON format with -list-bookmarks-json
o Load bookmarks in JSON format with -add-bookmarks-json o Load bookmarks in JSON format with -add-bookmarks-json

17
cpdf.ml
View File

@ -2621,9 +2621,22 @@ let list_page_annotations encoding pdf num page =
iter (print_annotation encoding pdf num) (map (Pdf.direct pdf) annots) 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 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 get_annotations encoding pdf =
let pages = Pdfpage.pages_of_pagetree pdf in let pages = Pdfpage.pages_of_pagetree pdf in

View File

@ -301,7 +301,7 @@ val padmultiple : int -> Pdf.t -> Pdf.t
(** {2 Annotations} *) (** {2 Annotations} *)
(** List the annotations to standard output in a given encoding. See cpdfmanual.pdf for the format details. *) (** 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 *) (** Return the annotations as a (pagenumber, content) list *)
val get_annotations : encoding -> Pdf.t -> (int * string) list val get_annotations : encoding -> Pdf.t -> (int * string) list

View File

@ -1071,7 +1071,7 @@ let setlistbookmarksjson () =
args.format_json <- true args.format_json <- true
let setlistannotationsjson () = let setlistannotationsjson () =
setop ListBookmarks (); setop ListAnnotations ();
args.format_json <- true args.format_json <- true
let setstampon f = let setstampon f =
@ -3839,7 +3839,7 @@ let go () =
| _ -> error "copy-annotations: No input file specified" | _ -> error "copy-annotations: No input file specified"
end end
| Some ListAnnotations -> | 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 -> | Some Shift ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in

View File

@ -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 -decrypt-force
%Document -collate %Document -collate
%Document -impose and friends (inc. 0-w, 0-h for long ones, how lines scale etc., undefined if pages different sizes) %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 -bookmarks-json including mentioning UTF8
%Document -list-annotations-json and -add-annotations-json %Document -list-annotations-json
\documentclass{book} \documentclass{book}
% Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf etc. % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf etc.