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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales