-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_interactive_annotation.py
More file actions
233 lines (176 loc) · 6.97 KB
/
Copy pathexample_interactive_annotation.py
File metadata and controls
233 lines (176 loc) · 6.97 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
from os import path
import sys
import aspose.pdf as ap
sys.path.append(path.join(path.dirname(__file__), ".."))
from config import set_license, initialize_data_dir
def link_add(infile, outfile):
"""
Add a link annotation to the first page of a PDF document.
Args:
infile (str): The name of the input PDF file.
outfile (str): The name of the output PDF file.
Returns:
None
Example:
>>> link_add("sample.pdf", "output.pdf")
Note:
This function finds the text "file" on the first page and creates a link annotation
that navigates to www.aspose.com when clicked.
"""
document = ap.Document(infile)
text_fragment_absorber = ap.text.TextFragmentAbsorber("file")
# Accept the absorber for the 1st page only
document.pages[1].accept(text_fragment_absorber)
phone_number_fragment = text_fragment_absorber.text_fragments[1]
link_annotation = ap.annotations.LinkAnnotation(document.pages[1], phone_number_fragment.rectangle)
link_annotation.action = ap.annotations.GoToURIAction("https://www.aspose.com")
# Add annotation to page
document.pages[1].annotations.append(link_annotation)
document.save(outfile)
def link_get(infile, outfile):
"""
Retrieve and print the rectangle coordinates of all link annotations on the first page of a PDF document.
Args:
infile (str): The name of the input PDF file.
outfile (str): The name of the output PDF file (not used in this function).
Returns:
None
Example:
>>> link_get("sample.pdf", "output.pdf")
Note:
This function prints the rectangle coordinates of all link annotations found on the first page.
"""
document = ap.Document(infile)
link_annotations = [
a
for a in document.pages[1].annotations
if (a.annotation_type == ap.annotations.AnnotationType.LINK)
]
for link_annotation in link_annotations:
print(link_annotation.rect)
def link_delete(infile, outfile):
"""
Delete all link annotations from the first page of a PDF document.
Args:
infile (str): The name of the input PDF file.
outfile (str): The name of the output PDF file.
Returns:
None
Example:
>>> link_delete("sample.pdf", "output.pdf")
Note:
This function removes all link annotations from the first page and saves the modified PDF.
"""
document = ap.Document(infile)
link_annotations = [
a
for a in document.pages[1].annotations
if (a.annotation_type == ap.annotations.AnnotationType.LINK)
]
for link_annotation in link_annotations:
document.pages[1].annotations.delete(link_annotation)
document.save(outfile)
def line_annotation_add(infile, outfile):
"""Add a line annotation to the first page."""
document = ap.Document(infile)
# Create Line Annotation
line_annotation = ap.annotations.LineAnnotation(
document.pages[1],
ap.Rectangle(550, 93, 562, 439, True),
ap.Point(556, 99),
ap.Point(556, 443)
)
# Set properties
line_annotation.title = "John Smith"
line_annotation.color = ap.Color.red
line_annotation.width = 3
line_annotation.starting_style = ap.annotations.LineEnding.OPEN_ARROW
line_annotation.ending_style = ap.annotations.LineEnding.OPEN_ARROW
# Create Popup Annotation
popup = ap.annotations.PopupAnnotation(
document.pages[1],
ap.Rectangle(842, 124, 1021, 266, True)
)
line_annotation.popup = popup
# Add annotation to the page
document.pages[1].annotations.append(line_annotation)
# Save PDF document
document.save(outfile)
def navigation_buttons_add(infile, outfile):
"""Add next/previous navigation buttons to each page."""
button_config = [
("Previous Page", 120.0, ap.annotations.PredefinedAction.PREV_PAGE ),
("Next Page", 230.0, ap.annotations.PredefinedAction.NEXT_PAGE ),
]
document = ap.Document(infile)
document.pages.add() # Ensure there are at least 2 pages for navigation
# Add navigation buttons to each page
for page in document.pages:
for name, x_pos, action in button_config:
# Create button rectangle
rect = ap.Rectangle(x_pos, 10.0, x_pos + 100, 40.0, True)
button = ap.forms.ButtonField(page, rect)
button.partial_name = name
button.value = name
button.characteristics.border = ap.Color.red.to_rgb()
button.characteristics.background = ap.Color.orange.to_rgb()
# Disable button when not applicable
button.actions.on_release_mouse_btn = ap.annotations.NamedAction(action)
document.form.add(button)
document.save(outfile)
def print_button_add(infile, outfile):
"""Create a one-page PDF and add a print button."""
# Create PDF document
document = ap.Document()
# Add page
page = document.pages.add()
# Define the rectangle for the button
rect = ap.Rectangle(72, 748, 164, 768, True)
# Create a button field
print_button = ap.forms.ButtonField(page, rect)
print_button.alternate_name = "Print current document"
print_button.color = ap.Color.black
print_button.partial_name = "printBtn1"
print_button.value = "Print Document"
print_button.actions.on_release_mouse_btn = ap.annotations.NamedAction(
ap.annotations.PredefinedAction.FILE_PRINT
)
# Set the border style for the button
border = ap.annotations.Border(print_button)
border.style = ap.annotations.BorderStyle.SOLID
border.width = 2
print_button.border = border
# Set border and background color characteristics
print_button.characteristics.border = ap.Color.blue.to_rgb()
print_button.characteristics.background = ap.Color.light_blue.to_rgb()
# Add the button to the form
document.form.add(print_button)
# Save PDF document
document.save(outfile)
def run_all_examples(data_dir=None, license_path=None):
"""Run all interactive annotation 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 = [
("link_add", link_add),
("link_get", link_get),
("link_delete", link_delete),
("navigation_buttons_add", navigation_buttons_add),
("print_button_add", print_button_add),
]
for name, func in examples:
input_file_name = path.join(input_dir, "sample_n.pdf")
output_file_name = path.join(output_dir, f"{func.__name__}_out.pdf")
try:
func(input_file_name, output_file_name)
print(f"✅ Success: {name}")
except Exception as e:
print(f"❌ Failed: {name} - {str(e)}")
if __name__ == "__main__":
run_all_examples()