PDF Annotation - Add/Edit - Python Sample Code

Sample code to use Apryse Server SDK for adding or editing PDF annotations. The annotation types included in this sample are: hyperlink, intra-document link, stamp, rubber stamp, file attachment, sound, text, free-text, line, circle, square, polygon, polyline, highlight, squiggly, caret, and ink. Learn more about our Server SDK and PDF Annotation Library.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6import site
7site.addsitedir("../../../PDFNetC/Lib")
8import sys
9from PDFNetPython import *
10
11sys.path.append("../../LicenseKey/PYTHON")
12from LicenseKey import *
13
14def AnnotationHighLevelAPI(doc):
15 # The following code snippet traverses all annotations in the document
16 print("Traversing all annotations in the document...")
17 page_num = 1
18 itr = doc.GetPageIterator()
19
20 while itr.HasNext():
21 print("Page " + str(page_num) + ": ")
22 page_num = page_num + 1
23 page = itr.Current()
24 num_annots = page.GetNumAnnots()
25 i = 0
26 while i < num_annots:
27 annot = page.GetAnnot(i)
28 if not annot.IsValid():
29 continue
30 print("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName())
31
32 bbox = annot.GetRect()
33 formatter = '{0:g}'
34 print(" Position: " + formatter.format(bbox.x1) +
35 ", " + formatter.format(bbox.y1) +
36 ", " + formatter.format(bbox.x2) +
37 ", " + formatter.format(bbox.y2))
38
39 type = annot.GetType()
40
41 if type == Annot.e_Link:
42 link = Link(annot)
43 action = link.GetAction()
44 if not action.IsValid():
45 continue
46 if action.GetType() == Action.e_GoTo:
47 dest = action.GetDest()
48 if not dest.IsValid():
49 print(" Destination is not valid.")
50 else:
51 page_n = dest.GetPage().GetIndex()
52 print(" Links to: page number " + str(page_n) + " in this document")
53 elif action.GetType() == Action.e_URI:
54 uri = action.GetSDFObj().Get("URI").Value().GetAsPDFText()
55 print(" Links to: " + str(uri))
56 elif type == Annot.e_Widget:
57 pass
58 elif type == Annot.e_FileAttachment:
59 pass
60 i = i + 1
61 itr.Next()
62
63 # Use the high-level API to create new annotations.
64 first_page = doc.GetPage(1)
65
66 # Create a hyperlink...
67 hyperlink = Link.Create(doc.GetSDFDoc(), Rect(85, 570, 503, 524), Action.CreateURI(doc.GetSDFDoc(), "http://www.pdftron.com"))
68 first_page.AnnotPushBack(hyperlink)
69
70 # Create an intra-document link...
71 goto_page_3 = Action.CreateGoto(Destination.CreateFitH(doc.GetPage(3), 0))
72 link = Link.Create(doc.GetSDFDoc(), Rect(85, 458, 503, 502), goto_page_3)
73 link.SetColor(ColorPt(0, 0, 1))
74
75 # Add the new annotation to the first page
76 first_page.AnnotPushBack(link)
77
78 # Create a stamp annotation ...
79 stamp = RubberStamp.Create(doc.GetSDFDoc(), Rect(30, 30, 300, 200))
80 stamp.SetIcon("Draft")
81 first_page.AnnotPushBack(stamp)
82
83 # Create a file attachment annotation (embed the 'peppers.jpg').
84 file_attach = FileAttachment.Create(doc.GetSDFDoc(), Rect(80, 280, 108, 320), (input_path + "peppers.jpg"))
85 first_page.AnnotPushBack(file_attach)
86
87
88 ink = Ink.Create(doc.GetSDFDoc(), Rect(110, 10, 300, 200))
89 pt3 = Point(110, 10)
90 pt3.x = 110
91 pt3.y = 10
92 ink.SetPoint(0, 0, pt3)
93 pt3.x = 150
94 pt3.y = 50
95 ink.SetPoint(0, 1, pt3)
96 pt3.x = 190
97 pt3.y = 60
98 ink.SetPoint(0, 2, pt3)
99 pt3.x = 180
100 pt3.y = 90
101 ink.SetPoint(1, 0, pt3)
102 pt3.x = 190
103 pt3.y = 95
104 ink.SetPoint(1, 1, pt3)
105 pt3.x = 200
106 pt3.y = 100
107 ink.SetPoint(1, 2, pt3)
108 pt3.x = 166
109 pt3.y = 86
110 ink.SetPoint(2, 0, pt3)
111 pt3.x = 196
112 pt3.y = 96
113 ink.SetPoint(2, 1, pt3)
114 pt3.x = 221
115 pt3.y = 121
116 ink.SetPoint(2, 2, pt3)
117 pt3.x = 288
118 pt3.y = 188
119 ink.SetPoint(2, 3, pt3)
120 ink.SetColor(ColorPt(0, 1, 1), 3)
121 first_page.AnnotPushBack(ink)
122
123def AnnotationLowLevelAPI(doc):
124 itr = doc.GetPageIterator()
125 page = itr.Current()
126 annots = page.GetAnnots()
127
128 if annots == None:
129 # If there are no annotations, create a new annotation
130 # array for the page.
131 annots = doc.CreateIndirectArray()
132 page.GetSDFObj().Put("Annots", annots)
133
134 # Create a Text annotation
135 annot = doc.CreateIndirectDict()
136 annot.PutName("Subtype", "Text")
137 annot.PutBool("Open", True)
138 annot.PutString("Contents", "The quick brown fox ate the lazy mouse.")
139 annot.PutRect("Rect", 266, 116, 430, 204)
140
141 # Insert the annotation in the page annotation array
142 annots.PushBack(annot)
143
144 # Create a Link annotation
145 link1 = doc.CreateIndirectDict()
146 link1.PutName("Subtype", "Link")
147 dest = Destination.CreateFit(doc.GetPage(2))
148 link1.Put("Dest", dest.GetSDFObj())
149 link1.PutRect("Rect", 85, 705, 503, 661)
150 annots.PushBack(link1)
151
152 # Create another Link annotation
153 link2 = doc.CreateIndirectDict()
154 link2.PutName("Subtype", "Link")
155 dest2 = Destination.CreateFit((doc.GetPage(3)))
156 link2.Put("Dest", dest2.GetSDFObj())
157 link2.PutRect("Rect", 85, 638, 503, 594)
158 annots.PushBack(link2)
159
160 # Note that PDFNet APi can be used to modify existing annotations.
161 # In the following example we will modify the second link annotation
162 # (link2) so that it points to the 10th page. We also use a different
163 # destination page fit type.
164
165 # link2 = annots.GetAt(annots.Size()-1)
166 link2.Put("Dest", Destination.CreateXYZ(doc.GetPage(10), 100, 792-70, 10).GetSDFObj())
167
168 # Create a third link annotation with a hyperlink action (all other
169 # annotation types can be created in a similar way)
170 link3 = doc.CreateIndirectDict()
171 link3.PutName("Subtype", "Link")
172 link3.PutRect("Rect", 85, 570, 503, 524)
173
174 # Create a URI action
175 action = link3.PutDict("A")
176 action.PutName("S", "URI")
177 action.PutString("URI", "http://www.pdftron.com")
178
179 annots.PushBack(link3)
180
181def CreateTestAnnots(doc):
182 ew = ElementWriter()
183 eb = ElementBuilder()
184
185 first_page = doc.PageCreate(Rect(0, 0, 600, 600))
186 doc.PagePushBack(first_page)
187 ew.Begin(first_page, ElementWriter.e_overlay, False ) # begin writing to this page
188 ew.End() # save changes to the current page
189
190 # Test of a free text annotation.
191 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect(10, 400, 160, 570) )
192 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out " +
193 "of the air by freezing it with an angry glare." +
194 "\n\nAha!\n\nAnd there was much rejoicing!" )
195 txtannot.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 1, 10, 20 ), False )
196 txtannot.SetQuaddingFormat(0)
197 first_page.AnnotPushBack(txtannot)
198 txtannot.RefreshAppearance()
199
200 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect(100, 100, 350, 500) )
201 txtannot.SetContentRect( Rect( 200, 200, 350, 500 ) )
202 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air "
203 "by freezing it with an angry glare."
204 "\n\nAha!\n\nAnd there was much rejoicing!" )
205 txtannot.SetCalloutLinePoints( Point(200,300), Point(150,290), Point(110,110) )
206 txtannot.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 1, 10, 20 ), False )
207 txtannot.SetEndingStyle( LineAnnot.e_ClosedArrow )
208 txtannot.SetColor( ColorPt( 0, 1, 0 ) )
209 txtannot.SetQuaddingFormat(1)
210 first_page.AnnotPushBack(txtannot)
211 txtannot.RefreshAppearance()
212
213 txtannot = FreeText.Create( doc.GetSDFDoc(), Rect(400, 10, 550, 400) )
214 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air "
215 "by freezing it with an angry glare."
216 "\n\nAha!\n\nAnd there was much rejoicing!" )
217 txtannot.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 1, 10, 20 ), False )
218 txtannot.SetColor( ColorPt( 0, 0, 1 ) )
219 txtannot.SetOpacity( 0.2 )
220 txtannot.SetQuaddingFormat(2)
221 first_page.AnnotPushBack(txtannot)
222 txtannot.RefreshAppearance()
223
224 page= doc.PageCreate(Rect(0, 0, 600, 600))
225 doc.PagePushBack(page)
226 ew.Begin(page, ElementWriter.e_overlay, False ) # begin writing to this page
227 eb.Reset() # Reset the GState to default
228 ew.End() # save changes to the current page
229
230 # Create a Line annotation...
231 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(250, 250, 400, 400))
232 line.SetStartPoint( Point(350, 270 ) )
233 line.SetEndPoint( Point(260,370) )
234 line.SetStartStyle(LineAnnot.e_Square)
235 line.SetEndStyle(LineAnnot.e_Circle)
236 line.SetColor(ColorPt(.3, .5, 0), 3)
237 line.SetContents( "Dashed Captioned" )
238 line.SetShowCaption(True)
239 line.SetCaptionPosition( LineAnnot.e_Top )
240 line.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 2, 0, 0, [2.0, 2.0] ) )
241 line.RefreshAppearance()
242 page.AnnotPushBack(line)
243
244 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(347, 377, 600, 600))
245 line.SetStartPoint( Point(385, 410 ) )
246 line.SetEndPoint( Point(540,555) )
247 line.SetStartStyle(LineAnnot.e_Circle)
248 line.SetEndStyle(LineAnnot.e_OpenArrow)
249 line.SetColor(ColorPt(1, 0, 0), 3)
250 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
251 line.SetContents( "Inline Caption" )
252 line.SetShowCaption(True)
253 line.SetCaptionPosition( LineAnnot.e_Inline )
254 line.SetLeaderLineExtensionLength( 4. )
255 line.SetLeaderLineLength( -12. )
256 line.SetLeaderLineOffset( 2. )
257 line.RefreshAppearance()
258 page.AnnotPushBack(line)
259
260 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(10, 400, 200, 600))
261 line.SetStartPoint( Point(25, 426 ) )
262 line.SetEndPoint( Point(180,555) )
263 line.SetStartStyle(LineAnnot.e_Circle)
264 line.SetEndStyle(LineAnnot.e_Square)
265 line.SetColor(ColorPt(0, 0, 1), 3)
266 line.SetInteriorColor(ColorPt(1, 0, 0), 3)
267 line.SetContents("Offset Caption")
268 line.SetShowCaption(True)
269 line.SetCaptionPosition( LineAnnot.e_Top )
270 line.SetTextHOffset( -60 )
271 line.SetTextVOffset( 10 )
272 line.RefreshAppearance()
273 page.AnnotPushBack(line)
274
275 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(200, 10, 400, 70))
276 line.SetStartPoint( Point(220, 25 ) )
277 line.SetEndPoint( Point(370,60) )
278 line.SetStartStyle(LineAnnot.e_Butt)
279 line.SetEndStyle(LineAnnot.e_OpenArrow)
280 line.SetColor(ColorPt(0, 0, 1), 3)
281 line.SetContents( "Regular Caption" )
282 line.SetShowCaption(True)
283 line.SetCaptionPosition( LineAnnot.e_Top )
284 line.RefreshAppearance()
285 page.AnnotPushBack(line)
286
287 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(200, 70, 400, 130))
288 line.SetStartPoint( Point(220, 111 ) )
289 line.SetEndPoint( Point(370,78) )
290 line.SetStartStyle(LineAnnot.e_Circle)
291 line.SetEndStyle(LineAnnot.e_Diamond)
292 line.SetContents( "Circle to Diamond" )
293 line.SetColor(ColorPt(0, 0, 1), 3)
294 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
295 line.SetShowCaption(True)
296 line.SetCaptionPosition( LineAnnot.e_Top )
297 line.RefreshAppearance()
298 page.AnnotPushBack(line)
299
300 line=LineAnnot.Create(doc.GetSDFDoc(), Rect(10, 100, 160, 200))
301 line.SetStartPoint( Point(15, 110 ) )
302 line.SetEndPoint( Point(150, 190) )
303 line.SetStartStyle(LineAnnot.e_Slash)
304 line.SetEndStyle(LineAnnot.e_ClosedArrow)
305 line.SetContents( "Slash to CArrow" )
306 line.SetColor(ColorPt(1, 0, 0), 3)
307 line.SetInteriorColor(ColorPt(0, 1, 1), 3)
308 line.SetShowCaption(True)
309 line.SetCaptionPosition( LineAnnot.e_Top )
310 line.RefreshAppearance()
311 page.AnnotPushBack(line)
312
313 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 270, 270, 570, 433 ))
314 line.SetStartPoint( Point(300, 400 ) )
315 line.SetEndPoint( Point(550, 300) )
316 line.SetStartStyle(LineAnnot.e_RClosedArrow)
317 line.SetEndStyle(LineAnnot.e_ROpenArrow)
318 line.SetContents( "ROpen & RClosed arrows" )
319 line.SetColor(ColorPt(0, 0, 1), 3)
320 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
321 line.SetShowCaption(True)
322 line.SetCaptionPosition( LineAnnot.e_Top )
323 line.RefreshAppearance()
324 page.AnnotPushBack(line)
325
326 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 195, 395, 205, 505 ))
327 line.SetStartPoint( Point(200, 400 ) )
328 line.SetEndPoint( Point(200, 500) )
329 line.RefreshAppearance()
330 page.AnnotPushBack(line)
331
332 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 55, 299, 150, 301 ))
333 line.SetStartPoint( Point(55, 300 ) )
334 line.SetEndPoint( Point(155, 300) )
335 line.SetStartStyle(LineAnnot.e_Circle)
336 line.SetEndStyle(LineAnnot.e_Circle)
337 line.SetContents( "Caption that's longer than its line." )
338 line.SetColor(ColorPt(1, 0, 1), 3)
339 line.SetInteriorColor(ColorPt(0, 1, 0), 3)
340 line.SetShowCaption(True)
341 line.SetCaptionPosition( LineAnnot.e_Top )
342 line.RefreshAppearance()
343 page.AnnotPushBack(line)
344
345 line=LineAnnot.Create(doc.GetSDFDoc(), Rect( 300, 200, 390, 234 ))
346 line.SetStartPoint( Point(310, 210 ) )
347 line.SetEndPoint( Point(380, 220) )
348 line.SetColor(ColorPt(0, 0, 0), 3)
349 line.RefreshAppearance()
350 page.AnnotPushBack(line)
351
352 page3 = doc.PageCreate(Rect(0, 0, 600, 600))
353 ew.Begin(page3) # begin writing to the page
354 ew.End() # save changes to the current page
355 doc.PagePushBack(page3)
356
357 circle=Circle.Create(doc.GetSDFDoc(), Rect( 300, 300, 390, 350 ))
358 circle.SetColor(ColorPt(0, 0, 0), 3)
359 circle.RefreshAppearance()
360 page3.AnnotPushBack(circle)
361
362 circle=Circle.Create(doc.GetSDFDoc(), Rect( 100, 100, 200, 200 ))
363 circle.SetColor(ColorPt(0, 1, 0), 3)
364 circle.SetInteriorColor(ColorPt(0, 0, 1), 3)
365 circle.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 3, 0, 0, [2, 4] ) )
366 circle.SetPadding( 2 )
367 circle.RefreshAppearance()
368 page3.AnnotPushBack(circle)
369
370 sq = Square.Create( doc.GetSDFDoc(), Rect(10,200, 80, 300 ) )
371 sq.SetColor(ColorPt(0, 0, 0), 3)
372 sq.RefreshAppearance()
373 page3.AnnotPushBack( sq )
374
375 sq = Square.Create( doc.GetSDFDoc(), Rect(500,200, 580, 300 ) )
376 sq.SetColor(ColorPt(1, 0, 0), 3)
377 sq.SetInteriorColor(ColorPt(0, 1, 1), 3)
378 sq.SetBorderStyle( BorderStyle( BorderStyle.e_dashed, 6, 0, 0, [4, 2] ) )
379 sq.SetPadding( 4 )
380 sq.RefreshAppearance()
381 page3.AnnotPushBack( sq )
382
383 poly = Polygon.Create(doc.GetSDFDoc(), Rect(5, 500, 125, 590))
384 poly.SetColor(ColorPt(1, 0, 0), 3)
385 poly.SetInteriorColor(ColorPt(1, 1, 0), 3)
386 poly.SetVertex(0, Point(12,510) )
387 poly.SetVertex(1, Point(100,510) )
388 poly.SetVertex(2, Point(100,555) )
389 poly.SetVertex(3, Point(35,544) )
390 poly.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 4, 0, 0 ) )
391 poly.SetPadding( 4 )
392 poly.RefreshAppearance()
393 page3.AnnotPushBack( poly )
394
395 poly = PolyLine.Create(doc.GetSDFDoc(), Rect(400, 10, 500, 90))
396 poly.SetColor(ColorPt(1, 0, 0), 3)
397 poly.SetInteriorColor(ColorPt(0, 1, 0), 3)
398 poly.SetVertex(0, Point(405,20) )
399 poly.SetVertex(1, Point(440,40) )
400 poly.SetVertex(2, Point(410,60) )
401 poly.SetVertex(3, Point(470,80) )
402 poly.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 2, 0, 0 ) )
403 poly.SetPadding( 4 )
404 poly.SetStartStyle( LineAnnot.e_RClosedArrow )
405 poly.SetEndStyle( LineAnnot.e_ClosedArrow )
406 poly.RefreshAppearance()
407 page3.AnnotPushBack( poly )
408
409 lk = Link.Create( doc.GetSDFDoc(), Rect(5,5,55,24) )
410 lk.RefreshAppearance()
411 page3.AnnotPushBack( lk )
412
413 page4 = doc.PageCreate(Rect(0, 0, 600, 600))
414 ew.Begin(page4) # begin writing to the page
415 ew.End() # save changes to the current page
416 doc.PagePushBack(page4)
417
418 ew.Begin( page4 )
419 font = Font.Create(doc.GetSDFDoc(), Font.e_helvetica)
420 element = eb.CreateTextBegin( font, 16 )
421 element.SetPathFill(True)
422 ew.WriteElement(element)
423 element = eb.CreateTextRun( "Some random text on the page", font, 16 )
424 element.SetTextMatrix(1, 0, 0, 1, 100, 500 )
425 ew.WriteElement(element)
426 ew.WriteElement( eb.CreateTextEnd() )
427 ew.End()
428
429 hl = HighlightAnnot.Create( doc.GetSDFDoc(), Rect(100,490,150,515) )
430 hl.SetColor( ColorPt(0,1,0), 3 )
431 hl.RefreshAppearance()
432 page4.AnnotPushBack( hl )
433
434 sq = Squiggly.Create( doc.GetSDFDoc(), Rect(100,450,250,600) )
435 sq.SetQuadPoint( 0, QuadPoint( Point( 122,455), Point(240, 545), Point(230, 595), Point(101,500 ) ) )
436 sq.RefreshAppearance()
437 page4.AnnotPushBack( sq )
438
439 cr = Caret.Create( doc.GetSDFDoc(), Rect(100,40,129,69) )
440 cr.SetColor( ColorPt(0,0,1), 3 )
441 cr.SetSymbol( "P" )
442 cr.RefreshAppearance()
443 page4.AnnotPushBack( cr )
444
445 page5 = doc.PageCreate(Rect(0, 0, 600, 600))
446 ew.Begin(page5) # begin writing to the page
447 ew.End() # save changes to the current page
448 doc.PagePushBack(page5)
449 fs = FileSpec.Create( doc.GetSDFDoc(), (input_path + "butterfly.png"), False )
450 page6 = doc.PageCreate(Rect(0, 0, 600, 600))
451 ew.Begin(page6) # begin writing to the page
452 ew.End() # save changes to the current page
453 doc.PagePushBack(page6)
454
455
456 txt = Text.Create( doc.GetSDFDoc(), Point(10, 20) )
457 txt.SetIcon( "UserIcon" )
458 txt.SetContents( "User defined icon, unrecognized by appearance generator" )
459 txt.SetColor( ColorPt(0,1,0) )
460 txt.RefreshAppearance()
461 page6.AnnotPushBack( txt )
462
463 ink = Ink.Create( doc.GetSDFDoc(), Rect( 100, 400, 200, 550 ) )
464 ink.SetColor( ColorPt(0,0,1) )
465 ink.SetPoint( 1, 3, Point( 220, 505) )
466 ink.SetPoint( 1, 0, Point( 100, 490) )
467 ink.SetPoint( 0, 1, Point( 120, 410) )
468 ink.SetPoint( 0, 0, Point( 100, 400) )
469 ink.SetPoint( 1, 2, Point( 180, 490) )
470 ink.SetPoint( 1, 1, Point( 140, 440) )
471 ink.SetBorderStyle( BorderStyle( BorderStyle.e_solid, 3, 0, 0 ) )
472 ink.RefreshAppearance()
473 page6.AnnotPushBack( ink )
474
475 page7 = doc.PageCreate(Rect(0, 0, 600, 600))
476 ew.Begin(page7) # begin writing to the page
477 ew.End() # save changes to the current page
478 doc.PagePushBack(page7)
479
480 snd = Sound.Create( doc.GetSDFDoc(), Rect( 100, 500, 120, 520 ) )
481 snd.SetColor( ColorPt(1,1,0) )
482 snd.SetIcon( Sound.e_Speaker )
483 snd.RefreshAppearance()
484 page7.AnnotPushBack( snd )
485
486 snd = Sound.Create( doc.GetSDFDoc(), Rect( 200, 500, 220, 520 ) )
487 snd.SetColor( ColorPt(1,1,0) )
488 snd.SetIcon( Sound.e_Mic )
489 snd.RefreshAppearance()
490 page7.AnnotPushBack( snd )
491
492 page8 = doc.PageCreate(Rect(0, 0, 600, 600))
493 ew.Begin(page8) # begin writing to the page
494 ew.End() # save changes to the current page
495 doc.PagePushBack(page8)
496
497 ipage = 0
498 while ipage<2:
499 px = 5
500 py = 520
501 istamp = RubberStamp.e_Approved
502 while istamp <= RubberStamp.e_Draft:
503 st = RubberStamp.Create(doc.GetSDFDoc(), Rect(1,1,100,100))
504 st.SetIcon( istamp )
505 st.SetContents( st.GetIconName() )
506 st.SetRect( Rect(px, py, px+100, py+25 ) )
507 py -= 100
508 if py < 0:
509 py = 520
510 px+=200
511 if ipage == 0:
512 #page7.AnnotPushBack(st)
513 pass
514 else:
515 page8.AnnotPushBack( st )
516 st.RefreshAppearance()
517 istamp = istamp + 1
518 ipage = ipage + 1
519
520 st = RubberStamp.Create( doc.GetSDFDoc(), Rect(400,5,550,45) )
521 st.SetIcon( "UserStamp" )
522 st.SetContents( "User defined stamp" )
523 page8.AnnotPushBack( st )
524 st.RefreshAppearance()
525
526if __name__ == '__main__':
527 PDFNet.Initialize(LicenseKey)
528
529 output_path = "../../TestFiles/Output/"
530 input_path = "../../TestFiles/"
531
532 doc = PDFDoc(input_path + "numbered.pdf")
533 doc.InitSecurityHandler()
534
535 # An example of using SDF/Cos API to add any type of annotations.
536 AnnotationLowLevelAPI(doc)
537 doc.Save(output_path + "annotation_test1.pdf", SDFDoc.e_remove_unused)
538 print("Done. Results saved in annotation_test1.pdf")
539
540 # An example of using the high-level PDFNet API to read existing annotations,
541 # to edit existing annotations, and to create new annotation from scratch.
542 AnnotationHighLevelAPI(doc)
543 doc.Save((output_path + "annotation_test2.pdf"), SDFDoc.e_linearized)
544 doc.Close()
545 print("Done. Results saved in annotation_test2.pdf")
546
547 doc1 = PDFDoc()
548 CreateTestAnnots(doc1)
549 outfname = output_path + "new_annot_test_api.pdf"
550 doc1.Save(outfname, SDFDoc.e_linearized)
551 print("Saved new_annot_test_api.pdf")
552 PDFNet.Terminate()

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales