-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFormFieldsHelper.cs
148 lines (135 loc) · 6.06 KB
/
FormFieldsHelper.cs
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
using iDiTect.Pdf.Editing;
using iDiTect.Pdf.InteractiveForms;
using iDiTect.Pdf.IO;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
namespace iDiTect.Pdf.Demo
{
public static class FormFieldsHelper
{
public static void CreateFormField()
{
PdfDocument document = new PdfDocument();
PdfPage page = document.Pages.AddPage();
PageContentBuilder builder = new PageContentBuilder(page);
double padding = 10;
double topOffset = 100;
//Create CheckBox FormField
CheckBoxField check = new CheckBoxField("checkBox1");
check.IsChecked = true;
//Add CheckBox to document fields
document.AcroForm.FormFields.Add(check);
//Draw CheckBox to page
builder.Position.Translate(50, topOffset);
builder.DrawWidget(check, new Size(10, 10));
//Draw CheckBox description
builder.Position.Translate(50 + 10 + padding, topOffset);
builder.DrawText("CheckBox");
//Create ComboBox FormField
ComboBoxField combo = new ComboBoxField("comboBox1");
combo.TextState.FontSize = 16;
combo.Options.Add(new ChoiceOption("Combo 1"));
combo.Options.Add(new ChoiceOption("Combo 2"));
combo.Options.Add(new ChoiceOption("Combo 3"));
combo.Value = combo.Options[2];
//Add ComboBox to document fields
document.AcroForm.FormFields.Add(combo);
//Draw ComboBox to page
topOffset += 30;
builder.Position.Translate(50, topOffset);
builder.DrawWidget(combo, new Size(100, 30));
//Draw ComboBox description
builder.Position.Translate(50 + 100 + padding, topOffset);
builder.DrawText("ComboBox");
//Create ListBox FormField
ListBoxField list = new ListBoxField("listBox1");
list.AllowMultiSelection = true;
list.TextState.FontSize = 16;
list.Options.Add(new ChoiceOption("List 1"));
list.Options.Add(new ChoiceOption("List 2"));
list.Options.Add(new ChoiceOption("List 3"));
list.Value = new ChoiceOption[] { list.Options[0], list.Options[2] };
//Add ListBox to document fields
document.AcroForm.FormFields.Add(list);
//Draw ListBox to page
topOffset += 30;
builder.Position.Translate(50, topOffset);
builder.DrawWidget(list, new Size(100, 100));
//Draw ListBox description
builder.Position.Translate(50 + 100 + padding, topOffset);
builder.DrawText("ListBox");
//Create RadioButton FormField
RadioButtonField radio = new RadioButtonField("radioButton1");
radio.Options.Add(new RadioOption("Radio 1"));
radio.Options.Add(new RadioOption("Radio 2"));
radio.Value = radio.Options[1];
//Add RadioButton to document fields
document.AcroForm.FormFields.Add(radio);
//Draw RadioButton to page
topOffset += 100;
foreach (RadioOption option in radio.Options)
{
topOffset += 30;
builder.Position.Translate(50, topOffset);
builder.DrawWidget(radio, option, new Size(10, 10));
}
//Draw ListBox description
builder.Position.Translate(50 + 10 + padding, topOffset);
builder.DrawText("RadioButton");
//Create TextBox FormField
TextBoxField textBox = new TextBoxField("textBox1");
textBox.TextState.FontSize = 16;
textBox.Value = "Input...";
//Add TextBox to document fields
document.AcroForm.FormFields.Add(textBox);
//Draw TextBox to page
topOffset += 30;
builder.Position.Translate(50, topOffset);
builder.DrawWidget(textBox, new Size(100, 30));
//Draw TextBox description
builder.Position.Translate(50 + 100 + padding, topOffset);
builder.DrawText("TextBox");
using (FileStream fs = File.OpenWrite("CreateFormField.pdf"))
{
PdfFile pdfFile = new PdfFile();
pdfFile.Export(document, fs);
}
}
public static void ReadAndModifyFormField()
{
PdfFile pdfFile = new PdfFile();
PdfDocument document = pdfFile.Import(File.ReadAllBytes("CreateFormField.pdf"));
foreach (FormField field in document.AcroForm.FormFields)
{
switch (field.FieldType)
{
case FormFieldType.CheckBox:
if (field.Name == "checkBox1")
((CheckBoxField)field).IsChecked = false;
break;
case FormFieldType.ComboBox:
if (field.Name == "comboBox1")
((ComboBoxField)field).Value = ((ComboBoxField)field).Options[0];
break;
case FormFieldType.ListBox:
if (field.Name == "listBox1")
((ListBoxField)field).Value = new ChoiceOption[] { ((ListBoxField)field).Options[0] };
break;
case FormFieldType.RadioButton:
if (field.Name == "radioButton1")
((RadioButtonField)field).Value = ((RadioButtonField)field).Options[0];
break;
case FormFieldType.TextBox:
if (field.Name == "textBox1")
((TextBoxField)field).Value = "new text";
break;
}
}
File.WriteAllBytes("ModifyFormField.pdf", pdfFile.Export(document));
}
}
}