/src/qpdf/libqpdf/QPDFAcroFormDocumentHelper.cc
Line | Count | Source (jump to first uncovered line) |
1 | | #include <qpdf/QPDFAcroFormDocumentHelper.hh> |
2 | | |
3 | | #include <qpdf/Pl_Buffer.hh> |
4 | | #include <qpdf/QPDFPageDocumentHelper.hh> |
5 | | #include <qpdf/QTC.hh> |
6 | | #include <qpdf/QUtil.hh> |
7 | | #include <qpdf/ResourceFinder.hh> |
8 | | |
9 | | QPDFAcroFormDocumentHelper::Members::Members() : |
10 | | cache_valid(false) |
11 | 12.6k | { |
12 | 12.6k | } |
13 | | |
14 | | QPDFAcroFormDocumentHelper::QPDFAcroFormDocumentHelper(QPDF& qpdf) : |
15 | | QPDFDocumentHelper(qpdf), |
16 | | m(new Members()) |
17 | 12.6k | { |
18 | | // We have to analyze up front. Otherwise, when we are adding annotations and fields, we are in |
19 | | // a temporarily unstable configuration where some widget annotations are not reachable. |
20 | 12.6k | analyze(); |
21 | 12.6k | } |
22 | | |
23 | | void |
24 | | QPDFAcroFormDocumentHelper::invalidateCache() |
25 | 0 | { |
26 | 0 | m->cache_valid = false; |
27 | 0 | m->field_to_annotations.clear(); |
28 | 0 | m->annotation_to_field.clear(); |
29 | 0 | } |
30 | | |
31 | | bool |
32 | | QPDFAcroFormDocumentHelper::hasAcroForm() |
33 | 0 | { |
34 | 0 | return this->qpdf.getRoot().hasKey("/AcroForm"); |
35 | 0 | } |
36 | | |
37 | | QPDFObjectHandle |
38 | | QPDFAcroFormDocumentHelper::getOrCreateAcroForm() |
39 | 0 | { |
40 | 0 | auto acroform = this->qpdf.getRoot().getKey("/AcroForm"); |
41 | 0 | if (!acroform.isDictionary()) { |
42 | 0 | acroform = this->qpdf.getRoot().replaceKeyAndGetNew( |
43 | 0 | "/AcroForm", this->qpdf.makeIndirectObject(QPDFObjectHandle::newDictionary())); |
44 | 0 | } |
45 | 0 | return acroform; |
46 | 0 | } |
47 | | |
48 | | void |
49 | | QPDFAcroFormDocumentHelper::addFormField(QPDFFormFieldObjectHelper ff) |
50 | 0 | { |
51 | 0 | auto acroform = getOrCreateAcroForm(); |
52 | 0 | auto fields = acroform.getKey("/Fields"); |
53 | 0 | if (!fields.isArray()) { |
54 | 0 | fields = acroform.replaceKeyAndGetNew("/Fields", QPDFObjectHandle::newArray()); |
55 | 0 | } |
56 | 0 | fields.appendItem(ff.getObjectHandle()); |
57 | 0 | QPDFObjGen::set visited; |
58 | 0 | traverseField(ff.getObjectHandle(), QPDFObjectHandle::newNull(), 0, visited); |
59 | 0 | } |
60 | | |
61 | | void |
62 | | QPDFAcroFormDocumentHelper::addAndRenameFormFields(std::vector<QPDFObjectHandle> fields) |
63 | 0 | { |
64 | 0 | analyze(); |
65 | 0 | std::map<std::string, std::string> renames; |
66 | 0 | QPDFObjGen::set seen; |
67 | 0 | for (std::list<QPDFObjectHandle> queue{fields.begin(), fields.end()}; !queue.empty(); |
68 | 0 | queue.pop_front()) { |
69 | 0 | auto& obj = queue.front(); |
70 | 0 | if (seen.add(obj)) { |
71 | 0 | auto kids = obj.getKey("/Kids"); |
72 | 0 | if (kids.isArray()) { |
73 | 0 | for (auto const& kid: kids.aitems()) { |
74 | 0 | queue.push_back(kid); |
75 | 0 | } |
76 | 0 | } |
77 | |
|
78 | 0 | if (obj.hasKey("/T")) { |
79 | | // Find something we can append to the partial name that makes the fully qualified |
80 | | // name unique. When we find something, reuse the same suffix for all fields in this |
81 | | // group with the same name. We can only change the name of fields that have /T, and |
82 | | // this field's /T is always at the end of the fully qualified name, appending to /T |
83 | | // has the effect of appending the same thing to the fully qualified name. |
84 | 0 | std::string old_name = QPDFFormFieldObjectHelper(obj).getFullyQualifiedName(); |
85 | 0 | if (renames.count(old_name) == 0) { |
86 | 0 | std::string new_name = old_name; |
87 | 0 | int suffix = 0; |
88 | 0 | std::string append; |
89 | 0 | while (!getFieldsWithQualifiedName(new_name).empty()) { |
90 | 0 | ++suffix; |
91 | 0 | append = "+" + std::to_string(suffix); |
92 | 0 | new_name = old_name + append; |
93 | 0 | } |
94 | 0 | renames[old_name] = append; |
95 | 0 | } |
96 | 0 | std::string append = renames[old_name]; |
97 | 0 | if (!append.empty()) { |
98 | 0 | obj.replaceKey( |
99 | 0 | "/T", |
100 | 0 | QPDFObjectHandle::newUnicodeString( |
101 | 0 | obj.getKey("/T").getUTF8Value() + append)); |
102 | 0 | } |
103 | 0 | } |
104 | 0 | } |
105 | 0 | } |
106 | |
|
107 | 0 | for (auto const& i: fields) { |
108 | 0 | addFormField(i); |
109 | 0 | } |
110 | 0 | } |
111 | | |
112 | | void |
113 | | QPDFAcroFormDocumentHelper::removeFormFields(std::set<QPDFObjGen> const& to_remove) |
114 | 0 | { |
115 | 0 | auto acroform = this->qpdf.getRoot().getKey("/AcroForm"); |
116 | 0 | if (!acroform.isDictionary()) { |
117 | 0 | return; |
118 | 0 | } |
119 | 0 | auto fields = acroform.getKey("/Fields"); |
120 | 0 | if (!fields.isArray()) { |
121 | 0 | return; |
122 | 0 | } |
123 | | |
124 | 0 | for (auto const& og: to_remove) { |
125 | 0 | auto annotations = m->field_to_annotations.find(og); |
126 | 0 | if (annotations != m->field_to_annotations.end()) { |
127 | 0 | for (auto aoh: annotations->second) { |
128 | 0 | m->annotation_to_field.erase(aoh.getObjectHandle().getObjGen()); |
129 | 0 | } |
130 | 0 | m->field_to_annotations.erase(og); |
131 | 0 | } |
132 | 0 | auto name = m->field_to_name.find(og); |
133 | 0 | if (name != m->field_to_name.end()) { |
134 | 0 | m->name_to_fields[name->second].erase(og); |
135 | 0 | if (m->name_to_fields[name->second].empty()) { |
136 | 0 | m->name_to_fields.erase(name->second); |
137 | 0 | } |
138 | 0 | m->field_to_name.erase(og); |
139 | 0 | } |
140 | 0 | } |
141 | |
|
142 | 0 | int i = 0; |
143 | 0 | while (i < fields.getArrayNItems()) { |
144 | 0 | auto field = fields.getArrayItem(i); |
145 | 0 | if (to_remove.count(field.getObjGen())) { |
146 | 0 | fields.eraseItem(i); |
147 | 0 | } else { |
148 | 0 | ++i; |
149 | 0 | } |
150 | 0 | } |
151 | 0 | } |
152 | | |
153 | | void |
154 | | QPDFAcroFormDocumentHelper::setFormFieldName(QPDFFormFieldObjectHelper ff, std::string const& name) |
155 | 0 | { |
156 | 0 | ff.setFieldAttribute("/T", name); |
157 | 0 | QPDFObjGen::set visited; |
158 | 0 | auto ff_oh = ff.getObjectHandle(); |
159 | 0 | traverseField(ff_oh, ff_oh.getKey("/Parent"), 0, visited); |
160 | 0 | } |
161 | | |
162 | | std::vector<QPDFFormFieldObjectHelper> |
163 | | QPDFAcroFormDocumentHelper::getFormFields() |
164 | 0 | { |
165 | 0 | analyze(); |
166 | 0 | std::vector<QPDFFormFieldObjectHelper> result; |
167 | 0 | for (auto const& iter: m->field_to_annotations) { |
168 | 0 | result.emplace_back(this->qpdf.getObject(iter.first)); |
169 | 0 | } |
170 | 0 | return result; |
171 | 0 | } |
172 | | |
173 | | std::set<QPDFObjGen> |
174 | | QPDFAcroFormDocumentHelper::getFieldsWithQualifiedName(std::string const& name) |
175 | 0 | { |
176 | 0 | analyze(); |
177 | | // Keep from creating an empty entry |
178 | 0 | auto iter = m->name_to_fields.find(name); |
179 | 0 | if (iter != m->name_to_fields.end()) { |
180 | 0 | return iter->second; |
181 | 0 | } |
182 | 0 | return {}; |
183 | 0 | } |
184 | | |
185 | | std::vector<QPDFAnnotationObjectHelper> |
186 | | QPDFAcroFormDocumentHelper::getAnnotationsForField(QPDFFormFieldObjectHelper h) |
187 | 0 | { |
188 | 0 | analyze(); |
189 | 0 | std::vector<QPDFAnnotationObjectHelper> result; |
190 | 0 | QPDFObjGen og(h.getObjectHandle().getObjGen()); |
191 | 0 | if (m->field_to_annotations.count(og)) { |
192 | 0 | result = m->field_to_annotations[og]; |
193 | 0 | } |
194 | 0 | return result; |
195 | 0 | } |
196 | | |
197 | | std::vector<QPDFAnnotationObjectHelper> |
198 | | QPDFAcroFormDocumentHelper::getWidgetAnnotationsForPage(QPDFPageObjectHelper h) |
199 | 38.3k | { |
200 | 38.3k | return h.getAnnotations("/Widget"); |
201 | 38.3k | } |
202 | | |
203 | | std::vector<QPDFFormFieldObjectHelper> |
204 | | QPDFAcroFormDocumentHelper::getFormFieldsForPage(QPDFPageObjectHelper ph) |
205 | 0 | { |
206 | 0 | analyze(); |
207 | 0 | QPDFObjGen::set todo; |
208 | 0 | std::vector<QPDFFormFieldObjectHelper> result; |
209 | 0 | for (auto& annot: getWidgetAnnotationsForPage(ph)) { |
210 | 0 | auto field = getFieldForAnnotation(annot).getTopLevelField(); |
211 | 0 | if (todo.add(field) && field.getObjectHandle().isDictionary()) { |
212 | 0 | result.push_back(field); |
213 | 0 | } |
214 | 0 | } |
215 | 0 | return result; |
216 | 0 | } |
217 | | |
218 | | QPDFFormFieldObjectHelper |
219 | | QPDFAcroFormDocumentHelper::getFieldForAnnotation(QPDFAnnotationObjectHelper h) |
220 | 25.7k | { |
221 | 25.7k | QPDFObjectHandle oh = h.getObjectHandle(); |
222 | 25.7k | QPDFFormFieldObjectHelper result(QPDFObjectHandle::newNull()); |
223 | 25.7k | if (!oh.isDictionaryOfType("", "/Widget")) { |
224 | 0 | return result; |
225 | 0 | } |
226 | 25.7k | analyze(); |
227 | 25.7k | QPDFObjGen og(oh.getObjGen()); |
228 | 25.7k | if (m->annotation_to_field.count(og)) { |
229 | 23.1k | result = m->annotation_to_field[og]; |
230 | 23.1k | } |
231 | 25.7k | return result; |
232 | 25.7k | } |
233 | | |
234 | | void |
235 | | QPDFAcroFormDocumentHelper::analyze() |
236 | 38.3k | { |
237 | 38.3k | if (m->cache_valid) { |
238 | 25.7k | return; |
239 | 25.7k | } |
240 | 12.6k | m->cache_valid = true; |
241 | 12.6k | QPDFObjectHandle acroform = this->qpdf.getRoot().getKey("/AcroForm"); |
242 | 12.6k | if (!(acroform.isDictionary() && acroform.hasKey("/Fields"))) { |
243 | 8.42k | return; |
244 | 8.42k | } |
245 | 4.25k | QPDFObjectHandle fields = acroform.getKey("/Fields"); |
246 | 4.25k | if (!fields.isArray()) { |
247 | 121 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper fields not array"); |
248 | 121 | acroform.warnIfPossible("/Fields key of /AcroForm dictionary is not an array; ignoring"); |
249 | 121 | fields = QPDFObjectHandle::newArray(); |
250 | 121 | } |
251 | | |
252 | | // Traverse /AcroForm to find annotations and map them bidirectionally to fields. |
253 | | |
254 | 4.25k | QPDFObjGen::set visited; |
255 | 4.25k | int nfields = fields.getArrayNItems(); |
256 | 4.25k | QPDFObjectHandle null(QPDFObjectHandle::newNull()); |
257 | 47.9k | for (int i = 0; i < nfields; ++i) { |
258 | 43.6k | traverseField(fields.getArrayItem(i), null, 0, visited); |
259 | 43.6k | } |
260 | | |
261 | | // All Widget annotations should have been encountered by traversing /AcroForm, but in case any |
262 | | // weren't, find them by walking through pages, and treat any widget annotation that is not |
263 | | // associated with a field as its own field. This just ensures that requesting the field for any |
264 | | // annotation we find through a page's /Annots list will have some associated field. Note that |
265 | | // a file that contains this kind of error will probably not |
266 | | // actually work with most viewers. |
267 | | |
268 | 8.54k | for (auto const& ph: QPDFPageDocumentHelper(this->qpdf).getAllPages()) { |
269 | 28.8k | for (auto const& iter: getWidgetAnnotationsForPage(ph)) { |
270 | 28.8k | QPDFObjectHandle annot(iter.getObjectHandle()); |
271 | 28.8k | QPDFObjGen og(annot.getObjGen()); |
272 | 28.8k | if (m->annotation_to_field.count(og) == 0) { |
273 | 3.79k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper orphaned widget"); |
274 | | // This is not supposed to happen, but it's easy enough for us to handle this case. |
275 | | // Treat the annotation as its own field. This could allow qpdf to sensibly handle a |
276 | | // case such as a PDF creator adding a self-contained annotation (merged with the |
277 | | // field dictionary) to the page's /Annots array and forgetting to also put it in |
278 | | // /AcroForm. |
279 | 3.79k | annot.warnIfPossible("this widget annotation is not" |
280 | 3.79k | " reachable from /AcroForm in the document catalog"); |
281 | 3.79k | m->annotation_to_field[og] = QPDFFormFieldObjectHelper(annot); |
282 | 3.79k | m->field_to_annotations[og].emplace_back(annot); |
283 | 3.79k | } |
284 | 28.8k | } |
285 | 8.54k | } |
286 | 4.25k | } |
287 | | |
288 | | void |
289 | | QPDFAcroFormDocumentHelper::traverseField( |
290 | | QPDFObjectHandle field, QPDFObjectHandle parent, int depth, QPDFObjGen::set& visited) |
291 | 92.9k | { |
292 | 92.9k | if (depth > 100) { |
293 | | // Arbitrarily cut off recursion at a fixed depth to avoid specially crafted files that |
294 | | // could cause stack overflow. |
295 | 0 | return; |
296 | 0 | } |
297 | 92.9k | if (!field.isIndirect()) { |
298 | 49.9k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper direct field"); |
299 | 49.9k | field.warnIfPossible("encountered a direct object as a field or annotation while " |
300 | 49.9k | "traversing /AcroForm; ignoring field or annotation"); |
301 | 49.9k | return; |
302 | 49.9k | } |
303 | 42.9k | if (!field.isDictionary()) { |
304 | 7.81k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper non-dictionary field"); |
305 | 7.81k | field.warnIfPossible("encountered a non-dictionary as a field or annotation while" |
306 | 7.81k | " traversing /AcroForm; ignoring field or annotation"); |
307 | 7.81k | return; |
308 | 7.81k | } |
309 | 35.1k | QPDFObjGen og(field.getObjGen()); |
310 | 35.1k | if (!visited.add(og)) { |
311 | 1.21k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper loop"); |
312 | 1.21k | field.warnIfPossible("loop detected while traversing /AcroForm"); |
313 | 1.21k | return; |
314 | 1.21k | } |
315 | | |
316 | | // A dictionary encountered while traversing the /AcroForm field may be a form field, an |
317 | | // annotation, or the merger of the two. A field that has no fields below it is a terminal. If a |
318 | | // terminal field looks like an annotation, it is an annotation because annotation dictionary |
319 | | // fields can be merged with terminal field dictionaries. Otherwise, the annotation fields might |
320 | | // be there to be inherited by annotations below it. |
321 | | |
322 | 33.9k | bool is_annotation = false; |
323 | 33.9k | bool is_field = (0 == depth); |
324 | 33.9k | QPDFObjectHandle kids = field.getKey("/Kids"); |
325 | 33.9k | if (kids.isArray()) { |
326 | 5.34k | is_field = true; |
327 | 5.34k | int nkids = kids.getArrayNItems(); |
328 | 54.6k | for (int k = 0; k < nkids; ++k) { |
329 | 49.2k | traverseField(kids.getArrayItem(k), field, 1 + depth, visited); |
330 | 49.2k | } |
331 | 28.6k | } else { |
332 | 28.6k | if (field.hasKey("/Parent")) { |
333 | 11.1k | is_field = true; |
334 | 11.1k | } |
335 | 28.6k | if (field.hasKey("/Subtype") || field.hasKey("/Rect") || field.hasKey("/AP")) { |
336 | 24.8k | is_annotation = true; |
337 | 24.8k | } |
338 | 28.6k | } |
339 | | |
340 | 33.9k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper field found", (depth == 0) ? 0 : 1); |
341 | 33.9k | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper annotation found", (is_field ? 0 : 1)); |
342 | | |
343 | 33.9k | if (is_annotation) { |
344 | 24.8k | QPDFObjectHandle our_field = (is_field ? field : parent); |
345 | 24.8k | m->field_to_annotations[our_field.getObjGen()].emplace_back(field); |
346 | 24.8k | m->annotation_to_field[og] = QPDFFormFieldObjectHelper(our_field); |
347 | 24.8k | } |
348 | | |
349 | 33.9k | if (is_field && (field.hasKey("/T"))) { |
350 | 22.5k | QPDFFormFieldObjectHelper foh(field); |
351 | 22.5k | auto f_og = field.getObjGen(); |
352 | 22.5k | std::string name = foh.getFullyQualifiedName(); |
353 | 22.5k | auto old = m->field_to_name.find(f_og); |
354 | 22.5k | if (old != m->field_to_name.end()) { |
355 | | // We might be updating after a name change, so remove any old information |
356 | 0 | std::string old_name = old->second; |
357 | 0 | m->name_to_fields[old_name].erase(f_og); |
358 | 0 | } |
359 | 22.5k | m->field_to_name[f_og] = name; |
360 | 22.5k | m->name_to_fields[name].insert(f_og); |
361 | 22.5k | } |
362 | 33.9k | } |
363 | | |
364 | | bool |
365 | | QPDFAcroFormDocumentHelper::getNeedAppearances() |
366 | 53.1k | { |
367 | 53.1k | bool result = false; |
368 | 53.1k | QPDFObjectHandle acroform = this->qpdf.getRoot().getKey("/AcroForm"); |
369 | 53.1k | if (acroform.isDictionary() && acroform.getKey("/NeedAppearances").isBool()) { |
370 | 1.97k | result = acroform.getKey("/NeedAppearances").getBoolValue(); |
371 | 1.97k | } |
372 | 53.1k | return result; |
373 | 53.1k | } |
374 | | |
375 | | void |
376 | | QPDFAcroFormDocumentHelper::setNeedAppearances(bool val) |
377 | 1.77k | { |
378 | 1.77k | QPDFObjectHandle acroform = this->qpdf.getRoot().getKey("/AcroForm"); |
379 | 1.77k | if (!acroform.isDictionary()) { |
380 | 0 | this->qpdf.getRoot().warnIfPossible( |
381 | 0 | "ignoring call to QPDFAcroFormDocumentHelper::setNeedAppearances" |
382 | 0 | " on a file that lacks an /AcroForm dictionary"); |
383 | 0 | return; |
384 | 0 | } |
385 | 1.77k | if (val) { |
386 | 0 | acroform.replaceKey("/NeedAppearances", QPDFObjectHandle::newBool(true)); |
387 | 1.77k | } else { |
388 | 1.77k | acroform.removeKey("/NeedAppearances"); |
389 | 1.77k | } |
390 | 1.77k | } |
391 | | |
392 | | void |
393 | | QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded() |
394 | 6.41k | { |
395 | 6.41k | if (!getNeedAppearances()) { |
396 | 4.44k | return; |
397 | 4.44k | } |
398 | | |
399 | 2.66k | for (auto const& page: QPDFPageDocumentHelper(this->qpdf).getAllPages()) { |
400 | 14.4k | for (auto& aoh: getWidgetAnnotationsForPage(page)) { |
401 | 14.4k | QPDFFormFieldObjectHelper ffh = getFieldForAnnotation(aoh); |
402 | 14.4k | if (ffh.getFieldType() == "/Btn") { |
403 | | // Rather than generating appearances for button fields, rely on what's already |
404 | | // there. Just make sure /AS is consistent with /V, which we can do by resetting the |
405 | | // value of the field back to itself. This code is referenced in a comment in |
406 | | // QPDFFormFieldObjectHelper::generateAppearance. |
407 | 6.67k | if (ffh.isRadioButton() || ffh.isCheckbox()) { |
408 | 6.64k | ffh.setV(ffh.getValue()); |
409 | 6.64k | } |
410 | 7.81k | } else { |
411 | 7.81k | ffh.generateAppearance(aoh); |
412 | 7.81k | } |
413 | 14.4k | } |
414 | 2.66k | } |
415 | 1.97k | setNeedAppearances(false); |
416 | 1.97k | } |
417 | | |
418 | | void |
419 | | QPDFAcroFormDocumentHelper::disableDigitalSignatures() |
420 | 0 | { |
421 | 0 | qpdf.removeSecurityRestrictions(); |
422 | 0 | std::set<QPDFObjGen> to_remove; |
423 | 0 | auto fields = getFormFields(); |
424 | 0 | for (auto& f: fields) { |
425 | 0 | auto ft = f.getFieldType(); |
426 | 0 | if (ft == "/Sig") { |
427 | 0 | auto oh = f.getObjectHandle(); |
428 | 0 | to_remove.insert(oh.getObjGen()); |
429 | | // Make this no longer a form field. If it's also an annotation, the annotation will |
430 | | // survive. If it's only a field and is no longer referenced, it will disappear. |
431 | 0 | oh.removeKey("/FT"); |
432 | | // Remove fields that are specific to signature fields. |
433 | 0 | oh.removeKey("/V"); |
434 | 0 | oh.removeKey("/SV"); |
435 | 0 | oh.removeKey("/Lock"); |
436 | 0 | } |
437 | 0 | } |
438 | 0 | removeFormFields(to_remove); |
439 | 0 | } |
440 | | |
441 | | void |
442 | | QPDFAcroFormDocumentHelper::adjustInheritedFields( |
443 | | QPDFObjectHandle obj, |
444 | | bool override_da, |
445 | | std::string const& from_default_da, |
446 | | bool override_q, |
447 | | int from_default_q) |
448 | 0 | { |
449 | | // Override /Q or /DA if needed. If this object has a field type, directly or inherited, it is a |
450 | | // field and not just an annotation. In that case, we need to override if we are getting a value |
451 | | // from the document that is different from the value we would have gotten from the old |
452 | | // document. We must take care not to override an explicit value. It's possible that /FT may be |
453 | | // inherited by lower fields that may explicitly set /DA or /Q or that this is a field whose |
454 | | // type does not require /DA or /Q and we may be put a value on the field that is unused. This |
455 | | // is harmless, so it's not worth trying to work around. |
456 | |
|
457 | 0 | auto has_explicit = [](QPDFFormFieldObjectHelper& field, std::string const& key) { |
458 | 0 | if (field.getObjectHandle().hasKey(key)) { |
459 | 0 | return true; |
460 | 0 | } |
461 | 0 | auto oh = field.getInheritableFieldValue(key); |
462 | 0 | if (!oh.isNull()) { |
463 | 0 | return true; |
464 | 0 | } |
465 | 0 | return false; |
466 | 0 | }; |
467 | |
|
468 | 0 | if (override_da || override_q) { |
469 | 0 | QPDFFormFieldObjectHelper cur_field(obj); |
470 | 0 | if (override_da && (!has_explicit(cur_field, "/DA"))) { |
471 | 0 | std::string da = cur_field.getDefaultAppearance(); |
472 | 0 | if (da != from_default_da) { |
473 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper override da"); |
474 | 0 | obj.replaceKey("/DA", QPDFObjectHandle::newUnicodeString(from_default_da)); |
475 | 0 | } |
476 | 0 | } |
477 | 0 | if (override_q && (!has_explicit(cur_field, "/Q"))) { |
478 | 0 | int q = cur_field.getQuadding(); |
479 | 0 | if (q != from_default_q) { |
480 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper override q"); |
481 | 0 | obj.replaceKey("/Q", QPDFObjectHandle::newInteger(from_default_q)); |
482 | 0 | } |
483 | 0 | } |
484 | 0 | } |
485 | 0 | } |
486 | | |
487 | | namespace |
488 | | { |
489 | | class ResourceReplacer: public QPDFObjectHandle::TokenFilter |
490 | | { |
491 | | public: |
492 | | ResourceReplacer( |
493 | | std::map<std::string, std::map<std::string, std::string>> const& dr_map, |
494 | | std::map<std::string, std::map<std::string, std::set<size_t>>> const& rnames); |
495 | 0 | ~ResourceReplacer() override = default; |
496 | | void handleToken(QPDFTokenizer::Token const&) override; |
497 | | |
498 | | private: |
499 | | size_t offset{0}; |
500 | | std::map<std::string, std::map<size_t, std::string>> to_replace; |
501 | | }; |
502 | | } // namespace |
503 | | |
504 | | ResourceReplacer::ResourceReplacer( |
505 | | std::map<std::string, std::map<std::string, std::string>> const& dr_map, |
506 | | std::map<std::string, std::map<std::string, std::set<size_t>>> const& rnames) |
507 | 0 | { |
508 | | // We have: |
509 | | // * dr_map[resource_type][key] == new_key |
510 | | // * rnames[resource_type][key] == set of offsets |
511 | | // |
512 | | // We want: |
513 | | // * to_replace[key][offset] = new_key |
514 | |
|
515 | 0 | for (auto const& rn_iter: rnames) { |
516 | 0 | std::string const& rtype = rn_iter.first; |
517 | 0 | auto dr_map_rtype = dr_map.find(rtype); |
518 | 0 | if (dr_map_rtype == dr_map.end()) { |
519 | 0 | continue; |
520 | 0 | } |
521 | 0 | auto const& key_offsets = rn_iter.second; |
522 | 0 | for (auto const& ko_iter: key_offsets) { |
523 | 0 | std::string const& old_key = ko_iter.first; |
524 | 0 | auto dr_map_rtype_old = dr_map_rtype->second.find(old_key); |
525 | 0 | if (dr_map_rtype_old == dr_map_rtype->second.end()) { |
526 | 0 | continue; |
527 | 0 | } |
528 | 0 | auto const& offsets = ko_iter.second; |
529 | 0 | for (auto const& o_iter: offsets) { |
530 | 0 | to_replace[old_key][o_iter] = dr_map_rtype_old->second; |
531 | 0 | } |
532 | 0 | } |
533 | 0 | } |
534 | 0 | } |
535 | | |
536 | | void |
537 | | ResourceReplacer::handleToken(QPDFTokenizer::Token const& token) |
538 | 0 | { |
539 | 0 | bool wrote = false; |
540 | 0 | if (token.getType() == QPDFTokenizer::tt_name) { |
541 | 0 | std::string name = QPDFObjectHandle::newName(token.getValue()).getName(); |
542 | 0 | if (to_replace.count(name) && to_replace[name].count(offset)) { |
543 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper replaced DA token"); |
544 | 0 | write(to_replace[name][offset]); |
545 | 0 | wrote = true; |
546 | 0 | } |
547 | 0 | } |
548 | 0 | this->offset += token.getRawValue().length(); |
549 | 0 | if (!wrote) { |
550 | 0 | writeToken(token); |
551 | 0 | } |
552 | 0 | } |
553 | | |
554 | | void |
555 | | QPDFAcroFormDocumentHelper::adjustDefaultAppearances( |
556 | | QPDFObjectHandle obj, std::map<std::string, std::map<std::string, std::string>> const& dr_map) |
557 | 0 | { |
558 | | // This method is called on a field that has been copied from another file but whose /DA still |
559 | | // refers to resources in the original file's /DR. |
560 | | |
561 | | // When appearance streams are generated for variable text fields (see ISO 32000 PDF spec |
562 | | // section 12.7.3.3), the field's /DA is used to generate content of the appearance stream. /DA |
563 | | // contains references to resources that may be resolved in the document's /DR dictionary, which |
564 | | // appears in the document's /AcroForm dictionary. For fields that we copied from other |
565 | | // documents, we need to ensure that resources are mapped correctly in the case of conflicting |
566 | | // names. For example, if a.pdf's /DR has /F1 pointing to one font and b.pdf's /DR also has /F1 |
567 | | // but it points elsewhere, we need to make sure appearance streams of fields copied from b.pdf |
568 | | // into a.pdf use whatever font /F1 meant in b.pdf, not whatever it means in a.pdf. This method |
569 | | // takes care of that. It is only called on fields copied from foreign files. |
570 | | |
571 | | // A few notes: |
572 | | // |
573 | | // * If the from document's /DR and the current document's /DR have conflicting keys, we have |
574 | | // already resolved the conflicts before calling this method. The dr_map parameter contains |
575 | | // the mapping from old keys to new keys. |
576 | | // |
577 | | // * /DA may be inherited from the document's /AcroForm dictionary. By the time this method has |
578 | | // been called, we have already copied any document-level values into the fields to avoid |
579 | | // having them inherit from the new document. This was done in adjustInheritedFields. |
580 | |
|
581 | 0 | auto DA = obj.getKey("/DA"); |
582 | 0 | if (!DA.isString()) { |
583 | 0 | return; |
584 | 0 | } |
585 | | |
586 | | // Find names in /DA. /DA is a string that contains content stream-like code, so we create a |
587 | | // stream out of the string and then filter it. We don't attach the stream to anything, so it |
588 | | // will get discarded. |
589 | 0 | ResourceFinder rf; |
590 | 0 | auto da_stream = QPDFObjectHandle::newStream(&this->qpdf, DA.getUTF8Value()); |
591 | 0 | try { |
592 | 0 | auto nwarnings = this->qpdf.numWarnings(); |
593 | 0 | da_stream.parseAsContents(&rf); |
594 | 0 | if (this->qpdf.numWarnings() > nwarnings) { |
595 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper /DA parse error"); |
596 | 0 | } |
597 | 0 | } catch (std::exception& e) { |
598 | | // No way to reproduce in test suite right now since error conditions are converted to |
599 | | // warnings. |
600 | 0 | obj.warnIfPossible( |
601 | 0 | std::string("Unable to parse /DA: ") + e.what() + |
602 | 0 | "; this form field may not update properly"); |
603 | 0 | return; |
604 | 0 | } |
605 | | |
606 | | // Regenerate /DA by filtering its tokens. |
607 | 0 | ResourceReplacer rr(dr_map, rf.getNamesByResourceType()); |
608 | 0 | Pl_Buffer buf_pl("filtered DA"); |
609 | 0 | da_stream.filterAsContents(&rr, &buf_pl); |
610 | 0 | std::string new_da = buf_pl.getString(); |
611 | 0 | obj.replaceKey("/DA", QPDFObjectHandle::newString(new_da)); |
612 | 0 | } |
613 | | |
614 | | void |
615 | | QPDFAcroFormDocumentHelper::adjustAppearanceStream( |
616 | | QPDFObjectHandle stream, std::map<std::string, std::map<std::string, std::string>> dr_map) |
617 | 0 | { |
618 | | // We don't have to modify appearance streams or their resource dictionaries for them to display |
619 | | // properly, but we need to do so to make them save to regenerate. Suppose an appearance stream |
620 | | // as a font /F1 that is different from /F1 in /DR, and that when we copy the field, /F1 is |
621 | | // remapped to /F1_1. When the field is regenerated, /F1_1 won't appear in the stream's resource |
622 | | // dictionary, so the regenerated appearance stream will revert to the /F1_1 in /DR. If we |
623 | | // adjust existing appearance streams, we are protected from this problem. |
624 | |
|
625 | 0 | auto dict = stream.getDict(); |
626 | 0 | auto resources = dict.getKey("/Resources"); |
627 | | |
628 | | // Make sure this stream has its own private resource dictionary. |
629 | 0 | bool was_indirect = resources.isIndirect(); |
630 | 0 | resources = resources.shallowCopy(); |
631 | 0 | if (was_indirect) { |
632 | 0 | resources = this->qpdf.makeIndirectObject(resources); |
633 | 0 | } |
634 | 0 | dict.replaceKey("/Resources", resources); |
635 | | // Create a dictionary with top-level keys so we can use mergeResources to force them to be |
636 | | // unshared. We will also use this to resolve conflicts that may already be in the resource |
637 | | // dictionary. |
638 | 0 | auto merge_with = QPDFObjectHandle::newDictionary(); |
639 | 0 | for (auto const& top_key: dr_map) { |
640 | 0 | merge_with.replaceKey(top_key.first, QPDFObjectHandle::newDictionary()); |
641 | 0 | } |
642 | 0 | resources.mergeResources(merge_with); |
643 | | // Rename any keys in the resource dictionary that we remapped. |
644 | 0 | for (auto const& i1: dr_map) { |
645 | 0 | std::string const& top_key = i1.first; |
646 | 0 | auto subdict = resources.getKey(top_key); |
647 | 0 | if (!subdict.isDictionary()) { |
648 | 0 | continue; |
649 | 0 | } |
650 | 0 | for (auto const& i2: i1.second) { |
651 | 0 | std::string const& old_key = i2.first; |
652 | 0 | std::string const& new_key = i2.second; |
653 | 0 | auto existing_new = subdict.getKey(new_key); |
654 | 0 | if (!existing_new.isNull()) { |
655 | | // The resource dictionary already has a key in it matching what we remapped an old |
656 | | // key to, so we'll have to move it out of the way. Stick it in merge_with, which we |
657 | | // will re-merge with the dictionary when we're done. We know merge_with already has |
658 | | // dictionaries for all the top keys. |
659 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper ap conflict"); |
660 | 0 | merge_with.getKey(top_key).replaceKey(new_key, existing_new); |
661 | 0 | } |
662 | 0 | auto existing_old = subdict.getKey(old_key); |
663 | 0 | if (!existing_old.isNull()) { |
664 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper ap rename"); |
665 | 0 | subdict.replaceKey(new_key, existing_old); |
666 | 0 | subdict.removeKey(old_key); |
667 | 0 | } |
668 | 0 | } |
669 | 0 | } |
670 | | // Deal with any any conflicts by re-merging with merge_with and updating our local copy of |
671 | | // dr_map, which we will use to modify the stream contents. |
672 | 0 | resources.mergeResources(merge_with, &dr_map); |
673 | | // Remove empty subdictionaries |
674 | 0 | for (auto iter: resources.ditems()) { |
675 | 0 | if (iter.second.isDictionary() && (iter.second.getKeys().size() == 0)) { |
676 | 0 | resources.removeKey(iter.first); |
677 | 0 | } |
678 | 0 | } |
679 | | |
680 | | // Now attach a token filter to replace the actual resources. |
681 | 0 | ResourceFinder rf; |
682 | 0 | try { |
683 | 0 | auto nwarnings = this->qpdf.numWarnings(); |
684 | 0 | stream.parseAsContents(&rf); |
685 | 0 | if (this->qpdf.numWarnings() > nwarnings) { |
686 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper AP parse error"); |
687 | 0 | } |
688 | 0 | auto rr = new ResourceReplacer(dr_map, rf.getNamesByResourceType()); |
689 | 0 | auto tf = std::shared_ptr<QPDFObjectHandle::TokenFilter>(rr); |
690 | 0 | stream.addTokenFilter(tf); |
691 | 0 | } catch (std::exception& e) { |
692 | | // No way to reproduce in test suite right now since error conditions are converted to |
693 | | // warnings. |
694 | 0 | stream.warnIfPossible(std::string("Unable to parse appearance stream: ") + e.what()); |
695 | 0 | } |
696 | 0 | } |
697 | | |
698 | | void |
699 | | QPDFAcroFormDocumentHelper::transformAnnotations( |
700 | | QPDFObjectHandle old_annots, |
701 | | std::vector<QPDFObjectHandle>& new_annots, |
702 | | std::vector<QPDFObjectHandle>& new_fields, |
703 | | std::set<QPDFObjGen>& old_fields, |
704 | | QPDFMatrix const& cm, |
705 | | QPDF* from_qpdf, |
706 | | QPDFAcroFormDocumentHelper* from_afdh) |
707 | 0 | { |
708 | 0 | std::shared_ptr<QPDFAcroFormDocumentHelper> afdhph; |
709 | 0 | if (!from_qpdf) { |
710 | | // Assume these are from the same QPDF. |
711 | 0 | from_qpdf = &this->qpdf; |
712 | 0 | from_afdh = this; |
713 | 0 | } else if ((from_qpdf != &this->qpdf) && (!from_afdh)) { |
714 | 0 | afdhph = std::make_shared<QPDFAcroFormDocumentHelper>(*from_qpdf); |
715 | 0 | from_afdh = afdhph.get(); |
716 | 0 | } |
717 | 0 | bool foreign = (from_qpdf != &this->qpdf); |
718 | | |
719 | | // It's possible that we will transform annotations that don't include any form fields. This |
720 | | // code takes care not to muck around with /AcroForm unless we have to. |
721 | |
|
722 | 0 | QPDFObjectHandle acroform = this->qpdf.getRoot().getKey("/AcroForm"); |
723 | 0 | QPDFObjectHandle from_acroform = from_qpdf->getRoot().getKey("/AcroForm"); |
724 | | |
725 | | // /DA and /Q may be inherited from the document-level /AcroForm dictionary. If we are copying a |
726 | | // foreign stream and the stream is getting one of these values from its document's /AcroForm, |
727 | | // we will need to copy the value explicitly so that it doesn't start getting its default from |
728 | | // the destination document. |
729 | 0 | bool override_da = false; |
730 | 0 | bool override_q = false; |
731 | 0 | std::string from_default_da; |
732 | 0 | int from_default_q = 0; |
733 | | // If we copy any form fields, we will need to merge the source document's /DR into this |
734 | | // document's /DR. |
735 | 0 | QPDFObjectHandle from_dr = QPDFObjectHandle::newNull(); |
736 | 0 | if (foreign) { |
737 | 0 | std::string default_da; |
738 | 0 | int default_q = 0; |
739 | 0 | if (acroform.isDictionary()) { |
740 | 0 | if (acroform.getKey("/DA").isString()) { |
741 | 0 | default_da = acroform.getKey("/DA").getUTF8Value(); |
742 | 0 | } |
743 | 0 | if (acroform.getKey("/Q").isInteger()) { |
744 | 0 | default_q = acroform.getKey("/Q").getIntValueAsInt(); |
745 | 0 | } |
746 | 0 | } |
747 | 0 | if (from_acroform.isDictionary()) { |
748 | 0 | if (from_acroform.getKey("/DR").isDictionary()) { |
749 | 0 | from_dr = from_acroform.getKey("/DR"); |
750 | 0 | if (!from_dr.isIndirect()) { |
751 | 0 | from_dr = from_qpdf->makeIndirectObject(from_dr); |
752 | 0 | } |
753 | 0 | from_dr = this->qpdf.copyForeignObject(from_dr); |
754 | 0 | } |
755 | 0 | if (from_acroform.getKey("/DA").isString()) { |
756 | 0 | from_default_da = from_acroform.getKey("/DA").getUTF8Value(); |
757 | 0 | } |
758 | 0 | if (from_acroform.getKey("/Q").isInteger()) { |
759 | 0 | from_default_q = from_acroform.getKey("/Q").getIntValueAsInt(); |
760 | 0 | } |
761 | 0 | } |
762 | 0 | if (from_default_da != default_da) { |
763 | 0 | override_da = true; |
764 | 0 | } |
765 | 0 | if (from_default_q != default_q) { |
766 | 0 | override_q = true; |
767 | 0 | } |
768 | 0 | } |
769 | | |
770 | | // If we have to merge /DR, we will need a mapping of conflicting keys for rewriting /DA. Set |
771 | | // this up for lazy initialization in case we encounter any form fields. |
772 | 0 | std::map<std::string, std::map<std::string, std::string>> dr_map; |
773 | 0 | bool initialized_dr_map = false; |
774 | 0 | QPDFObjectHandle dr = QPDFObjectHandle::newNull(); |
775 | 0 | auto init_dr_map = [&]() { |
776 | 0 | if (!initialized_dr_map) { |
777 | 0 | initialized_dr_map = true; |
778 | | // Ensure that we have a /DR that is an indirect |
779 | | // dictionary object. |
780 | 0 | if (!acroform.isDictionary()) { |
781 | 0 | acroform = getOrCreateAcroForm(); |
782 | 0 | } |
783 | 0 | dr = acroform.getKey("/DR"); |
784 | 0 | if (!dr.isDictionary()) { |
785 | 0 | dr = QPDFObjectHandle::newDictionary(); |
786 | 0 | } |
787 | 0 | dr.makeResourcesIndirect(this->qpdf); |
788 | 0 | if (!dr.isIndirect()) { |
789 | 0 | dr = acroform.replaceKeyAndGetNew("/DR", this->qpdf.makeIndirectObject(dr)); |
790 | 0 | } |
791 | | // Merge the other document's /DR, creating a conflict map. mergeResources checks to |
792 | | // make sure both objects are dictionaries. By this point, if this is foreign, from_dr |
793 | | // has been copied, so we use the target qpdf as the owning qpdf. |
794 | 0 | from_dr.makeResourcesIndirect(this->qpdf); |
795 | 0 | dr.mergeResources(from_dr, &dr_map); |
796 | |
|
797 | 0 | if (from_afdh->getNeedAppearances()) { |
798 | 0 | setNeedAppearances(true); |
799 | 0 | } |
800 | 0 | } |
801 | 0 | }; |
802 | | |
803 | | // This helper prevents us from copying the same object multiple times. |
804 | 0 | std::map<QPDFObjGen, QPDFObjectHandle> orig_to_copy; |
805 | 0 | auto maybe_copy_object = [&](QPDFObjectHandle& to_copy) { |
806 | 0 | auto og = to_copy.getObjGen(); |
807 | 0 | if (orig_to_copy.count(og)) { |
808 | 0 | to_copy = orig_to_copy[og]; |
809 | 0 | return false; |
810 | 0 | } else { |
811 | 0 | to_copy = this->qpdf.makeIndirectObject(to_copy.shallowCopy()); |
812 | 0 | orig_to_copy[og] = to_copy; |
813 | 0 | return true; |
814 | 0 | } |
815 | 0 | }; |
816 | | |
817 | | // Now do the actual copies. |
818 | |
|
819 | 0 | QPDFObjGen::set added_new_fields; |
820 | 0 | for (auto annot: old_annots.aitems()) { |
821 | 0 | if (annot.isStream()) { |
822 | 0 | annot.warnIfPossible("ignoring annotation that's a stream"); |
823 | 0 | continue; |
824 | 0 | } |
825 | | |
826 | | // Make copies of annotations and fields down to the appearance streams, preserving all |
827 | | // internal referential integrity. When the incoming annotations are from a different file, |
828 | | // we first copy them locally. Then, whether local or foreign, we copy them again so that if |
829 | | // we bring the same annotation in multiple times (e.g. overlaying a foreign page onto |
830 | | // multiple local pages or a local page onto multiple other local pages), we don't create |
831 | | // annotations that are referenced in more than one place. If we did that, the effect of |
832 | | // applying transformations would be cumulative, which is definitely not what we want. |
833 | | // Besides, annotations and fields are not intended to be referenced in multiple places. |
834 | | |
835 | | // Determine if this annotation is attached to a form field. If so, the annotation may be |
836 | | // the same object as the form field, or the form field may have the annotation as a kid. In |
837 | | // either case, we have to walk up the field structure to find the top-level field. Within |
838 | | // one iteration through a set of annotations, we don't want to copy the same item more than |
839 | | // once. For example, suppose we have field A with kids B, C, and D, each of which has |
840 | | // annotations BA, CA, and DA. When we get to BA, we will find that BA is a kid of B which |
841 | | // is under A. When we do a copyForeignObject of A, it will also copy everything else |
842 | | // because of the indirect references. When we clone BA, we will want to clone A and then |
843 | | // update A's clone's kid to point B's clone and B's clone's parent to point to A's clone. |
844 | | // The same thing holds for annotations. Next, when we get to CA, we will again discover |
845 | | // that A is the top, but we don't want to re-copy A. We want CA's clone to be linked to the |
846 | | // same clone as BA's. Failure to do this will break up things like radio button groups, |
847 | | // which all have to kids of the same parent. |
848 | | |
849 | 0 | auto ffield = from_afdh->getFieldForAnnotation(annot); |
850 | 0 | auto ffield_oh = ffield.getObjectHandle(); |
851 | 0 | QPDFObjectHandle top_field; |
852 | 0 | bool have_field = false; |
853 | 0 | bool have_parent = false; |
854 | 0 | if (ffield_oh.isStream()) { |
855 | 0 | ffield_oh.warnIfPossible("ignoring form field that's a stream"); |
856 | 0 | } else if ((!ffield_oh.isNull()) && (!ffield_oh.isIndirect())) { |
857 | 0 | ffield_oh.warnIfPossible("ignoring form field not indirect"); |
858 | 0 | } else if (!ffield_oh.isNull()) { |
859 | | // A field and its associated annotation can be the same object. This matters because we |
860 | | // don't want to clone the annotation and field separately in this case. |
861 | 0 | have_field = true; |
862 | | // Find the top-level field. It may be the field itself. |
863 | 0 | top_field = ffield.getTopLevelField(&have_parent).getObjectHandle(); |
864 | 0 | if (foreign) { |
865 | | // copyForeignObject returns the same value if called multiple times with the same |
866 | | // field. Create/retrieve the local copy of the original field. This pulls over |
867 | | // everything the field references including annotations and appearance streams, but |
868 | | // it's harmless to call copyForeignObject on them too. They will already be copied, |
869 | | // so we'll get the right object back. |
870 | | |
871 | | // top_field and ffield_oh are known to be indirect. |
872 | 0 | top_field = this->qpdf.copyForeignObject(top_field); |
873 | 0 | ffield_oh = this->qpdf.copyForeignObject(ffield_oh); |
874 | 0 | } else { |
875 | | // We don't need to add top_field to old_fields if it's foreign because the new copy |
876 | | // of the foreign field won't be referenced anywhere. It's just the starting point |
877 | | // for us to make an additional local copy of. |
878 | 0 | old_fields.insert(top_field.getObjGen()); |
879 | 0 | } |
880 | | |
881 | | // Traverse the field, copying kids, and preserving integrity. |
882 | 0 | std::list<QPDFObjectHandle> queue; |
883 | 0 | QPDFObjGen::set seen; |
884 | 0 | if (maybe_copy_object(top_field)) { |
885 | 0 | queue.push_back(top_field); |
886 | 0 | } |
887 | 0 | for (; !queue.empty(); queue.pop_front()) { |
888 | 0 | auto& obj = queue.front(); |
889 | 0 | if (seen.add(obj)) { |
890 | 0 | auto parent = obj.getKey("/Parent"); |
891 | 0 | if (parent.isIndirect()) { |
892 | 0 | auto parent_og = parent.getObjGen(); |
893 | 0 | if (orig_to_copy.count(parent_og)) { |
894 | 0 | obj.replaceKey("/Parent", orig_to_copy[parent_og]); |
895 | 0 | } else { |
896 | 0 | parent.warnIfPossible( |
897 | 0 | "while traversing field " + obj.getObjGen().unparse(',') + |
898 | 0 | ", found parent (" + parent_og.unparse(',') + |
899 | 0 | ") that had not been seen, indicating likely invalid field " |
900 | 0 | "structure"); |
901 | 0 | } |
902 | 0 | } |
903 | 0 | auto kids = obj.getKey("/Kids"); |
904 | 0 | if (kids.isArray()) { |
905 | 0 | for (int i = 0; i < kids.getArrayNItems(); ++i) { |
906 | 0 | auto kid = kids.getArrayItem(i); |
907 | 0 | if (maybe_copy_object(kid)) { |
908 | 0 | kids.setArrayItem(i, kid); |
909 | 0 | queue.push_back(kid); |
910 | 0 | } |
911 | 0 | } |
912 | 0 | } |
913 | |
|
914 | 0 | if (override_da || override_q) { |
915 | 0 | adjustInheritedFields( |
916 | 0 | obj, override_da, from_default_da, override_q, from_default_q); |
917 | 0 | } |
918 | 0 | if (foreign) { |
919 | | // Lazily initialize our /DR and the conflict map. |
920 | 0 | init_dr_map(); |
921 | | // The spec doesn't say anything about /DR on the field, but lots of writers |
922 | | // put one there, and it is frequently the same as the document-level /DR. |
923 | | // To avoid having the field's /DR point to information that we are not |
924 | | // maintaining, just reset it to that if it exists. Empirical evidence |
925 | | // suggests that many readers, including Acrobat, Adobe Acrobat Reader, |
926 | | // chrome, firefox, the mac Preview application, and several of the free |
927 | | // readers on Linux all ignore /DR at the field level. |
928 | 0 | if (obj.hasKey("/DR")) { |
929 | 0 | obj.replaceKey("/DR", dr); |
930 | 0 | } |
931 | 0 | } |
932 | 0 | if (foreign && obj.getKey("/DA").isString() && (!dr_map.empty())) { |
933 | 0 | adjustDefaultAppearances(obj, dr_map); |
934 | 0 | } |
935 | 0 | } |
936 | 0 | } |
937 | | |
938 | | // Now switch to copies. We already switched for top_field |
939 | 0 | maybe_copy_object(ffield_oh); |
940 | 0 | ffield = QPDFFormFieldObjectHelper(ffield_oh); |
941 | 0 | } |
942 | |
|
943 | 0 | QTC::TC( |
944 | 0 | "qpdf", |
945 | 0 | "QPDFAcroFormDocumentHelper copy annotation", |
946 | 0 | (have_field ? 1 : 0) | (foreign ? 2 : 0)); |
947 | 0 | if (have_field) { |
948 | 0 | QTC::TC( |
949 | 0 | "qpdf", |
950 | 0 | "QPDFAcroFormDocumentHelper field with parent", |
951 | 0 | (have_parent ? 1 : 0) | (foreign ? 2 : 0)); |
952 | 0 | } |
953 | 0 | if (foreign) { |
954 | 0 | if (!annot.isIndirect()) { |
955 | 0 | annot = from_qpdf->makeIndirectObject(annot); |
956 | 0 | } |
957 | 0 | annot = this->qpdf.copyForeignObject(annot); |
958 | 0 | } |
959 | 0 | maybe_copy_object(annot); |
960 | | |
961 | | // Now we have copies, so we can safely mutate. |
962 | 0 | if (have_field && added_new_fields.add(top_field)) { |
963 | 0 | new_fields.push_back(top_field); |
964 | 0 | } |
965 | 0 | new_annots.push_back(annot); |
966 | | |
967 | | // Identify and copy any appearance streams |
968 | |
|
969 | 0 | auto ah = QPDFAnnotationObjectHelper(annot); |
970 | 0 | auto apdict = ah.getAppearanceDictionary(); |
971 | 0 | std::vector<QPDFObjectHandle> streams; |
972 | 0 | auto replace_stream = [](auto& dict, auto& key, auto& old) { |
973 | 0 | return dict.replaceKeyAndGetNew(key, old.copyStream()); |
974 | 0 | }; |
975 | 0 | if (apdict.isDictionary()) { |
976 | 0 | for (auto& ap: apdict.ditems()) { |
977 | 0 | if (ap.second.isStream()) { |
978 | 0 | streams.push_back(replace_stream(apdict, ap.first, ap.second)); |
979 | 0 | } else if (ap.second.isDictionary()) { |
980 | 0 | for (auto& ap2: ap.second.ditems()) { |
981 | 0 | if (ap2.second.isStream()) { |
982 | 0 | streams.push_back( |
983 | | // line-break |
984 | 0 | replace_stream(ap.second, ap2.first, ap2.second)); |
985 | 0 | } |
986 | 0 | } |
987 | 0 | } |
988 | 0 | } |
989 | 0 | } |
990 | | |
991 | | // Now we can safely mutate the annotation and its appearance streams. |
992 | 0 | for (auto& stream: streams) { |
993 | 0 | auto dict = stream.getDict(); |
994 | 0 | auto omatrix = dict.getKey("/Matrix"); |
995 | 0 | QPDFMatrix apcm; |
996 | 0 | if (omatrix.isArray()) { |
997 | 0 | QTC::TC("qpdf", "QPDFAcroFormDocumentHelper modify ap matrix"); |
998 | 0 | auto m1 = omatrix.getArrayAsMatrix(); |
999 | 0 | apcm = QPDFMatrix(m1); |
1000 | 0 | } |
1001 | 0 | apcm.concat(cm); |
1002 | 0 | auto new_matrix = QPDFObjectHandle::newFromMatrix(apcm); |
1003 | 0 | if (omatrix.isArray() || (apcm != QPDFMatrix())) { |
1004 | 0 | dict.replaceKey("/Matrix", new_matrix); |
1005 | 0 | } |
1006 | 0 | auto resources = dict.getKey("/Resources"); |
1007 | 0 | if ((!dr_map.empty()) && resources.isDictionary()) { |
1008 | 0 | adjustAppearanceStream(stream, dr_map); |
1009 | 0 | } |
1010 | 0 | } |
1011 | 0 | auto rect = cm.transformRectangle(annot.getKey("/Rect").getArrayAsRectangle()); |
1012 | 0 | annot.replaceKey("/Rect", QPDFObjectHandle::newFromRectangle(rect)); |
1013 | 0 | } |
1014 | 0 | } |
1015 | | |
1016 | | void |
1017 | | QPDFAcroFormDocumentHelper::fixCopiedAnnotations( |
1018 | | QPDFObjectHandle to_page, |
1019 | | QPDFObjectHandle from_page, |
1020 | | QPDFAcroFormDocumentHelper& from_afdh, |
1021 | | std::set<QPDFObjGen>* added_fields) |
1022 | 0 | { |
1023 | 0 | auto old_annots = from_page.getKey("/Annots"); |
1024 | 0 | if ((!old_annots.isArray()) || (old_annots.getArrayNItems() == 0)) { |
1025 | 0 | return; |
1026 | 0 | } |
1027 | | |
1028 | 0 | std::vector<QPDFObjectHandle> new_annots; |
1029 | 0 | std::vector<QPDFObjectHandle> new_fields; |
1030 | 0 | std::set<QPDFObjGen> old_fields; |
1031 | 0 | transformAnnotations( |
1032 | 0 | old_annots, |
1033 | 0 | new_annots, |
1034 | 0 | new_fields, |
1035 | 0 | old_fields, |
1036 | 0 | QPDFMatrix(), |
1037 | 0 | &(from_afdh.getQPDF()), |
1038 | 0 | &from_afdh); |
1039 | |
|
1040 | 0 | to_page.replaceKey("/Annots", QPDFObjectHandle::newArray(new_annots)); |
1041 | 0 | addAndRenameFormFields(new_fields); |
1042 | 0 | if (added_fields) { |
1043 | 0 | for (auto const& f: new_fields) { |
1044 | 0 | added_fields->insert(f.getObjGen()); |
1045 | 0 | } |
1046 | 0 | } |
1047 | 0 | } |