From 935431e52a498201d14a9910ab23c0e235b8b97f Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 1 Jan 2025 16:54:28 +0000 Subject: [PATCH] -replace-stream now works with objspecs --- cpdfcommand.ml | 2 +- cpdftweak.ml | 19 ++++++++----------- cpdftweak.mli | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index c47d4e4..65f4e20 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4840,7 +4840,7 @@ let go () = extract_stream pdf args.extract_stream_decompress s | Some (ReplaceStream s) -> let pdf = get_single_pdf args.op false in - Cpdftweak.replace_stream pdf (int_of_string s) args.replace_stream_with; + Cpdftweak.replace_stream pdf s args.replace_stream_with; write_pdf false pdf | Some (PrintObj s) -> let pdf = get_single_pdf args.op true in diff --git a/cpdftweak.ml b/cpdftweak.ml index 8c43015..4e67b77 100644 --- a/cpdftweak.ml +++ b/cpdftweak.ml @@ -307,7 +307,8 @@ let remove_clipping pdf range = commas. Begins with P and it's a page number then a (possibly empty) chain. Otherwise it's an object number (0 = trailerdict) then a (possibly empty) chain. *) -let split_chain str = map (fun x -> "/" ^ x) (tl (String.split_on_char '/' str)) +let split_chain str = + map (fun x -> "/" ^ x) (tl (String.split_on_char '/' str)) let find_obj pdf objspec = let simple_obj obj = @@ -334,18 +335,14 @@ let find_obj pdf objspec = chain_obj (int_of_string (implode digits)) (split_chain (implode rest)) let replace_obj pdf objspec obj = - let split_chain str = map (fun x -> "/" ^ x) (tl (String.split_on_char '/' str)) in - let chain = split_chain objspec in - try - Pdf.replace_chain pdf chain obj - with - e -> Pdfe.log "Chain not found"; exit 2 + try Pdf.replace_chain pdf (split_chain objspec) obj with + _ -> raise (Pdf.PDFError "Chain not found") -(* Replace a stream from a file e.g 4=data.dat replaces contents of object 4. The stream dictionary is -altered only to correct the length. *) -let replace_stream pdf n filename = +(* Replace a stream from a file e.g 4=data.dat replaces contents of object 4. + The stream dictionary is altered only to correct the length. *) +let replace_stream pdf objspec filename = let data = Pdfio.bytes_of_string (contents_of_file filename) in - begin match Pdf.lookup_obj pdf n with + begin match find_obj pdf objspec with | Pdf.Stream ({contents = dict, stream} as s) -> s := (Pdf.add_dict_entry dict "/Length" (Pdf.Integer (bytes_size data)), Pdf.Got data) | _ -> error "not a stream" diff --git a/cpdftweak.mli b/cpdftweak.mli index b9c58bb..984584c 100644 --- a/cpdftweak.mli +++ b/cpdftweak.mli @@ -32,6 +32,6 @@ val remove_clipping : Pdf.t -> int list -> Pdf.t val find_obj : Pdf.t -> string -> Pdf.pdfobject -val replace_stream : Pdf.t -> int -> string -> unit +val replace_stream : Pdf.t -> string -> string -> unit val replace_obj : Pdf.t -> string -> Pdf.pdfobject -> unit