Adds -replace-stream

This commit is contained in:
John Whitington 2024-11-29 15:21:57 +00:00
parent dc477d6b2e
commit 58e2f98fcd
2 changed files with 16 additions and 14 deletions

View File

@ -569,7 +569,8 @@ type args =
mutable rast_annots : bool; mutable rast_annots : bool;
mutable rast_antialias : bool; mutable rast_antialias : bool;
mutable rast_jpeg_quality : int; mutable rast_jpeg_quality : int;
mutable rast_downsample : bool} mutable rast_downsample : bool;
mutable replace_stream_with : string}
let args = let args =
{op = None; {op = None;
@ -716,7 +717,8 @@ let args =
rast_annots = false; rast_annots = false;
rast_antialias = true; rast_antialias = true;
rast_jpeg_quality = 75; rast_jpeg_quality = 75;
rast_downsample = false} rast_downsample = false;
replace_stream_with = ""}
(* 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
@ -849,7 +851,8 @@ let reset_arguments () =
args.rast_annots <- false; args.rast_annots <- false;
args.rast_antialias <- true; args.rast_antialias <- true;
args.rast_jpeg_quality <- 75; args.rast_jpeg_quality <- 75;
args.rast_downsample <- false args.rast_downsample <- false;
args.replace_stream_with <- ""
(* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume (* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume
installed at a system place *) installed at a system place *)
@ -3003,6 +3006,7 @@ let specs =
("-extract-stream", Arg.String setextractstream, " Extract a stream"); ("-extract-stream", Arg.String setextractstream, " Extract a stream");
("-extract-stream-decompress", Arg.String setextractstreamdecomp, " Extract a stream, decompressing"); ("-extract-stream-decompress", Arg.String setextractstreamdecomp, " Extract a stream, decompressing");
("-replace-stream", Arg.String (fun s -> args.op <- Some (ReplaceStream s)), " Replace a stream"); ("-replace-stream", Arg.String (fun s -> args.op <- Some (ReplaceStream s)), " Replace a stream");
("-replace-stream-with", Arg.String (fun s -> args.replace_stream_with <- s), " File to replace stream with");
("-obj", Arg.String setprintobj, " Print object"); ("-obj", Arg.String setprintobj, " Print object");
("-obj-json", Arg.String setprintobjjson, " Print object in JSON format"); ("-obj-json", Arg.String setprintobjjson, " Print object in JSON format");
("-replace-obj", Arg.String setreplaceobj, "Replace object"); ("-replace-obj", Arg.String setreplaceobj, "Replace object");
@ -3634,16 +3638,13 @@ let extract_stream pdf decomp objnum =
(* Replace a stream from a file e.g 4=data.dat replaces contents of object 4. The stream dictionary is (* 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. *) altered only to correct the length. *)
let replace_stream pdf spec = let replace_stream pdf n filename =
match String.split_on_char '=' spec with let data = Pdfio.bytes_of_string (contents_of_file filename) in
| [n; filename] -> begin match Pdf.lookup_obj pdf n with
let data = Pdfio.bytes_of_string (contents_of_file filename) in | Pdf.Stream ({contents = dict, stream} as s) ->
begin match Pdf.lookup_obj pdf (int_of_string n) with s := (Pdf.add_dict_entry dict "/Length" (Pdf.Integer (bytes_size data)), Pdf.Got data)
| Pdf.Stream ({contents = dict, stream} as s) -> | _ -> error "not a stream"
s := (Pdf.add_dict_entry dict "/Length" (Pdf.Integer (bytes_size data)), Pdf.Got data) end
| _ -> error "not a stream"
end
| _ -> error "replace_stream: bad specifcation"
(* Empty string is trailerdict. Begins with / and it's a chain separated by (* Empty string is trailerdict. Begins with / and it's a chain separated by
commas. Begins with P and it's a page number then a (possibly empty) chain. commas. Begins with P and it's a page number then a (possibly empty) chain.
@ -4852,7 +4853,7 @@ let go () =
extract_stream pdf args.extract_stream_decompress s extract_stream pdf args.extract_stream_decompress s
| Some (ReplaceStream s) -> | Some (ReplaceStream s) ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
replace_stream pdf s; replace_stream pdf (int_of_string s) args.replace_stream_with;
write_pdf false pdf write_pdf false pdf
| Some (PrintObj s) -> | Some (PrintObj s) ->
let pdf = get_single_pdf args.op true in let pdf = get_single_pdf args.op true in

View File

@ -1,3 +1,4 @@
%Document -replace-stream
\documentclass{book} \documentclass{book}
% Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf, % Edit here to produce cpdfmanual.pdf, cpdflibmanual.pdf, pycpdfmanual.pdf,
% dotnetcpdflibmanual.pdf, jcpdflibmanual.pdf jscpdflibmanual.pdf etc. % dotnetcpdflibmanual.pdf, jcpdflibmanual.pdf jscpdflibmanual.pdf etc.