Scaffolding for -remove-obj

This commit is contained in:
John Whitington
2025-03-26 20:35:21 +00:00
parent 715d16db1e
commit 19762f09df
3 changed files with 13 additions and 2 deletions

View File

@ -223,6 +223,7 @@ type op =
| ReplaceStream of string | ReplaceStream of string
| PrintObj of string | PrintObj of string
| ReplaceObj of string * string | ReplaceObj of string * string
| RemoveObj of string
| Verify of string | Verify of string
| MarkAs of Cpdfua.subformat | MarkAs of Cpdfua.subformat
| RemoveMark of Cpdfua.subformat | RemoveMark of Cpdfua.subformat
@ -393,6 +394,7 @@ let string_of_op = function
| Redact -> "Redact" | Redact -> "Redact"
| Rasterize -> "Rasterize" | Rasterize -> "Rasterize"
| OutputImage -> "OutputImage" | OutputImage -> "OutputImage"
| RemoveObj _ -> "RemoveObj"
(* Inputs: filename, pagespec. *) (* Inputs: filename, pagespec. *)
type input_kind = type input_kind =
@ -964,7 +966,7 @@ let banned banlist = function
| ExtractText | ExtractImages | ExtractFontFile _ | ExtractText | ExtractImages | ExtractFontFile _
| AddPageLabels | RemovePageLabels | OutputJSON | OCGCoalesce | AddPageLabels | RemovePageLabels | OutputJSON | OCGCoalesce
| OCGRename | OCGList | OCGOrderAll | PrintFontEncoding _ | TableOfContents | Typeset _ | Composition _ | OCGRename | OCGList | OCGOrderAll | PrintFontEncoding _ | TableOfContents | Typeset _ | Composition _
| TextWidth _ | SetAnnotations _ | CopyAnnotations _ | ExtractStream _ | ReplaceStream _ | PrintObj _ | ReplaceObj _ | TextWidth _ | SetAnnotations _ | CopyAnnotations _ | ExtractStream _ | ReplaceStream _ | PrintObj _ | ReplaceObj _ | RemoveObj _
| Verify _ | MarkAs _ | RemoveMark _ | ExtractStructTree | ReplaceStructTree _ | SetLanguage _ | Verify _ | MarkAs _ | RemoveMark _ | ExtractStructTree | ReplaceStructTree _ | SetLanguage _
| PrintStructTree | Rasterize | OutputImage | RemoveStructTree | MarkAsArtifact | PrintStructTree | Rasterize | OutputImage | RemoveStructTree | MarkAsArtifact
-> false (* Always allowed *) -> false (* Always allowed *)
@ -3031,7 +3033,8 @@ let specs =
("-replace-stream-with", Arg.String (fun s -> args.replace_stream_with <- s), " File to replace stream with"); ("-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");
("-remove-obj", Arg.String (fun s -> setop (RemoveObj s) ()), " Remove object");
("-json", Arg.Unit (fun () -> args.format_json <- true), " Format output as JSON"); ("-json", Arg.Unit (fun () -> args.format_json <- true), " Format output as JSON");
("-verify", Arg.String (fun s -> setop (Verify s) ()), " Verify conformance to a standard"); ("-verify", Arg.String (fun s -> setop (Verify s) ()), " Verify conformance to a standard");
("-verify-single", Arg.String (fun s -> args.verify_single <- Some s), " Verify a single test"); ("-verify-single", Arg.String (fun s -> args.verify_single <- Some s), " Verify a single test");
@ -4866,6 +4869,9 @@ let go () =
let pdfobj = pdf_or_json b in let pdfobj = pdf_or_json b in
Cpdftweak.replace_obj pdf a pdfobj; Cpdftweak.replace_obj pdf a pdfobj;
write_pdf false pdf write_pdf false pdf
| Some (RemoveObj s) ->
let pdf = get_single_pdf args.op true in
Cpdftweak.remove_obj pdf s
| Some (Verify standard) -> | Some (Verify standard) ->
begin match standard with begin match standard with
| "PDF/UA-1(matterhorn)" -> | "PDF/UA-1(matterhorn)" ->

View File

@ -313,6 +313,8 @@ let replace_obj pdf objspec obj =
try Pdf.replace_chain pdf (split_chain objspec) obj with try Pdf.replace_chain pdf (split_chain objspec) obj with
_ -> raise (Pdf.PDFError "Chain not found") _ -> raise (Pdf.PDFError "Chain not found")
let remove_obj pdf objspec = ()
(* Replace a stream from a file e.g 4=data.dat replaces contents of object 4. (* 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. *) The stream dictionary is altered only to correct the length. *)
let replace_stream pdf objspec filename = let replace_stream pdf objspec filename =

View File

@ -32,3 +32,6 @@ val replace_stream : Pdf.t -> string -> string -> unit
(** [replace_obj pdf objspec obj] replace the object at [objspec] (which must exist) with [obj]. *) (** [replace_obj pdf objspec obj] replace the object at [objspec] (which must exist) with [obj]. *)
val replace_obj : Pdf.t -> string -> Pdf.pdfobject -> unit val replace_obj : Pdf.t -> string -> Pdf.pdfobject -> unit
(** [remove_obj pdf objspec] removes the object described by the object specification. *)
val remove_obj : Pdf.t -> string -> unit