beginning JSON annotations

This commit is contained in:
John Whitington 2021-10-27 18:55:52 +01:00
parent ec14b3ab9c
commit e918630ea4
4 changed files with 17 additions and 74 deletions

58
cpdf.ml
View File

@ -2640,64 +2640,6 @@ let get_annotations encoding pdf =
pages pages
(ilist 1 (length pages))) (ilist 1 (length pages)))
let list_annotations_more pdf =
let pages = Pdfpage.pages_of_pagetree pdf in
iter2
(fun page pagenumber ->
iter
(function annot ->
let print_annot annot =
let annot_type_string =
match annot.Pdfannot.subtype with
| Pdfannot.Stamp -> "Stamp"
| Pdfannot.Text -> "Text"
| Pdfannot.Link -> "Link"
| Pdfannot.FreeText -> "FreeText"
| Pdfannot.Line -> "Line"
| Pdfannot.Square -> "Square"
| Pdfannot.Circle -> "Circle"
| Pdfannot.Polygon -> "Polygon"
| Pdfannot.PolyLine -> "PolyLine"
| Pdfannot.Highlight -> "Highlight"
| Pdfannot.Underline -> "Underline"
| Pdfannot.Squiggly -> "Squiggly"
| Pdfannot.StrikeOut -> "StrikeOut"
| Pdfannot.Caret -> "Caret"
| Pdfannot.Ink -> "Ink"
| Pdfannot.Popup _ -> "Popup"
| Pdfannot.FileAttachment -> "FileAttachment"
| Pdfannot.Sound -> "Sound"
| Pdfannot.Movie -> "Movie"
| Pdfannot.Widget -> "Widget"
| Pdfannot.Screen -> "Screen"
| Pdfannot.PrinterMark -> "PrinterMark"
| Pdfannot.TrapNet -> "TrapNet"
| Pdfannot.Watermark -> "Watermark"
| Pdfannot.ThreeDee -> "ThreeDee"
| Pdfannot.Unknown -> "Unknown"
in let subject =
match annot.Pdfannot.subject with
| Some s -> s
| None -> ""
in let contents =
match annot.Pdfannot.annot_contents with
| Some s -> s
| None -> ""
in
Printf.printf "Page: %i\n" pagenumber;
Printf.printf "Subtype: %s\n" annot_type_string;
Printf.printf "Subject: %s\n" subject;
Printf.printf "Contents: %s\n" contents;
in
match annot.Pdfannot.subtype with
| Pdfannot.Popup annot -> print_annot annot
| _ -> print_annot annot
)
(Pdfannot.annotations_of_page pdf page))
pages
(ilist 1 (length pages));
flprint "" (* flush *)
(* Equalise the page lengths of two PDFs by chopping or extending the first one. (* Equalise the page lengths of two PDFs by chopping or extending the first one.
*) *)
let equalise_lengths a b = let equalise_lengths a b =

View File

@ -303,9 +303,6 @@ val padmultiple : int -> Pdf.t -> Pdf.t
(** 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 : encoding -> Pdf.t -> unit
(** The same, but giving more information. Deprecated *)
val list_annotations_more : 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

@ -107,7 +107,6 @@ type op =
| DumpAttachedFiles | DumpAttachedFiles
| RemoveAnnotations | RemoveAnnotations
| ListAnnotations | ListAnnotations
| ListAnnotationsMore
| CopyAnnotations of string | CopyAnnotations of string
| Merge | Merge
| Split | Split
@ -233,7 +232,6 @@ let string_of_op = function
| DumpAttachedFiles -> "DumpAttachedFiles" | DumpAttachedFiles -> "DumpAttachedFiles"
| RemoveAnnotations -> "RemoveAnnotations" | RemoveAnnotations -> "RemoveAnnotations"
| ListAnnotations -> "ListAnnotations" | ListAnnotations -> "ListAnnotations"
| ListAnnotationsMore -> "ListAnnotationsMore"
| CopyAnnotations _ -> "CopyAnnotations" | CopyAnnotations _ -> "CopyAnnotations"
| Merge -> "Merge" | Merge -> "Merge"
| Split -> "Split" | Split -> "Split"
@ -465,7 +463,7 @@ type args =
mutable impose_margin : float; mutable impose_margin : float;
mutable impose_spacing : float; mutable impose_spacing : float;
mutable impose_linewidth : float; mutable impose_linewidth : float;
mutable bookmarks_json : bool} mutable format_json : bool}
let args = let args =
{op = None; {op = None;
@ -579,7 +577,7 @@ let args =
impose_margin = 0.; impose_margin = 0.;
impose_spacing = 0.; impose_spacing = 0.;
impose_linewidth = 0.; impose_linewidth = 0.;
bookmarks_json = false} format_json = false}
let reset_arguments () = let reset_arguments () =
args.op <- None; args.op <- None;
@ -678,7 +676,7 @@ let reset_arguments () =
args.impose_margin <- 0.; args.impose_margin <- 0.;
args.impose_spacing <- 0.; args.impose_spacing <- 0.;
args.impose_linewidth <- 0.; args.impose_linewidth <- 0.;
args.bookmarks_json <- false args.format_json <- false
(* Do not reset original_filename or cpdflin or was_encrypted or (* Do not reset original_filename or cpdflin or was_encrypted or
* was_decrypted_with_owner or recrypt or producer or creator or path_to_* or * was_decrypted_with_owner or recrypt or producer or creator or path_to_* or
* gs_malformed or gs_quiet, since we want these to work across ANDs. Or * gs_malformed or gs_quiet, since we want these to work across ANDs. Or
@ -727,7 +725,7 @@ bans list in the input file, the operation cannot proceed. Other operations
cannot proceed at all without owner password. *) cannot proceed at all without owner password. *)
let banned banlist = function let banned banlist = function
| Fonts | Info | Metadata | PageInfo | Revisions | CountPages | Fonts | Info | Metadata | PageInfo | Revisions | CountPages
| ListAttachedFiles | ListAnnotationsMore | ListAnnotations | ListAttachedFiles | ListAnnotations
| ListBookmarks | ImageResolution _ | MissingFonts | ListBookmarks | ImageResolution _ | MissingFonts
| PrintPageLabels | Clean | Compress | Decompress | PrintPageLabels | Clean | Compress | Decompress
| RemoveUnusedResources | ChangeId | CopyId _ | ListSpotColours | Version | RemoveUnusedResources | ChangeId | CopyId _ | ListSpotColours | Version
@ -1066,11 +1064,15 @@ let setaddbookmarks s =
let setaddbookmarksjson s = let setaddbookmarksjson s =
setop (AddBookmarks s) (); setop (AddBookmarks s) ();
args.bookmarks_json <- true args.format_json <- true
let setlistbookmarksjson s = let setlistbookmarksjson () =
setop ListBookmarks (); setop ListBookmarks ();
args.bookmarks_json <- true args.format_json <- true
let setlistannotationsjson () =
setop ListBookmarks ();
args.format_json <- true
let setstampon f = let setstampon f =
setop (StampOn f) (); setop (StampOn f) ();
@ -2025,6 +2027,9 @@ and specs =
("-list-annotations", ("-list-annotations",
Arg.Unit (setop ListAnnotations), Arg.Unit (setop ListAnnotations),
" List annotations"); " List annotations");
("-list-annotations-json",
Arg.Unit setlistannotationsjson,
" List annotations in JSON format");
("-copy-annotations", ("-copy-annotations",
Arg.String setcopyannotations, Arg.String setcopyannotations,
" Copy annotations from given file"); " Copy annotations from given file");
@ -3448,7 +3453,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
Cpdf.list_bookmarks args.bookmarks_json args.encoding range pdf (Pdfio.output_of_channel stdout); Cpdf.list_bookmarks args.format_json args.encoding range pdf (Pdfio.output_of_channel stdout);
flush stdout flush stdout
| _ -> error "list-bookmarks: bad command line" | _ -> error "list-bookmarks: bad command line"
end end
@ -3835,8 +3840,6 @@ let go () =
end end
| Some ListAnnotations -> | Some ListAnnotations ->
Cpdf.list_annotations args.encoding (get_single_pdf args.op true) Cpdf.list_annotations args.encoding (get_single_pdf args.op true)
| Some ListAnnotationsMore ->
Cpdf.list_annotations_more (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
@ -3967,7 +3970,7 @@ let go () =
args.relative_to_cropbox args.underneath range pdf) args.relative_to_cropbox args.underneath range pdf)
| Some (AddBookmarks file) -> | Some (AddBookmarks file) ->
write_pdf false write_pdf false
(Cpdf.add_bookmarks ~json:args.bookmarks_json true (Pdfio.input_of_channel (open_in_bin file)) (Cpdf.add_bookmarks ~json:args.format_json true (Pdfio.input_of_channel (open_in_bin file))
(get_single_pdf args.op false)) (get_single_pdf args.op false))
| Some RemoveBookmarks -> | Some RemoveBookmarks ->
write_pdf false (Pdfmarks.remove_bookmarks (get_single_pdf args.op false)) write_pdf false (Pdfmarks.remove_bookmarks (get_single_pdf args.op false))

View File

@ -3,6 +3,7 @@
%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
\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.