forked from johnwhitington/camlpdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfannot.ml
193 lines (183 loc) · 5.56 KB
/
pdfannot.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
(* Read and Write Annotations *)
open Pdfutil
(* Annotation Border Styles *)
type style =
| NoStyle
| Solid
| Dashed
| Beveled
| Inset
| UnderlineStyle
type border =
{width : float;
vradius : float;
hradius : float;
style : style;
dasharray : int array}
type subtype =
| Text
| Link
| FreeText
| Line
| Square
| Circle
| Polygon
| PolyLine
| Highlight
| Underline
| Squiggly
| StrikeOut
| Stamp
| Caret
| Ink
| Popup of t
| FileAttachment
| Sound
| Movie
| Widget
| Screen
| PrinterMark
| TrapNet
| Watermark
| ThreeDee
| Unknown
(* Main type. 'rest' contains the raw annotation dictionary with the exception
of the entries corresponding to the other items in the record. *)
and t =
{subtype : subtype;
annot_contents : string option;
subject : string option;
rectangle : float * float * float * float;
border : border;
colour : (int * int * int) option;
annotrest : Pdf.pdfobject}
(* Read a single annotation *)
let rec read_annotation pdf annot =
let subtype =
match Pdf.lookup_direct pdf "/Subtype" annot with
| Some (Pdf.Name "/Text") -> Text
| Some (Pdf.Name "/FreeText") -> FreeText
| Some (Pdf.Name "/Popup") ->
(* Look up /Parent. If exists, include it *)
begin match Pdf.direct pdf annot with
| Pdf.Dictionary d ->
begin match lookup "/Parent" d with
| Some (Pdf.Indirect i) ->
Popup (read_annotation pdf (Pdf.Indirect i))
| _ -> Unknown
end
| _ -> raise (Pdf.PDFError "read_annotation failed")
end
| Some (Pdf.Name "/Stamp") -> Stamp
| Some (Pdf.Name "/Link") -> Link
| _ -> Unknown
in let contents =
match Pdf.lookup_direct pdf "/Contents" annot with
| Some (Pdf.String s) -> Some s
| _ -> None
in let subject =
match Pdf.lookup_direct pdf "/Subj" annot with
| Some (Pdf.String s) -> Some s
| _ -> None
in let rectangle =
Pdf.parse_rectangle (Pdf.lookup_fail "No /rect in annot" pdf "/Rect" annot)
in let border =
match Pdf.lookup_direct pdf "/BS" annot with
| Some bsdict ->
let width =
match Pdf.lookup_direct pdf "/W" bsdict with
| Some x -> Pdf.getnum x
| _ -> 1.
in let style =
match Pdf.lookup_direct pdf "/S" bsdict with
| Some (Pdf.Name "/S") -> Solid
| Some (Pdf.Name "/D") -> Dashed
| Some (Pdf.Name "/B") -> Beveled
| Some (Pdf.Name "/I") -> Inset
| Some (Pdf.Name "/U") -> UnderlineStyle
| _ -> NoStyle
in let dasharray =
match Pdf.lookup_direct pdf "/D" bsdict with
| Some (Pdf.Array dash) ->
Array.of_list
(map int_of_float (map Pdf.getnum (map (Pdf.direct pdf) dash)))
| _ -> [||]
in
{width = width;
vradius = 0.;
hradius = 0.;
style = style;
dasharray = dasharray}
| None ->
match Pdf.lookup_direct pdf "/Border" annot with
| Some (Pdf.Array [h; v; w]) ->
{width = Pdf.getnum (Pdf.direct pdf w);
vradius = Pdf.getnum (Pdf.direct pdf v);
hradius = Pdf.getnum (Pdf.direct pdf h);
style = NoStyle;
dasharray = [||]}
| Some (Pdf.Array [h; v; w; Pdf.Array dash]) ->
{width = Pdf.getnum (Pdf.direct pdf w);
vradius = Pdf.getnum (Pdf.direct pdf v);
hradius = Pdf.getnum (Pdf.direct pdf h);
style = NoStyle;
dasharray =
Array.of_list
(map
int_of_float
(map Pdf.getnum (map (Pdf.direct pdf) dash)))}
| _ ->
{width = 1.;
vradius = 0.;
hradius = 0.;
style = NoStyle;
dasharray = [||]}
in let colour =
match Pdf.lookup_direct pdf "/C" annot with
| Some (Pdf.Array [r; g; b]) ->
Some (int_of_float (Pdf.getnum (Pdf.direct pdf r)),
int_of_float (Pdf.getnum (Pdf.direct pdf g)),
int_of_float (Pdf.getnum (Pdf.direct pdf b)))
| _ -> None
in let annotrest =
match Pdf.direct pdf annot with
| Pdf.Dictionary entries ->
Pdf.Dictionary
(lose
(fun (k, _) -> mem k ["/Subtype"; "/Contents"; "/Rect"; "/Border"])
entries)
| _ -> raise (Pdf.PDFError "Bad annotation dictionary")
in
{subtype = subtype;
annot_contents = contents;
subject = subject;
rectangle = rectangle;
border = border;
colour = colour;
annotrest = annotrest}
let get_popup_parent pdf annotation =
match Pdf.direct pdf annotation with
| Pdf.Dictionary d ->
begin match lookup "/Parent" d with
| Some (Pdf.Indirect i) -> Some i
| _ -> None
end
| _ -> raise (Pdf.PDFError "Pdfannot.get_popup_parent: not a dictionary")
(* Read the annotations from a page. *)
let annotations_of_page pdf page =
match Pdf.lookup_direct pdf "/Annots" page.Pdfpage.rest with
| Some (Pdf.Array annotations) ->
(* We don't read annotations which are parents of Popup annotations - they
will be caught anyway. This seems to be the right thing to do, but will
need more advice. *)
let popup_parents =
option_map (get_popup_parent pdf) annotations
in
map
(read_annotation pdf)
(lose
(function Pdf.Indirect i -> mem i popup_parents | _ -> false)
annotations)
| _ -> []
(* Add an annotation to a page *)
(*i let add_annotation pdf page annotation = () i*)