From 7b8032a6626997eb7e25c39caddda39d1e157ed0 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 2 Mar 2020 13:37:39 +0100 Subject: [PATCH] Added -ocg-order-all --- cpdf.ml | 19 +++++++++++++++++++ cpdf.mli | 2 ++ cpdfcommand.ml | 9 ++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/cpdf.ml b/cpdf.ml index a73c2be..cbd2e9e 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -4326,3 +4326,22 @@ let ocg_rename f t pdf = | x -> x ) pdf + +let ocg_order_all pdf = + match Pdf.lookup_direct pdf "/OCProperties" (Pdf.catalog_of_pdf pdf) with + None -> () + | Some ocpdict -> + match Pdf.lookup_direct pdf "/OCGs" ocpdict with + Some (Pdf.Array elts) -> + begin match Pdf.lookup_direct pdf "/D" ocpdict with + Some (Pdf.Dictionary d) -> + let newd = Pdf.add_dict_entry (Pdf.Dictionary d) "/Order" (Pdf.Array elts) in + let new_ocproperties = Pdf.add_dict_entry ocpdict "/D" newd in + let ocp_objnum = Pdf.addobj pdf new_ocproperties in + let new_catalog = Pdf.addobj pdf (Pdf.add_dict_entry (Pdf.catalog_of_pdf pdf) "/OCProperties" (Pdf.Indirect ocp_objnum)) in + pdf.Pdf.trailerdict <- Pdf.add_dict_entry pdf.Pdf.trailerdict "/Root" (Pdf.Indirect new_catalog); + pdf.Pdf.root <- new_catalog + | _ -> () + end + | _ -> () + diff --git a/cpdf.mli b/cpdf.mli index 95dd36c..fd4b50e 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -456,3 +456,5 @@ val ocg_list : Pdf.t -> unit val ocg_rename : string -> string -> Pdf.t -> unit +val ocg_order_all : Pdf.t -> unit + diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 66c9cc0..41a10f2 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -193,6 +193,7 @@ type op = | OCGCoalesce | OCGList | OCGRename + | OCGOrderAll let string_of_op = function | CopyFont _ -> "CopyFont" @@ -319,6 +320,7 @@ let string_of_op = function | OCGCoalesce -> "OCGCoalesce" | OCGList -> "OCGList" | OCGRename -> "OCGRename" + | OCGOrderAll -> "OCGOrderAll" (* Inputs: filename, pagespec. *) type input_kind = @@ -695,7 +697,7 @@ let banned banlist = function | SetModify _|SetCreator _|SetProducer _|RemoveDictEntry _ | SetMetadata _ | ExtractText | ExtractImages | ExtractFontFile | AddPageLabels | RemovePageLabels | OutputJSON | OCGCoalesce - | OCGRename | OCGList + | OCGRename | OCGList | OCGOrderAll -> false (* Always allowed *) (* Combine pages is not allowed because we would not know where to get the -recrypt from -- the first or second file? *) @@ -2123,6 +2125,7 @@ and specs = ("-ocg-rename", Arg.Unit (setop OCGRename), ""); ("-ocg-rename-from", Arg.String setocgrenamefrom, ""); ("-ocg-rename-to", Arg.String setocgrenameto, ""); + ("-ocg-order-all", Arg.Unit (setop OCGOrderAll), ""); (* These items are undocumented *) ("-remove-unused-resources", Arg.Unit (setop RemoveUnusedResources), ""); ("-stay-on-error", Arg.Unit setstayonerror, ""); @@ -4484,6 +4487,10 @@ let go () = let pdf = get_single_pdf args.op false in Cpdf.ocg_rename args.ocgrenamefrom args.ocgrenameto pdf; write_pdf false pdf + | Some OCGOrderAll -> + let pdf = get_single_pdf args.op false in + Cpdf.ocg_order_all pdf; + write_pdf false pdf let parse_argv () = if args.debug then