From 1305853bd889d154ea076a4a7ae06195d577eff7 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Tue, 21 Jul 2020 12:28:32 +0100 Subject: [PATCH] more --- Changes | 1 + Makefile | 5 +++-- cpdf.ml | 37 +++++++++++++++++++++++++++++++++++-- cpdfwriteJSON.ml | 6 +++++- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/Changes b/Changes index 929d355..59fa2b8 100644 --- a/Changes +++ b/Changes @@ -7,6 +7,7 @@ o New -labels-progress option to improve page labels interface o New options -output-json et al. to export PDF files in JSON format o New option -stamp-as-xobject to add one PDF as an xobject in another o No longer depends on Bigarray or Unix modules +o Appearance streams transformed when transforming annotations Version 2.3 (patchlevel 1, December 2019) diff --git a/Makefile b/Makefile index 6d3da1b..d165c73 100644 --- a/Makefile +++ b/Makefile @@ -3,12 +3,13 @@ MODS = tjutil tjutf16 tjllist tjparserMonad tjjson \ xmlm \ cpdfwriteJSON cpdfstrftime cpdfcoord cpdf cpdfcommand -SOURCES = cpdftime.c $(foreach x,$(MODS),$(x).ml $(x).mli) cpdfcommandrun.ml +SOURCES = $(foreach x,$(MODS),$(x).ml $(x).mli) cpdfcommandrun.ml RESULT = cpdf ANNOTATE = true -PACKS = camlpdf +PACKS = unix camlpdf +CFLAGS = -fPIC OCAMLFLAGS = -bin-annot OCAMLNCFLAGS = -g -safe-string -w -3 OCAMLBCFLAGS = -g -safe-string -w -3 diff --git a/cpdf.ml b/cpdf.ml index 850a5e6..31cdb78 100644 --- a/cpdf.ml +++ b/cpdf.ml @@ -2287,6 +2287,10 @@ let change_pattern_matrices_page pdf tr page = page | _ -> page +(* test *) +let transform_xobject_in_place pdf transform i = + Printf.printf "transforming xobject %i as part of annotation\n" i + (* Apply transformations to any annotations in /Annots (i.e their /Rect entries) *) let transform_annotations pdf transform rest = (*Printf.printf "in transform_annotations\n";*) @@ -2313,8 +2317,37 @@ let transform_annotations pdf transform rest = Pdf.Array [Pdf.Real minx; Pdf.Real miny; Pdf.Real maxx; Pdf.Real maxy] | None -> raise (Pdf.PDFError "transform_annotations: no rect") in - let annot' = Pdf.add_dict_entry annot "/Rect" rect' in - Pdf.addobj_given_num pdf (i, annot') + let ap' = + match Pdf.lookup_direct pdf "/AP" annot with + None -> None + | Some dict -> Some dict + in + let annot = Pdf.add_dict_entry annot "/Rect" rect' in + begin match ap' with + None -> () + | Some (Pdf.Dictionary dict) -> + (* Each entry in the dictionary is either + * a) an indirect reference to a stream Form XObject + * b) a direct or indirect dictionary with some entries, + * each of which is an indirect reference to a stream. *) + (* We do this in place. *) + List.iter + (fun (k, v) -> + match v with + Pdf.Indirect i -> transform_xobject_in_place pdf transform i + | _ -> let dict = Pdf.lookup_direct pdf k (Pdf.Dictionary dict) in + match dict with Some (Pdf.Dictionary dict) -> + List.iter + (fun (_, v) -> + match v with + Pdf.Indirect i -> transform_xobject_in_place pdf transform i + | _ -> Printf.eprintf "Malformed /AP structure b"; ()) + dict + | _ -> Printf.eprintf "Malformed /AP structure c"; ()) + dict + | _ -> Printf.eprintf "Malformed /AP structure\n"; () + end; + Pdf.addobj_given_num pdf (i, annot) | _ -> Printf.eprintf "transform_annotations: not indirect") annots | _ -> () diff --git a/cpdfwriteJSON.ml b/cpdfwriteJSON.ml index a8c63ad..e5183d1 100644 --- a/cpdfwriteJSON.ml +++ b/cpdfwriteJSON.ml @@ -20,7 +20,11 @@ let rec json_of_object pdf fcs no_stream_data = function | P.Dictionary elts -> iter (function - ("/Contents", P.Indirect i) -> fcs i + ("/Contents", P.Indirect i) -> + begin match Pdf.lookup_obj pdf i with + | Pdf.Array is -> iter (function Pdf.Indirect i -> fcs i | _ -> ()) is + | _ -> fcs i + end | ("/Contents", P.Array elts) -> iter (function P.Indirect i -> fcs i | _ -> ()) elts | _ -> ()) elts;