From bd7cba7730cb13c54280d26ee3d15da039536f3c Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 18 Apr 2023 16:39:51 +0100 Subject: [PATCH] more --- cpdfannot.ml | 18 ++++++++++++++++-- cpdfannot.mli | 20 ++++++++------------ 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/cpdfannot.ml b/cpdfannot.ml index 50e2701..c9f17c3 100644 --- a/cpdfannot.ml +++ b/cpdfannot.ml @@ -1,4 +1,5 @@ open Pdfutil +open Cpdferror (* List annotations *) let get_annotation_string encoding pdf annot = @@ -200,8 +201,21 @@ let get_annotations_json pdf = let json = `List (flatten (map2 (annotations_json_page pdf) pages pagenums)) in Pdfio.bytes_of_string (J.to_string json) -(** Set annotations from JSON. Existing annotations? *) -let set_annotations_json pdf json = () +(** Set annotations from JSON, keeping any existing ones. *) +let set_annotations_json pdf i = + let module J = Cpdfyojson.Safe in + let content = Pdfio.string_of_bytes (Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length) in + let json = J.from_string content in + (* Find largest negative objnumber. Then add number of annot objects. *) + match json with + | `List entries -> + let maxobjnum = + fold_left max min_int (map (fun e -> match e with `List (`Int i::_) -> abs i | _ -> error "Bad annots JSON entry") entries) + in + (* Renumber the PDF so everything has bigger object numbers than that. *) + Printf.printf "maxobjnum = %i\n" maxobjnum; + () + | _ -> error "Bad Annotations JSON file" (* Equalise the page lengths of two PDFs by chopping or extending the first one. *) diff --git a/cpdfannot.mli b/cpdfannot.mli index 3e95d6a..235e152 100644 --- a/cpdfannot.mli +++ b/cpdfannot.mli @@ -1,6 +1,13 @@ (** Annotations *) -(** Old fashioned functions first, still in use for backward-compatibilty *) +(** Remove the annotations on given pages. *) +val remove_annotations : int list -> Pdf.t -> Pdf.t + +(** Get annotations as JSON *) +val get_annotations_json : Pdf.t -> Pdfio.bytes + +(** Set annotations from JSON. *) +val set_annotations_json : Pdf.t -> Pdfio.input -> unit (** Return the annotations as a (pagenumber, content) list. *) val get_annotations : Cpdfmetadata.encoding -> Pdf.t -> (int * string) list @@ -10,14 +17,3 @@ val copy_annotations : int list -> Pdf.t -> Pdf.t -> Pdf.t (** List the annotations to standard output in a given encoding. See cpdfmanual.pdf for the format details. *) val list_annotations : json:bool -> int list -> Cpdfmetadata.encoding -> Pdf.t -> unit - -(** Remove the annotations on given pages. *) -val remove_annotations : int list -> Pdf.t -> Pdf.t - -(** Modern functions *) - -(** Get annotations as JSON *) -val get_annotations_json : Pdf.t -> Pdfio.bytes - -(** Set annotations from JSON. Existing annotations will be removed. *) -val set_annotations_json : Pdf.t -> Pdfio.input -> unit