-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilling_form_fields.py
More file actions
151 lines (115 loc) · 4.24 KB
/
filling_form_fields.py
File metadata and controls
151 lines (115 loc) · 4.24 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
import sys
from os import path
import aspose.pdf as ap
import aspose.pdf.facades as pdf_facades
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
# Fill Text Fields
def fill_text_fields(infile, outfile):
"""Fill text fields in PDF form."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Fill text fields by name
pdf_form.fill_field("name", "John Doe")
pdf_form.fill_field("address", "123 Main St, Anytown, USA")
pdf_form.fill_field("email", "john.doe@example.com")
# Save updated PDF
pdf_form.save(outfile)
# Fill Check Box Fields
def fill_check_box_fields(infile, outfile):
"""Fill check box fields in PDF form."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Fill check box fields by name
pdf_form.fill_field("subscribe_newsletter", "Yes")
pdf_form.fill_field("accept_terms", "Yes")
# Save updated PDF
pdf_form.save(outfile)
# Fill Radio Button Fields
def fill_radio_button_fields(infile, outfile):
"""Fill radio button fields in PDF form."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Fill radio button fields by name
pdf_form.fill_field("gender", 0) # Select male option (index 0)
#pdf_form.fill_field("gender", 1) # Select female option (index 1)
# Save updated PDF
pdf_form.save(outfile)
# Fill List Box / Multi-Select Fields
def fill_list_box_fields(infile, outfile):
"""Fill list box and multi-select fields in PDF form."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Fill list box / multi-select fields by name
pdf_form.fill_field("favorite_colors", "Red")
# Save updated PDF
pdf_form.save(outfile)
# Fill Barcode Fields
def fill_barcode_fields(infile, outfile):
"""Fill barcode fields in PDF form."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Fill barcode fields by name
pdf_form.fill_field("product_barcode", "123456789012")
# Save updated PDF
pdf_form.save(outfile)
# Fill Fields by Name and Value
def fill_fields_by_name_and_value(infile, outfile):
"""Fill PDF form fields by name and value."""
# Create Form object
pdf_form = pdf_facades.Form()
# Bind PDF document
pdf_form.bind_pdf(infile)
# Fill fields by name and value
fields = {
"name": "Jane Smith",
"address": "456 Elm St, Othertown, USA",
"email": "jane.smith@example.com"
}
names = list(fields.keys())
values = list(fields.values())
output = []
pdf_form.fill_fields(names, values, output)
stream = output[0] # Get filled PDF as stream
stream.seek(0) # Reset stream position to beginning
with open(outfile, 'wb') as f:
f.write(stream.read())
def run_all_examples(data_dir=None, license_path=None):
"""Run all import form data examples and report status.
Args:
data_dir (str, optional): Input/output directory override.
license_path (str, optional): Path to Aspose.PDF license file.
Returns:
None
"""
set_license(license_path)
input_dir, output_dir = initialize_data_dir(data_dir)
examples = [
("Fill Text Fields", fill_text_fields),
("Fill Check Box Fields", fill_check_box_fields),
("Fill Radio Button Fields", fill_radio_button_fields),
("Fill List Box / Multi-Select Fields", fill_list_box_fields),
("Fill Barcode Fields", fill_barcode_fields),
("Fill Fields by Name and Value", fill_fields_by_name_and_value)
]
for name, func in examples:
try:
input_file_name = path.join(input_dir, f"{func.__name__}_in.pdf")
output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf")
func(input_file_name, output_file_name)
print(f"✅ Success: {name}")
except Exception as e:
print(f"❌ Failed: {name} - {str(e)}")
print("\nAll Fill Form Fields examples finished.")
if __name__ == "__main__":
run_all_examples()