-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf_form_fill.h
More file actions
1157 lines (1011 loc) · 41.9 KB
/
pdf_form_fill.h
File metadata and controls
1157 lines (1011 loc) · 41.9 KB
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __PDF_FORM_FILL_H__
#define __PDF_FORM_FILL_H__
#include <stdint.h>
#include <stdexcept>
#include <vector>
#include <string>
#include <algorithm>
#include "PDFParser.h"
#include "PDFObjectCast.h"
#include "PDFDictionary.h"
#include "PDFDocumentCopyingContext.h"
#include "InputFile.h"
#include "RefCountPtr.h"
#include "PDFArray.h"
#include "PDFName.h"
#include "PDFIndirectObjectReference.h"
#include "PDFStreamInput.h"
#include "IByteReader.h"
#include "EStatusCode.h"
#include "ParsedPrimitiveHelper.h"
#include "PDFHexString.h"
#include "UnicodeString.h"
#include "AbstractContentContext.h"
#include "PDFWriter.h"
#include "PDFModifiedPage.h"
#include "ObjectsContext.h"
#include "DictionaryContext.h"
#include "PDFPage.h"
#include "PDFRectangle.h"
#include "PageContentContext.h"
#include "PDFFormXObject.h"
#include "XObjectContentContext.h"
#include "PDFUsedFont.h"
#include "PDFInteger.h"
#include "PDFReal.h"
#include "PDFTextString.h"
#include "PDFLiteralString.h"
#include "Trace.h"
class pdf_form_fill {
public:
const std::string _NULL_ = "__MAGIC_NULL__";
class pdf_value_t {
public:
enum {
NONE,
INTEGER,
DOUBLE,
BOOL,
STRING,
PDFARRAY,
};
private:
int p_type = NONE;
union {
long long i_value;
double d_value;
bool b_value;
};
std::string s_value;
PDFObjectCastPtr<PDFArray> a_value;
public:
void set() {
p_type = NONE;
}
void set(long long value) {
i_value = value;
p_type = INTEGER;
}
void set(double value) {
d_value = value;
p_type = DOUBLE;
}
void set(bool value) {
b_value = value;
p_type = BOOL;
}
void set(std::string value) {
s_value = value;
p_type = STRING;
}
void set(PDFObjectCastPtr<PDFArray> value) {
a_value = value;
p_type = PDFARRAY;
}
void set(const char* value) {
s_value = value;
p_type = STRING;
}
void set(const pdf_value_t& m) {
p_type = m.p_type;
switch(p_type) {
case NONE: {
break;
}
case INTEGER: {
i_value = m.i_value;
break;
}
case DOUBLE: {
d_value = m.d_value;
break;
}
case BOOL: {
b_value = m.b_value;
break;
}
case STRING: {
s_value = m.s_value;
break;
}
case PDFARRAY: {
a_value = m.a_value;
}
default: {
break;
}
}
}
pdf_value_t() {
p_type = NONE;
}
pdf_value_t(const pdf_value_t& value) {
set(value);
}
pdf_value_t(long long value) {
set(value);
}
pdf_value_t(double value) {
set(value);
}
pdf_value_t(std::string value) {
set(value);
}
pdf_value_t(const char* value) {
set(value);
}
pdf_value_t(bool value) {
set(value);
}
pdf_value_t(PDFObjectCastPtr<PDFArray> value) {
set(value);
}
std::string type() {
switch(p_type) {
case NONE: {
return "none";
}
case INTEGER: {
return "int";
}
case DOUBLE: {
return "double";
}
case BOOL: {
return "bool";
}
case STRING: {
return "string";
}
case PDFARRAY: {
return "pdfarray";
}
default: {
return "none";
}
}
}
bool operator==(const pdf_value_t& m) {
if(p_type != m.p_type) {
return false;
}
switch(p_type) {
case NONE: {
return true;
}
case INTEGER: {
return i_value == m.i_value;
}
case DOUBLE: {
return d_value == m.d_value;
}
case BOOL: {
return b_value == m.b_value;
}
case STRING: {
return s_value == m.s_value;
}
case PDFARRAY: {
return a_value.GetPtr() == m.a_value.GetPtr();
}
default: {
return false;
}
}
}
bool operator!=(const pdf_value_t& m) {
if(p_type != m.p_type) {
return true;
}
switch(p_type) {
case NONE: {
return false;
}
case INTEGER: {
return i_value != m.i_value;
}
case DOUBLE: {
return d_value != m.d_value;
}
case BOOL: {
return b_value != m.b_value;
}
case STRING: {
return s_value != m.s_value;
}
case PDFARRAY: {
return a_value.GetPtr() != m.a_value.GetPtr();
}
default: {
return false;
}
}
}
void operator=(long long value) {
set(value);
}
void operator=(double value) {
set(value);
}
void operator=(bool value) {
set(value);
}
void operator=(std::string value) {
set(value);
}
void operator=(const char* value) {
set(value);
}
void operator=(const pdf_value_t& value) {
set(value);
}
void operator=(PDFObjectCastPtr<PDFArray> value) {
set(value);
}
long long ToInteger() {
switch(p_type) {
case NONE: {
return 0;
}
case INTEGER: {
return i_value;
}
case DOUBLE: {
return (long long)d_value;
}
case BOOL: {
return (long long)b_value;
}
case STRING: {
return strtoll(s_value.c_str(), NULL, 0);
}
case PDFARRAY: {
return 0;
}
default: {
return 0;
}
}
}
double ToDouble() {
switch(p_type) {
case NONE: {
return 0.0;
}
case INTEGER: {
return (double)i_value;
}
case DOUBLE: {
return d_value;
}
case BOOL: {
return (double)b_value;
}
case STRING: {
return strtod(s_value.c_str(), NULL);
}
case PDFARRAY: {
return 0.0;
}
default: {
return 0.0;
}
}
}
bool ToBool() {
switch(p_type) {
case NONE: {
return false;
}
case INTEGER: {
return (bool)i_value;
}
case DOUBLE: {
return (bool)d_value;
}
case BOOL: {
return b_value;
}
case STRING: {
return s_value.size() != 0;
}
case PDFARRAY: {
return a_value.GetPtr() != NULL;
}
default: {
return false;
}
}
}
std::string ToString() {
switch(p_type) {
case NONE: {
return "";
}
case INTEGER: {
return std::to_string(i_value);
}
case DOUBLE: {
return std::to_string(d_value);
}
case BOOL: {
return b_value ? "true" : "false";
}
case STRING: {
return s_value;
}
case PDFARRAY: {
return "";
}
default: {
return "";
}
}
}
PDFObjectCastPtr<PDFArray> ToPDFArray() {
switch(p_type) {
case NONE: {
return NULL;
}
case INTEGER: {
return NULL;
}
case DOUBLE: {
return NULL;
}
case BOOL: {
return NULL;
}
case STRING: {
return NULL;
}
case PDFARRAY: {
return a_value;
}
default: {
return NULL;
}
}
}
};
typedef struct {
bool debug;
AbstractContentContext::TextOptions* defaultTextOptions;
} options_t;
typedef struct {
PDFWriter& writer;
PDFParser& reader;
PDFDocumentCopyingContext* copyingContext;
ObjectsContext& objectsContext;
std::map<std::string, pdf_value_t> data;
PDFObjectCastPtr<PDFDictionary> acroformDict;
options_t options;
} handles_t;
typedef struct {
bool existing;
ObjectIDType id;
PDFObject* field;
} field_ref_t;
private:
/**
* a wonderfully reusable method to recreate a dict without all the keys that we want to change
* note that it starts writing a dict, but doesn't finish it. your job
*/
DictionaryContext* startModifiedDictionary(handles_t handles, PDFObjectCastPtr<PDFDictionary> originalDict, std::vector<std::string> excludedKeys) {
DictionaryContext* newDict = handles.objectsContext.StartDictionary();
MapIterator<PDFNameToPDFObjectMap> it = originalDict->GetIterator();
PDFObjectCastPtr<PDFName> key;
PDFObject* value;
while(it.MoveNext()) {
key = it.GetKey();
value = it.GetValue();
if(std::find(excludedKeys.begin(), excludedKeys.end(), key->GetValue()) != excludedKeys.end()) {
continue;
}
newDict->WriteKey(key->GetValue());
handles.copyingContext->CopyDirectObjectAsIs(value);
}
return newDict;
}
void defaultTerminalFieldWrite(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary) {
// default write of ending field. no reason to recurse to kids
handles.copyingContext->CopyDirectObjectAsIs(fieldDictionary.GetPtr());
handles.objectsContext.EndIndirectObject();
}
/**
* Update radio button value. look for the field matching the value, which should be an index.
* Set its ON appearance as the value, and set all radio buttons appearance to off, but the selected one which should be on
*/
void updateOptionButtonValue(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary, pdf_value_t value) {
bool isWidget = false;
PDFObjectCastPtr<PDFName> subtype = fieldDictionary->QueryDirectObject("Subtype");
isWidget = (subtype != NULL && subtype->GetValue() == "Widget");
if (isWidget || fieldDictionary->Exists("Kids") == false) {
// this radio button has just one option and its in the widget. also means no kids
DictionaryContext* modifiedDict = startModifiedDictionary(handles, fieldDictionary, { "V", "AS" });
std::string appearanceName;
if (value.type() == "none") {
// false is easy, just write '/Off' as the value and as the appearance stream
appearanceName = "Off";
} else {
// grab the non off value. that should be the yes one
PDFObjectCastPtr<PDFDictionary> apDictionary = handles.reader.QueryDictionaryObject(fieldDictionary.GetPtr(), "AP");
PDFObjectCastPtr<PDFDictionary> nAppearances = handles.reader.QueryDictionaryObject(apDictionary.GetPtr(), "N");
MapIterator<PDFNameToPDFObjectMap> it = nAppearances->GetIterator();
PDFObjectCastPtr<PDFName> key;
while(it.MoveNext()) {
key = it.GetKey();
if(key->GetValue() != "Off") {
appearanceName = key->GetValue();
break;
}
}
}
modifiedDict->WriteKey("V");
modifiedDict->WriteNameValue(appearanceName);
modifiedDict->WriteKey("AS");
modifiedDict->WriteNameValue(appearanceName);
handles.objectsContext.EndDictionary(modifiedDict);
handles.objectsContext.EndIndirectObject();
} else {
// Field. this would mean that there's a kid array, and there are offs and ons to set
DictionaryContext* modifiedDict = startModifiedDictionary(handles, fieldDictionary, {"V", "Kids"});
PDFObjectCastPtr<PDFArray> kidsArray = handles.reader.QueryDictionaryObject(fieldDictionary.GetPtr(), "Kids");
std::string appearanceName;
if (value.ToString() == "none") {
// false is easy, just write '/Off' as the value and as the appearance stream
appearanceName = "Off";
} else {
// grab the non off value. that should be the yes one
PDFObjectCastPtr<PDFDictionary> widgetDictionary = handles.reader.QueryArrayObject(kidsArray.GetPtr(), value.ToInteger());
PDFObjectCastPtr<PDFDictionary> apDictionary = handles.reader.QueryDictionaryObject(widgetDictionary.GetPtr(), "AP");
PDFObjectCastPtr<PDFDictionary> nAppearances = handles.reader.QueryDictionaryObject(apDictionary.GetPtr(), "N");
MapIterator<PDFNameToPDFObjectMap> it = nAppearances->GetIterator();
PDFObjectCastPtr<PDFName> key;
while(it.MoveNext()) {
key = it.GetKey();
if(key->GetValue() != "Off") {
appearanceName = key->GetValue();
break;
}
}
}
// set the V value on the new field dictionary
modifiedDict->WriteKey("V");
modifiedDict->WriteNameValue(appearanceName);
// write the Kids key before we write the kids array
modifiedDict->WriteKey("Kids");
// write the kids array, similar to writeFilledFields, but knowing that these are widgets and that AS needs to be set
std::vector<field_ref_t> fieldsReferences = writeKidsAndEndObject(handles, modifiedDict, kidsArray);
// recreate widget kids, turn on or off based on their relation to the target value
for(size_t i = 0; i < fieldsReferences.size(); i++) {
field_ref_t fieldReference = fieldsReferences[i];
PDFObjectCastPtr<PDFDictionary> sourceField = NULL;
if(fieldReference.existing) {
handles.objectsContext.StartModifiedIndirectObject(fieldReference.id);
sourceField = handles.reader.ParseNewObject(fieldReference.id);
} else {
handles.objectsContext.StartNewIndirectObject(fieldReference.id);
sourceField = fieldReference.field;
}
DictionaryContext* modifiedFieldDict = startModifiedDictionary(handles, sourceField, { "AS" });
if (value.ToInteger() == i) {
// this widget should be on
modifiedFieldDict->WriteKey("AS"); //doen
modifiedFieldDict->WriteNameValue(appearanceName); // note that we have saved it earlier
} else {
// this widget should be off
modifiedFieldDict->WriteKey("AS");
modifiedFieldDict->WriteNameValue("Off");
}
// finish
handles.objectsContext.EndDictionary(modifiedFieldDict);
handles.objectsContext.EndIndirectObject();
}
}
}
std::string getOriginalTextFieldAppearanceStreamCode(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary) {
// get the single appearance stream for the text, field. we'll use it to recreate the new one
bool appearanceInField = fieldDictionary->Exists("Subtype");
if(appearanceInField) {
bool is_widget;
PDFObjectCastPtr<PDFName> subtype = fieldDictionary->QueryDirectObject("Subtype");
appearanceInField = (subtype->GetValue() == "Widget" || !fieldDictionary->Exists("Kids"));
}
PDFObjectCastPtr<PDFDictionary> appearanceParent = NULL;
if(appearanceInField) {
appearanceParent = fieldDictionary;
} else {
if(fieldDictionary->Exists("Kids")) {
PDFObjectCastPtr<PDFArray> kidsArray = handles.reader.QueryDictionaryObject(fieldDictionary.GetPtr(), "Kids");
if(kidsArray->GetLength() > 0) {
appearanceParent = handles.reader.QueryArrayObject(kidsArray.GetPtr(), 0);
}
}
}
if(!appearanceParent) {
return _NULL_;
}
if(!appearanceParent->Exists("AP"))
return _NULL_;
PDFObject* m = handles.reader.QueryDictionaryObject(appearanceParent.GetPtr(), "AP");
PDFObjectCastPtr<PDFDictionary> appearance = handles.reader.QueryDictionaryObject(appearanceParent.GetPtr(), "AP");
if(!appearance->Exists("N"))
return _NULL_;
PDFObjectCastPtr<PDFStreamInput> appearanceXObject = handles.reader.QueryDictionaryObject(appearance.GetPtr(), "N");
return readStreamToString(handles, appearanceXObject);
}
void writeAppearanceXObjectForText(handles_t handles, ObjectIDType formId, PDFObjectCastPtr<PDFDictionary> fieldsDictionary, pdf_value_t text, std::map<std::string, pdf_value_t> inheritedProperties) {
PDFObjectCastPtr<PDFArray> rect = handles.reader.QueryDictionaryObject(fieldsDictionary.GetPtr(), "Rect");
PDFObjectCastPtr<PDFName> _da;
std::string da;
if((_da = fieldsDictionary->QueryDirectObject("DA")) != NULL) {
da = _da->GetValue();
} else {
da = inheritedProperties["DA"].ToString();
}
PDFObjectCastPtr<PDFInteger> _q;
long long q;
if((_q = fieldsDictionary->QueryDirectObject("Q")) != NULL) {
q = _q->GetValue();
} else {
q = inheritedProperties["Q"].ToInteger();
}
if(handles.options.debug) {
printf("creating new appearance with:\n");
printf("da = %s\n", da.c_str());
printf("q = %lli\n", q);
//printf("fieldsDictionary =", fieldsDictionary.toJSObject());
//printf("inheritedProperties =", inheritedProperties);
printf("text = %s\n", text.ToString().c_str());
}
std::string originalAppearanceContent = getOriginalTextFieldAppearanceStreamCode(handles, fieldsDictionary);
std::string before = "";
std::string after = "";
std::string lookfor_bmc = "/Tx BMC";
std::string lookfor_emc = "EMC";
if(originalAppearanceContent != _NULL_) {
size_t pre = originalAppearanceContent.find(lookfor_bmc);
if(pre != std::string::npos) {
before = originalAppearanceContent.substr(0, pre);
size_t post = originalAppearanceContent.find(lookfor_emc, pre + lookfor_bmc.size());
if(post != std::string::npos) {
after = originalAppearanceContent.substr(post + lookfor_emc.size());
}
} else {
before = originalAppearanceContent;
}
}
double upper_right_x = 0.0;
double lower_left_x = 0.0;
double upper_right_y = 0.0;
double lower_left_y = 0.0;
PDFObject* t;
t = handles.reader.QueryArrayObject(rect.GetPtr(), 0);
if(t->GetType() == PDFDictionary::ePDFObjectInteger) {
lower_left_x = ((PDFObjectCastPtr<PDFInteger>)t)->GetValue();
} else if(t->GetType() == PDFDictionary::ePDFObjectReal){
lower_left_x = ((PDFObjectCastPtr<PDFReal>)t)->GetValue();
}
t = handles.reader.QueryArrayObject(rect.GetPtr(), 1);
if(t->GetType() == PDFDictionary::ePDFObjectInteger) {
lower_left_y = ((PDFObjectCastPtr<PDFInteger>)t)->GetValue();
} else if(t->GetType() == PDFDictionary::ePDFObjectReal){
lower_left_y = ((PDFObjectCastPtr<PDFReal>)t)->GetValue();
}
t = handles.reader.QueryArrayObject(rect.GetPtr(), 2);
if(t->GetType() == PDFDictionary::ePDFObjectInteger) {
upper_right_x = ((PDFObjectCastPtr<PDFInteger>)t)->GetValue();
} else if(t->GetType() == PDFDictionary::ePDFObjectReal){
upper_right_x = ((PDFObjectCastPtr<PDFReal>)t)->GetValue();
}
t = handles.reader.QueryArrayObject(rect.GetPtr(), 3);
if(t->GetType() == PDFDictionary::ePDFObjectInteger) {
upper_right_y = ((PDFObjectCastPtr<PDFInteger>)t)->GetValue();
} else if(t->GetType() == PDFDictionary::ePDFObjectReal){
upper_right_y = ((PDFObjectCastPtr<PDFReal>)t)->GetValue();
}
double boxWidth = upper_right_x - lower_left_x;
double boxHeight = upper_right_y - lower_left_y;
PDFFormXObject* xobjectForm = handles.writer.StartFormXObject(PDFRectangle(0, 0, boxWidth, boxHeight), formId);
// If default text options setup, use them to determine the text appearance. including quad support, horizontal centering etc.
// Otherwise, use naive method: Will use Tj with "code" encoding to write the text, assuming encoding should work (??). if it won't i need real fonts here
// and DA is not gonna be useful. so for now let's use as is.
// For the same reason i'm not support Quad, as well
// Should be able to parse the following from the DA, and map to system font
// temporarily, let user input the values
AbstractContentContext::TextOptions* textOptions = handles.options.defaultTextOptions;
if(textOptions != NULL) {
// grab text dimensions for quad support and vertical centering
PDFUsedFont::TextMeasures textDimensions = textOptions->font->CalculateTextDimensions(text.ToString(), textOptions->fontSize);
// vertical centering
double yPos = (boxHeight - textDimensions.height) / 2;
// horizontal pos per quad
long long quad = q || 0;
double xPos = 0;
switch(quad) {
case 0:
// left align
xPos = 0;
break;
case 1:
// center
xPos = (boxWidth - textDimensions.width) / 2;
break;
case 2:
// right align
xPos = (boxWidth - textDimensions.width);
}
XObjectContentContext* xobjectFormContext = xobjectForm->GetContentContext();
xobjectFormContext->WriteFreeCode(before);
xobjectFormContext->WriteFreeCode("/Tx BMC\r\n");
xobjectFormContext->q();
xobjectFormContext->WriteText(xPos, yPos, text.ToString(), *textOptions);
xobjectFormContext->Q();
xobjectFormContext->WriteFreeCode("EMC");
xobjectFormContext->WriteFreeCode(after);;
} else {
// Naive form, no quad support...and text may not show and may be mispositioned
XObjectContentContext* xobjectFormContext = xobjectForm->GetContentContext();
xobjectFormContext->WriteFreeCode(before);
xobjectFormContext->WriteFreeCode("/Tx BMC\r\n");
xobjectFormContext->q();
xobjectFormContext->BT();
xobjectFormContext->WriteFreeCode(da + "\r\n");
xobjectFormContext->Tj(text.ToString());
xobjectFormContext->ET();
xobjectFormContext->Q();
xobjectFormContext->WriteFreeCode("EMC");
xobjectFormContext->WriteFreeCode(after);
}
if(handles.acroformDict->Exists("DR")) {
// copy all but the keys that exist already
PDFObjectCastPtr<PDFDictionary> dr = handles.reader.QueryDictionaryObject(handles.acroformDict.GetPtr(), "DR");
MapIterator<PDFNameToPDFObjectMap> it = dr->GetIterator();
RefCountPtr<PDFName> key;
PDFObject* value;
DictionaryContext* page_out_dic = handles.writer.GetObjectsContext().StartDictionary();
while(it.MoveNext()) {
key = it.GetKey();
value = it.GetValue();
if(key->GetValue() != "ProcSet" && (textOptions == NULL || key->GetValue() != "Font")) {
page_out_dic->WriteKey(key->GetValue());
handles.copyingContext->CopyDirectObjectAsIs(value);
}
}
}
handles.writer.EndFormXObject(xobjectForm);
}
#define BUFFER_SIZE 10000
std::string readStreamToString(handles_t handles, PDFObjectCastPtr<PDFStreamInput> stream) {
Byte readData[BUFFER_SIZE];
std::string buff = "";
IByteReader* readStream = handles.reader.StartReadingFromStream(stream.GetPtr());
while(readStream->NotEnded()) {
IOBasicTypes::LongBufferSizeType read = readStream->Read(readData, BUFFER_SIZE);
for(IOBasicTypes::LongBufferSizeType i = 0; i < read; i++) {
UnicodeString u;
u.FromUTF16((char*)readData);
EStatusCodeAndString result = u.ToUTF8();
buff += result.second;
}
}
return buff;
}
void writeFieldWithAppearanceForText(handles_t handles, DictionaryContext* targetFieldDict, PDFObjectCastPtr<PDFDictionary> sourceFieldDictionary, bool appearanceInField, pdf_value_t textToWrite, std::map<std::string, pdf_value_t> inheritedProperties) {
// determine how to write appearance
ObjectIDType newAppearanceFormId = handles.objectsContext.GetInDirectObjectsRegistry().AllocateNewObjectID();
if(appearanceInField) {
// Appearance in field - so write appearance dict in field
targetFieldDict->WriteKey("AP");
DictionaryContext* apDict = handles.objectsContext.StartDictionary();
apDict->WriteKey("N");
apDict->WriteObjectReferenceValue(newAppearanceFormId);
handles.objectsContext.EndDictionary(apDict);
handles.objectsContext.EndDictionary(targetFieldDict);
handles.objectsContext.EndIndirectObject();
} else {
// finish the field object
handles.objectsContext.EndDictionary(targetFieldDict);
handles.objectsContext.EndIndirectObject();
// write in kid (there should be just one)
PDFObjectCastPtr<PDFArray> kidsArray = handles.reader.QueryDictionaryObject(sourceFieldDictionary.GetPtr(), "Kids");
std::vector<field_ref_t> fieldsReferences = writeKidsAndEndObject(handles, targetFieldDict, kidsArray);
// recreate widget kid, with new stream reference
field_ref_t fieldReference = fieldsReferences[0];
PDFObjectCastPtr<PDFDictionary> sourceField;
if(fieldReference.existing) {
handles.objectsContext.StartModifiedIndirectObject(fieldReference.id);
sourceField = handles.reader.ParseNewObject(fieldReference.id);
} else {
handles.objectsContext.StartNewIndirectObject(fieldReference.id);
sourceField = fieldReference.field;
}
DictionaryContext* modifiedDict = startModifiedDictionary(handles, sourceField, {"AP"});
modifiedDict->WriteKey("AP");
DictionaryContext* apDict = handles.objectsContext.StartDictionary();
apDict->WriteKey("N");
apDict->WriteObjectReferenceValue(newAppearanceFormId);
handles.objectsContext.EndDictionary(apDict);
handles.objectsContext.EndDictionary(modifiedDict);
handles.objectsContext.EndIndirectObject();
}
// write the new stream xobject
writeAppearanceXObjectForText(handles, newAppearanceFormId, sourceFieldDictionary, textToWrite, inheritedProperties);
}
void updateTextValue(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary, pdf_value_t value, bool isRich, std::map<std::string, pdf_value_t> inheritedProperties) {
std::vector<std::string> fieldsToRemove;
bool appearanceInField = fieldDictionary->Exists("Subtype");
if(appearanceInField) {
PDFObjectCastPtr<PDFName> subtype = fieldDictionary->QueryDirectObject("Subtype");
appearanceInField = (subtype->GetValue() == "Widget" || !fieldDictionary->Exists("Kids"));
}
fieldsToRemove.push_back("V");
if(appearanceInField) {
// add skipping AP if in field (and not in a child widget)
fieldsToRemove.push_back("AP");
}
if(isRich) {
// skip RV if rich
fieldsToRemove.push_back("RV");
}
DictionaryContext* modifiedDict = startModifiedDictionary(handles, fieldDictionary, fieldsToRemove);
// start with value, setting both plain value and rich value
modifiedDict->WriteKey("V");
modifiedDict->WriteLiteralStringValue(PDFHexString(value.ToString()));
if(isRich) {
modifiedDict->WriteKey("RV");
modifiedDict->WriteLiteralStringValue(PDFHexString(value.ToString()));
}
writeFieldWithAppearanceForText(handles, modifiedDict, fieldDictionary, appearanceInField, value, inheritedProperties);
}
void updateChoiceValue(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary, pdf_value_t value, std::map<std::string, pdf_value_t> inheritedProperties) {
bool appearanceInField = fieldDictionary->Exists("Subtype");
if(appearanceInField) {
PDFObjectCastPtr<PDFName> subtype = fieldDictionary->QueryDirectObject("Subtype");
appearanceInField = (subtype->GetValue() == "Widget" || !fieldDictionary->Exists("Kids"));
}
std::vector<std::string> fieldsToRemove = { "V" };
if (appearanceInField) {
// add skipping AP if in field (and not in a child widget)
fieldsToRemove.push_back("AP");
}
DictionaryContext* modifiedDict = startModifiedDictionary(handles, fieldDictionary, fieldsToRemove);
// start with value, setting per one or multiple selection. also choose the text to write in appearance
std::string textToWrite;
if(value.type() == "string") {
// one option
modifiedDict->WriteKey("V");
modifiedDict->WriteLiteralStringValue(PDFTextString(value.ToString()).ToString());
textToWrite = value.ToString();
} else {
// multiple options
modifiedDict->WriteKey("V");
handles.objectsContext.StartArray();
PDFObjectCastPtr<PDFArray> array = value.ToPDFArray();
SingleValueContainerIterator<PDFObjectVector> it = array->GetIterator();
bool first = true;
textToWrite = "";
while(it.MoveNext()) {
PDFObjectCastPtr<PDFLiteralString> field = it.GetItem();
if(first) {
first = false;
textToWrite = field->GetValue();
}
handles.objectsContext.WriteLiteralString(field->GetValue());
}
handles.objectsContext.EndArray();
}
writeFieldWithAppearanceForText(handles, modifiedDict, fieldDictionary, appearanceInField, textToWrite, inheritedProperties);
}
/**
* Update a field. splits to per type functions
*/
void updateFieldWithValue(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary, pdf_value_t value, std::map<std::string, pdf_value_t> inheritedProperties) {
// Update a field with value. There is a logical assumption made here:
// This must be a terminal field. meaning it is a field, and it either has no kids, it also holding
// Widget data or that it has one or more kids defining its widget annotation(s). Normally it would be
// One but in the case of a radio button, where there's one per option.
std::string localFieldType = "";
std::string fieldType = "";
long long localFlags = 0;
long long flags = 0;
PDFObjectCastPtr<PDFName> ft = fieldDictionary->QueryDirectObject("FT");
if(ft != NULL) {
fieldType = ft->GetValue();
} else {
fieldType = inheritedProperties["FT"].ToString();
}
PDFObjectCastPtr<PDFInteger> ff = fieldDictionary->QueryDirectObject("Ff");
if(ff != NULL) {
flags = ff->GetValue();
} else {
flags = inheritedProperties["Ff"].ToInteger();
}
// the rest is fairly type dependent, so let's check the type
if(fieldType == "Btn") {
if((flags >> 16) & 1) {
// push button. can't write a value. forget it.
defaultTerminalFieldWrite(handles, fieldDictionary);
} else {
// checkbox or radio button
updateOptionButtonValue(handles,fieldDictionary,(((flags >> 15) & 1) != 0) ? value : (value.ToBool() ? pdf_value_t(false): pdf_value_t()));
}
} else if(fieldType == "Tx") {
// rich or plain text
updateTextValue(handles, fieldDictionary, value, (flags >> 25) & 1, inheritedProperties);
} else if(fieldType == "Ch") {
updateChoiceValue(handles, fieldDictionary, value, inheritedProperties);
} else if(fieldType == "Sig") {
// signature, ain't handling that. should return or throw an error sometimes
defaultTerminalFieldWrite(handles, fieldDictionary);
} else {
// in case there's a fault and there's no type, or it's irrelevant
defaultTerminalFieldWrite(handles, fieldDictionary);
}
}
void writeFieldAndKids(handles_t handles, PDFObjectCastPtr<PDFDictionary> fieldDictionary, std::map<std::string, pdf_value_t> inheritedProperties, std::string baseFieldName) {
// this field or widget doesn't need value rewrite. but its kids might. so write the dictionary as is, dropping kids.
// write them later and recurse.
DictionaryContext* modifiedFieldDict = startModifiedDictionary(handles, fieldDictionary, { "Kids" });
// if kids exist, continue to them for extra filling!
PDFObjectCastPtr<PDFArray> kids = handles.reader.QueryDictionaryObject(fieldDictionary.GetPtr(), "Kids");
if(kids != NULL) {
std::map<std::string, pdf_value_t> localEnv;
// prep some inherited values and push env
if (fieldDictionary->Exists("FT"))
localEnv["FT"] = ((PDFObjectCastPtr<PDFName>) fieldDictionary->QueryDirectObject("FT"))->GetValue();
if (fieldDictionary->Exists("Ff"))
localEnv["Ff"] = ((PDFObjectCastPtr<PDFInteger>) fieldDictionary->QueryDirectObject("Ff"))->GetValue();
if (fieldDictionary->Exists("DA"))
localEnv["DA"] = ((PDFObjectCastPtr<PDFName>) fieldDictionary->QueryDirectObject("DA"))->GetValue();
if (fieldDictionary->Exists("Q"))
localEnv["Q"] = ((PDFObjectCastPtr<PDFInteger>) fieldDictionary->QueryDirectObject("Q"))->GetValue();
if (fieldDictionary->Exists("Opt"))
localEnv["Opt"] = (PDFObjectCastPtr<PDFArray>) fieldDictionary->QueryDirectObject("Opt");
modifiedFieldDict->WriteKey("Kids");
// recurse to kids. note that this will take care of ending this object
localEnv.insert(inheritedProperties.begin(), inheritedProperties.end());
writeFilledFields(handles, modifiedFieldDict, kids, localEnv, baseFieldName + ".");
} else {
// no kids, can finish object now
handles.objectsContext.EndDictionary(modifiedFieldDict);
handles.objectsContext.EndIndirectObject();
}
}
/**
* writes a single field. will fill with value if found in data.