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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! Hyperlink and AcroForm field extraction.
use crate::types::{ItemType, TextItem};
use lopdf::{Document, Object, ObjectId};
use std::collections::HashMap;
use super::fonts::{resolve_array, resolve_dict};
use super::get_number;
pub fn extract_page_links(doc: &Document, page_id: ObjectId, page_num: u32) -> Vec<TextItem> {
let mut links = Vec::new();
// Try to get the page dictionary
if let Ok(page_dict) = doc.get_dictionary(page_id) {
// Get Annots array
let annots = if let Ok(annots_ref) = page_dict.get(b"Annots") {
if let Ok(obj_ref) = annots_ref.as_reference() {
doc.get_object(obj_ref)
.ok()
.and_then(|o| o.as_array().ok().cloned())
} else {
annots_ref.as_array().ok().cloned()
}
} else {
None
};
if let Some(annots) = annots {
for annot_ref in annots {
// Get annotation dictionary
let annot_dict = if let Ok(obj_ref) = annot_ref.as_reference() {
doc.get_dictionary(obj_ref).ok()
} else {
annot_ref.as_dict().ok()
};
if let Some(annot_dict) = annot_dict {
// Check if this is a Link annotation
if let Ok(subtype) = annot_dict.get(b"Subtype") {
if let Ok(subtype_name) = subtype.as_name() {
if subtype_name != b"Link" {
continue;
}
}
}
// Get the Rect (position)
let rect = if let Ok(rect_obj) = annot_dict.get(b"Rect") {
if let Ok(rect_array) = rect_obj.as_array() {
if rect_array.len() >= 4 {
let x1 = get_number(&rect_array[0]).unwrap_or(0.0);
let y1 = get_number(&rect_array[1]).unwrap_or(0.0);
let x2 = get_number(&rect_array[2]).unwrap_or(0.0);
let y2 = get_number(&rect_array[3]).unwrap_or(0.0);
Some((x1, y1, x2 - x1, y2 - y1))
} else {
None
}
} else {
None
}
} else {
None
};
// Get the action (A dictionary) or Dest
let uri = extract_link_uri(doc, annot_dict);
if let (Some((x, y, width, height)), Some(url)) = (rect, uri) {
links.push(TextItem {
text: url.clone(),
x,
y,
width,
height,
font: String::new(),
font_size: 0.0,
page: page_num,
is_bold: false,
is_italic: false,
is_underline: false,
is_strikeout: false,
item_type: ItemType::Link(url),
mcid: None,
});
}
}
}
}
}
links
}
/// Extract URI from a link annotation
pub(crate) fn extract_link_uri(doc: &Document, annot_dict: &lopdf::Dictionary) -> Option<String> {
// Try to get the A (Action) dictionary
if let Ok(action_ref) = annot_dict.get(b"A") {
let action_dict = if let Ok(obj_ref) = action_ref.as_reference() {
doc.get_dictionary(obj_ref).ok()
} else {
action_ref.as_dict().ok()
};
if let Some(action_dict) = action_dict {
// Check for URI action
if let Ok(uri_obj) = action_dict.get(b"URI") {
if let Ok(uri_str) = uri_obj.as_str() {
return Some(String::from_utf8_lossy(uri_str).to_string());
}
}
}
}
// Try Dest (named destination) - less common for external links
// We'll skip this for now as it requires looking up named destinations
None
}
/// Extract form field values from AcroForm dictionary.
/// Returns TextItems positioned at each field's Rect so they flow into the markdown pipeline.
pub(crate) fn extract_form_fields(
doc: &Document,
page_map: &HashMap<ObjectId, u32>,
) -> Vec<TextItem> {
let mut items = Vec::new();
// Navigate: trailer -> /Root -> /AcroForm -> /Fields
let root = match doc.trailer.get(b"Root") {
Ok(root_ref) => match root_ref.as_reference() {
Ok(r) => match doc.get_dictionary(r) {
Ok(d) => d,
Err(_) => return items,
},
Err(_) => return items,
},
Err(_) => return items,
};
let acroform = match root.get(b"AcroForm") {
Ok(obj) => match resolve_dict(doc, obj) {
Some(d) => d,
None => return items,
},
Err(_) => return items,
};
let fields = match acroform.get(b"Fields") {
Ok(obj) => match resolve_array(doc, obj) {
Some(arr) => arr.clone(),
None => return items,
},
Err(_) => return items,
};
for field_obj in &fields {
if let Ok(field_ref) = field_obj.as_reference() {
walk_form_fields(doc, field_ref, None, "", page_map, &mut items);
}
}
items
}
/// Recursively walk the form field tree, extracting leaf field values.
pub(crate) fn walk_form_fields(
doc: &Document,
field_id: ObjectId,
parent_ft: Option<&[u8]>,
parent_name: &str,
page_map: &HashMap<ObjectId, u32>,
items: &mut Vec<TextItem>,
) {
let field_dict = match doc.get_dictionary(field_id) {
Ok(d) => d,
Err(_) => return,
};
// Build fully qualified field name
let local_name = field_dict
.get(b"T")
.ok()
.and_then(|o| o.as_str().ok())
.map(|s| String::from_utf8_lossy(s).to_string())
.unwrap_or_default();
let full_name = if parent_name.is_empty() {
local_name.clone()
} else if local_name.is_empty() {
parent_name.to_string()
} else {
format!("{}.{}", parent_name, local_name)
};
// Determine field type (may be inherited from parent)
let ft = field_dict
.get(b"FT")
.ok()
.and_then(|o| o.as_name().ok())
.or(parent_ft);
// Check for /Kids — if present, recurse into children
if let Ok(kids_obj) = field_dict.get(b"Kids") {
if let Some(kids) = resolve_array(doc, kids_obj) {
let kids = kids.clone();
for kid in &kids {
if let Ok(kid_ref) = kid.as_reference() {
walk_form_fields(doc, kid_ref, ft, &full_name, page_map, items);
}
}
return;
}
}
// Leaf field — extract value
let ft = match ft {
Some(ft) => ft,
None => return,
};
// Skip signature fields
if ft == b"Sig" {
return;
}
// Get field value
let value = match field_dict.get(b"V") {
Ok(v) => v,
Err(_) => return,
};
let value_str = match ft {
b"Tx" | b"Ch" => {
// Text or Choice field — value is a string or array of strings
match value {
Object::String(s, _) => {
let s = String::from_utf8_lossy(s).to_string();
if s.is_empty() {
return;
}
s
}
Object::Array(arr) => {
let parts: Vec<String> = arr
.iter()
.filter_map(|o| {
if let Object::String(s, _) = o {
Some(String::from_utf8_lossy(s).to_string())
} else {
None
}
})
.collect();
if parts.is_empty() {
return;
}
parts.join(", ")
}
_ => return,
}
}
b"Btn" => {
// Checkbox/radio — value is a name
match value.as_name() {
Ok(name) if name == b"Off" => return,
Ok(name) => {
let name_str = String::from_utf8_lossy(name).to_string();
if name_str == "Yes" || name_str == "1" {
"Yes".to_string()
} else {
name_str
}
}
Err(_) => return,
}
}
_ => return,
};
// Get Rect for positioning
let (x, y, width, height) = match field_dict.get(b"Rect") {
Ok(rect_obj) => match rect_obj.as_array() {
Ok(rect_array) if rect_array.len() >= 4 => {
let x1 = get_number(&rect_array[0]).unwrap_or(0.0);
let y1 = get_number(&rect_array[1]).unwrap_or(0.0);
let x2 = get_number(&rect_array[2]).unwrap_or(0.0);
let y2 = get_number(&rect_array[3]).unwrap_or(0.0);
(x1, y1.min(y2), (x2 - x1).abs(), (y2 - y1).abs())
}
_ => (0.0, 0.0, 0.0, 0.0),
},
Err(_) => (0.0, 0.0, 0.0, 0.0),
};
// Determine page number from /P reference
let page_num = field_dict
.get(b"P")
.ok()
.and_then(|o| o.as_reference().ok())
.and_then(|p| page_map.get(&p).copied())
.unwrap_or(1);
let text = if full_name.is_empty() {
value_str
} else {
format!("{}: {}", full_name, value_str)
};
items.push(TextItem {
text,
x,
y,
width,
height,
font: String::new(),
font_size: 0.0,
page: page_num,
is_bold: false,
is_italic: false,
is_underline: false,
is_strikeout: false,
item_type: ItemType::FormField,
mcid: None,
});
}