More attachment work

This commit is contained in:
John Whitington 2015-09-26 20:30:57 +01:00
parent a68136c6c2
commit 20ba0b1031
3 changed files with 19 additions and 6 deletions

10
cpdf.ml
View File

@ -759,6 +759,11 @@ let attach_file ?memory keepversion topage pdf file =
{pdf with {pdf with
Pdf.minor = if keepversion then pdf.Pdf.minor else max pdf.Pdf.minor 4} 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 list_attached_files pdf =
let toplevel = let toplevel =
match Pdf.lookup_direct pdf "/Root" pdf.Pdf.trailerdict with 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 match Pdf.lookup_direct pdf "/EmbeddedFiles" namedict with
| Some nametree -> | Some nametree ->
map map
(function x -> x, 0) (function x -> {name = x; pagenumber = 0; data = 0})
(option_map (option_map
(function (Pdf.String s, _) -> Some s | _ -> None) (function (Pdf.String s, _) -> Some s | _ -> None)
(Pdf.contents_of_nametree pdf nametree)) (Pdf.contents_of_nametree pdf nametree))
@ -785,7 +790,8 @@ let list_attached_files pdf =
match Pdf.lookup_direct pdf "/Subtype" annot with match Pdf.lookup_direct pdf "/Subtype" annot with
| Some (Pdf.Name "/FileAttachment") -> | Some (Pdf.Name "/FileAttachment") ->
(match Pdf.lookup_direct pdf "/Contents" annot with (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)
| _ -> None) | _ -> None)
(match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with (match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with

View File

@ -154,8 +154,13 @@ val attach_file : ?memory:Pdfio.bytes -> bool -> int option -> Pdf.t -> string -
(** Remove attached files. *) (** Remove attached files. *)
val remove_attached_files : Pdf.t -> Pdf.t 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. *) (** 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} *) (** {2 Bookmarks} *)

View File

@ -3463,9 +3463,11 @@ let go () =
write_pdf false (Cpdf.scale_contents ~fast:args.fast args.position scale pdf range) write_pdf false (Cpdf.scale_contents ~fast:args.fast args.position scale pdf range)
| Some ListAttachedFiles -> | Some ListAttachedFiles ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
let names, numbers = split (Cpdf.list_attached_files pdf) in let attachments = Cpdf.list_attached_files pdf in
iter2 (Printf.printf "%i %s\n") numbers names; iter
flprint "" (fun a -> Printf.printf "%i %s\n" a.Cpdf.pagenumber a.Cpdf.name)
attachments;
flprint ""
| Some DumpAttachedFiles -> | Some DumpAttachedFiles ->
let pdf = get_single_pdf args.op false in let pdf = get_single_pdf args.op false in
begin match args.out with begin match args.out with