diff --git a/cpdf.ml b/cpdf.ml index c93b686..826e37f 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -759,6 +759,11 @@ let attach_file ?memory keepversion topage pdf file = {pdf with Pdf.minor = if keepversion then pdf.Pdf.minor else max pdf.Pdf.minor 4} +type attachment = + {name : string; + pagenumber : int; + data : int} + let list_attached_files pdf = let toplevel = match Pdf.lookup_direct pdf "/Root" pdf.Pdf.trailerdict with @@ -770,7 +775,7 @@ let list_attached_files pdf = match Pdf.lookup_direct pdf "/EmbeddedFiles" namedict with | Some nametree -> map - (function x -> x, 0) + (function x -> {name = x; pagenumber = 0; data = 0}) (option_map (function (Pdf.String s, _) -> Some s | _ -> None) (Pdf.contents_of_nametree pdf nametree)) @@ -785,7 +790,8 @@ let list_attached_files pdf = match Pdf.lookup_direct pdf "/Subtype" annot with | Some (Pdf.Name "/FileAttachment") -> (match Pdf.lookup_direct pdf "/Contents" annot with - | Some (Pdf.String s) -> Some (s, pagenumber) + | Some (Pdf.String s) -> + Some {name = s; pagenumber; data = 0} | _ -> None) | _ -> None) (match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with diff --git a/cpdf.mli b/cpdf.mli index a92746e..f28252a 100644 --- a/cpdf.mli +++ b/cpdf.mli @@ -154,8 +154,13 @@ val attach_file : ?memory:Pdfio.bytes -> bool -> int option -> Pdf.t -> string - (** Remove attached files. *) val remove_attached_files : Pdf.t -> Pdf.t +type attachment = + {name : string; + pagenumber : int; + data : int} + (** List attached files. Attachment name and page number. Page 0 is document level. *) -val list_attached_files : Pdf.t -> (string * int) list +val list_attached_files : Pdf.t -> attachment list (** {2 Bookmarks} *) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index cce53bb..6b325bf 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -3463,9 +3463,11 @@ let go () = write_pdf false (Cpdf.scale_contents ~fast:args.fast args.position scale pdf range) | Some ListAttachedFiles -> let pdf = get_single_pdf args.op false in - let names, numbers = split (Cpdf.list_attached_files pdf) in - iter2 (Printf.printf "%i %s\n") numbers names; - flprint "" + let attachments = Cpdf.list_attached_files pdf in + iter + (fun a -> Printf.printf "%i %s\n" a.Cpdf.pagenumber a.Cpdf.name) + attachments; + flprint "" | Some DumpAttachedFiles -> let pdf = get_single_pdf args.op false in begin match args.out with