Fetching data for attachments

This commit is contained in:
John Whitington 2015-09-27 19:10:06 +01:00
parent 20ba0b1031
commit 36df106ec0
2 changed files with 36 additions and 4 deletions

38
cpdf.ml
View File

@ -762,7 +762,7 @@ let attach_file ?memory keepversion topage pdf file =
type attachment = type attachment =
{name : string; {name : string;
pagenumber : int; pagenumber : int;
data : int} data : unit -> Pdfio.bytes}
let list_attached_files pdf = let list_attached_files pdf =
let toplevel = let toplevel =
@ -775,7 +775,10 @@ 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 -> {name = x; pagenumber = 0; data = 0}) (function x ->
{name = x;
pagenumber = 0;
data = (fun () -> Pdfio.mkbytes 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))
@ -791,7 +794,36 @@ let list_attached_files pdf =
| 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 (Pdf.String s) ->
Some {name = s; pagenumber; data = 0} begin match Pdf.lookup_direct pdf "/FS" annot with
| Some ((Pdf.Dictionary _) as d) ->
Printf.eprintf "%s\n" (Pdfwrite.string_of_pdf d);
begin match Pdf.lookup_direct pdf "/EF" d with
| Some ((Pdf.Dictionary _) as d) ->
begin match Pdf.lookup_direct pdf "/F" d with
| Some stream ->
Some
{name = s;
pagenumber;
data =
(fun () ->
try
Pdf.getstream stream;
Pdfcodec.decode_pdfstream pdf stream;
match stream with
Pdf.Stream {contents = (_, Pdf.Got data)} -> data
| _ -> raise Not_found
with
_ -> raise (Pdf.PDFError "could not retreive attachment data"))}
| _ -> raise (Pdf.PDFError "no /F found in attachment")
end
| _ ->
Some
{name = s;
pagenumber;
data = (fun () -> raise (Pdf.PDFError "no attachment data"))}
end
| _ -> None
end
| _ -> 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

@ -157,7 +157,7 @@ val remove_attached_files : Pdf.t -> Pdf.t
type attachment = type attachment =
{name : string; {name : string;
pagenumber : int; pagenumber : int;
data : int} data : unit -> Pdfio.bytes}
(** 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 -> attachment list val list_attached_files : Pdf.t -> attachment list