PDF Annotation - Add/Edit - Go 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-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 "fmt"
9 "strconv"
10 . "pdftron"
11)
12
13import "pdftron/Samples/LicenseKey/GO"
14
15// Relative path to the folder containing the test files.
16var inputPath = "../../TestFiles/"
17var outputPath = "../../TestFiles/Output/"
18
19func AnnotationLowLevelAPI(doc PDFDoc){
20 itr := doc.GetPageIterator()
21 page := itr.Current()
22 annots := page.GetAnnots()
23 if (page.GetNumAnnots() == 0){
24 // If there are no annotations, create a new annotation
25 // array for the page.
26 annots = doc.CreateIndirectArray()
27 page.GetSDFObj().Put("Annots", annots)
28 }
29
30 // Create a Text annotation
31 annot := doc.CreateIndirectDict()
32 annot.PutName("Subtype", "Text")
33 annot.PutBool("Open", true)
34 annot.PutString("Contents", "The quick brown fox ate the lazy mouse.")
35 annot.PutRect("Rect", 266.0, 116.0, 430.0, 204.0)
36
37 // Insert the annotation in the page annotation array
38 annots.PushBack(annot)
39
40 // Create a Link annotation
41 link1 := doc.CreateIndirectDict()
42 link1.PutName("Subtype", "Link")
43 dest := DestinationCreateFit(doc.GetPage(2))
44 link1.Put("Dest", dest.GetSDFObj())
45 link1.PutRect("Rect", 85.0, 705.0, 503.0, 661.0)
46 annots.PushBack(link1)
47
48 // Create another Link annotation
49 link2 := doc.CreateIndirectDict()
50 link2.PutName("Subtype", "Link")
51 dest2 := DestinationCreateFit((doc.GetPage(3)))
52 link2.Put("Dest", dest2.GetSDFObj())
53 link2.PutRect("Rect", 85.0, 638.0, 503.0, 594.0)
54 annots.PushBack(link2)
55
56 // Note that PDFNet APi can be used to modify existing annotations.
57 // In the following example we will modify the second link annotation
58 // (link2) so that it points to the 10th page. We also use a different
59 // destination page fit type.
60
61 // link2 = annots.GetAt(annots.Size()-1)
62 link2.Put("Dest", DestinationCreateXYZ(doc.GetPage(10), 100, 792-70, 10).GetSDFObj())
63
64 // Create a third link annotation with a hyperlink action (all other
65 // annotation types can be created in a similar way)
66 link3 := doc.CreateIndirectDict()
67 link3.PutName("Subtype", "Link")
68 link3.PutRect("Rect", 85.0, 570.0, 503.0, 524.0)
69
70 // Create a URI action
71 action := link3.PutDict("A")
72 action.PutName("S", "URI")
73 action.PutString("URI", "http://www.pdftron.com")
74
75 annots.PushBack(link3)
76}
77
78func AnnotationHighLevelAPI(doc PDFDoc){
79 // The following code snippet traverses all annotations in the document
80 fmt.Println("Traversing all annotations in the document...")
81 pageNum := 1
82 itr := doc.GetPageIterator()
83
84 for itr.HasNext(){
85 fmt.Println("Page " + strconv.Itoa(pageNum) + ": ")
86 pageNum = pageNum + 1
87 page := itr.Current()
88 numAnnots := page.GetNumAnnots()
89 i := uint(0)
90 for i < numAnnots{
91 annot := page.GetAnnot(i)
92 if (!annot.IsValid()){
93 continue
94 }
95 fmt.Println("Annot Type: " + annot.GetSDFObj().Get("Subtype").Value().GetName())
96
97 bbox := annot.GetRect()
98 fmt.Println(" Position: " + fmt.Sprintf("%.0f", bbox.GetX1()) +
99 ", " + fmt.Sprintf("%.0f", bbox.GetY1()) +
100 ", " + fmt.Sprintf("%.0f", bbox.GetX2()) +
101 ", " + fmt.Sprintf("%.0f", bbox.GetY2()))
102
103 atype := annot.GetType()
104
105 if (atype == AnnotE_Link){
106 link := NewLink(annot)
107 action := link.GetAction()
108 if (!action.IsValid()){
109 continue
110 }
111 if (action.GetType() == ActionE_GoTo){
112 dest := action.GetDest()
113 if (!dest.IsValid()){
114 fmt.Println(" Destination is not valid.")
115 }else{
116 pageN := dest.GetPage().GetIndex()
117 fmt.Println(" Links to: page number " + strconv.Itoa(pageN) + " in this document")
118 }
119 }else if (action.GetType() == ActionE_URI){
120 uri := action.GetSDFObj().Get("URI").Value().GetAsPDFText()
121 fmt.Println(" Links to: " + uri)
122 }
123 }else if (atype == AnnotE_Widget){
124 //handle Widget here
125 }else if (atype == AnnotE_FileAttachment){
126 //handle FileAttachment here
127 }
128 i = i + 1
129 }
130 itr.Next()
131 }
132 // Use the high-level API to create new annotations.
133 firstPage := doc.GetPage(1)
134
135 // Create a hyperlink...
136 hyperlink := LinkCreate(doc.GetSDFDoc(), NewRect(85.0, 570.0, 503.0, 524.0), ActionCreateURI(doc.GetSDFDoc(), "http://www.pdftron.com"))
137 firstPage.AnnotPushBack(hyperlink)
138
139 // Create an intra-document link...
140 gotoPage3 := ActionCreateGoto(DestinationCreateFitH(doc.GetPage(3), 0))
141 link := LinkCreate(doc.GetSDFDoc(), NewRect(85.0, 458.0, 503.0, 502.0), gotoPage3)
142 link.SetColor(NewColorPt(0.0, 0.0, 1.0))
143
144 // Add the new annotation to the first page
145 firstPage.AnnotPushBack(link)
146
147 // Create a stamp annotation ...
148 stamp := RubberStampCreate(doc.GetSDFDoc(), NewRect(30.0, 30.0, 300.0, 200.0))
149 stamp.SetIcon("Draft")
150 firstPage.AnnotPushBack(stamp)
151
152 // Create a file attachment annotation (embed the 'peppers.jpg').
153 file_attach := FileAttachmentCreate(doc.GetSDFDoc(), NewRect(80.0, 280.0, 108.0, 320.0), (inputPath + "peppers.jpg"))
154 firstPage.AnnotPushBack(file_attach)
155
156
157 ink := InkCreate(doc.GetSDFDoc(), NewRect(110.0, 10.0, 300.0, 200.0))
158 pt3 := NewPoint(110.0, 10.0)
159 pt3.SetX(110)
160 pt3.SetY(10)
161 ink.SetPoint(0, 0, pt3)
162 pt3.SetX(150)
163 pt3.SetY(50)
164 ink.SetPoint(0, 1, pt3)
165 pt3.SetX(190)
166 pt3.SetY(60)
167 ink.SetPoint(0, 2, pt3)
168 pt3.SetX(180)
169 pt3.SetY(90)
170 ink.SetPoint(1, 0, pt3)
171 pt3.SetX(190)
172 pt3.SetY(95)
173 ink.SetPoint(1, 1, pt3)
174 pt3.SetX(200)
175 pt3.SetY(100)
176 ink.SetPoint(1, 2, pt3)
177 pt3.SetX(166)
178 pt3.SetY(86)
179 ink.SetPoint(2, 0, pt3)
180 pt3.SetX(196)
181 pt3.SetY(96)
182 ink.SetPoint(2, 1, pt3)
183 pt3.SetX(221)
184 pt3.SetY(121)
185 ink.SetPoint(2, 2, pt3)
186 pt3.SetX(288)
187 pt3.SetY(188)
188 ink.SetPoint(2, 3, pt3)
189 ink.SetColor(NewColorPt(0.0, 1.0, 1.0), 3)
190 firstPage.AnnotPushBack(ink)
191}
192
193func CreateTestAnnots(doc PDFDoc){
194 ew := NewElementWriter()
195 eb := NewElementBuilder()
196
197 firstPage := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
198 doc.PagePushBack(firstPage)
199 ew.Begin(firstPage, ElementWriterE_overlay, false ) // begin writing to this page
200 ew.End() // save changes to the current page
201
202 // Test of a free text annotation.
203 txtannot := FreeTextCreate( doc.GetSDFDoc(), NewRect(10.0, 400.0, 160.0, 570.0) )
204 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out " +
205 "of the air by freezing it with an angry glare." +
206 "\n\nAha!\n\nAnd there was much rejoicing!" )
207 txtannot.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 1.0, 10.0, 20.0 ), false )
208 txtannot.SetQuaddingFormat(0)
209 firstPage.AnnotPushBack(txtannot)
210 txtannot.RefreshAppearance()
211
212 txtannot = FreeTextCreate( doc.GetSDFDoc(), NewRect(100.0, 100.0, 350.0, 500.0) )
213 txtannot.SetContentRect( NewRect(200.0, 200.0, 350.0, 500.0 ) )
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.SetCalloutLinePoints( NewPoint(200.0,300.0), NewPoint(150.0,290.0), NewPoint(110.0,110.0) )
218 txtannot.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 1.0, 10.0, 20.0 ), false )
219 txtannot.SetEndingStyle( LineAnnotE_ClosedArrow )
220 txtannot.SetColor( NewColorPt( 0.0, 1.0, 0.0 ) )
221 txtannot.SetQuaddingFormat(1)
222 firstPage.AnnotPushBack(txtannot)
223 txtannot.RefreshAppearance()
224
225 txtannot = FreeTextCreate( doc.GetSDFDoc(), NewRect(400.0, 10.0, 550.0, 400.0) )
226 txtannot.SetContents( "\n\nSome swift brown fox snatched a gray hare out of the air " +
227 "by freezing it with an angry glare." +
228 "\n\nAha!\n\nAnd there was much rejoicing!" )
229 txtannot.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 1.0, 10.0, 20.0 ), false )
230 txtannot.SetColor( NewColorPt( 0.0, 0.0, 1.0 ) )
231 txtannot.SetOpacity( 0.2 )
232 txtannot.SetQuaddingFormat(2)
233 firstPage.AnnotPushBack(txtannot)
234 txtannot.RefreshAppearance()
235
236 page := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
237 doc.PagePushBack(page)
238 ew.Begin(page, ElementWriterE_overlay, false ) // begin writing to this page
239 eb.Reset() // Reset the GState to default
240 ew.End() // save changes to the current page
241
242 // Create a Line annotation...
243 line := LineAnnotCreate(doc.GetSDFDoc(), NewRect(250.0, 250.0, 400.0, 400.0))
244 line.SetStartPoint( NewPoint(350.0, 270.0 ) )
245 line.SetEndPoint( NewPoint(260.0,370.0) )
246 line.SetStartStyle(LineAnnotE_Square)
247 line.SetEndStyle(LineAnnotE_Circle)
248 line.SetColor(NewColorPt(.3, .5, 0.0), 3)
249 line.SetContents( "Dashed Captioned" )
250 line.SetShowCaption(true)
251 line.SetCaptionPosition( &LineAnnotE_Top )
252 var dash = NewVectorDouble()
253 dash.Add(2.0)
254 dash.Add(2.0)
255 line.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 2.0, 0.0, 0.0, dash ) )
256 dash.Clear()
257 line.RefreshAppearance()
258 page.AnnotPushBack(line)
259
260 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(347.0, 377.0, 600.0, 600.0))
261 line.SetStartPoint( NewPoint(385.0, 410.0 ) )
262 line.SetEndPoint( NewPoint(540.0,555.0) )
263 line.SetStartStyle(LineAnnotE_Circle)
264 line.SetEndStyle(LineAnnotE_OpenArrow)
265 line.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
266 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
267 line.SetContents( "Inline Caption" )
268 line.SetShowCaption(true)
269 line.SetCaptionPosition( &LineAnnotE_Inline )
270 line.SetLeaderLineExtensionLength( -4. )
271 line.SetLeaderLineLength( -12. )
272 line.SetLeaderLineOffset( 2. )
273 line.RefreshAppearance()
274 page.AnnotPushBack(line)
275
276 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(10.0, 400.0, 200.0, 600.0))
277 line.SetStartPoint( NewPoint(25.0, 426.0 ) )
278 line.SetEndPoint( NewPoint(180.0,555.0) )
279 line.SetStartStyle(LineAnnotE_Circle)
280 line.SetEndStyle(LineAnnotE_Square)
281 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
282 line.SetInteriorColor(NewColorPt(1.0, 0.0, 0.0), 3)
283 line.SetContents("Offset Caption")
284 line.SetShowCaption(true)
285 line.SetCaptionPosition( &LineAnnotE_Top )
286 line.SetTextHOffset( -60 )
287 line.SetTextVOffset( 10 )
288 line.RefreshAppearance()
289 page.AnnotPushBack(line)
290
291 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(200.0, 10.0, 400.0, 70.0))
292 line.SetStartPoint( NewPoint(222.0, 25.0 ) )
293 line.SetEndPoint( NewPoint(370.0,60.0) )
294 line.SetStartStyle(LineAnnotE_Butt)
295 line.SetEndStyle(LineAnnotE_OpenArrow)
296 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
297 line.SetContents( "Regular Caption" )
298 line.SetShowCaption(true)
299 line.SetCaptionPosition( &LineAnnotE_Top )
300 line.RefreshAppearance()
301 page.AnnotPushBack(line)
302
303 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(200.0, 70.0, 400.0, 130.0))
304 line.SetStartPoint( NewPoint(220.0, 111.0 ) )
305 line.SetEndPoint( NewPoint(370.0,78.0) )
306 line.SetStartStyle(LineAnnotE_Circle)
307 line.SetEndStyle(LineAnnotE_Diamond)
308 line.SetContents( "Circle to Diamond" )
309 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
310 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
311 line.SetShowCaption(true)
312 line.SetCaptionPosition( &LineAnnotE_Top )
313 line.RefreshAppearance()
314 page.AnnotPushBack(line)
315
316 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(10.0, 100.0, 160.0, 200.0))
317 line.SetStartPoint( NewPoint(15.0, 110.0 ) )
318 line.SetEndPoint( NewPoint(150.0, 190.0) )
319 line.SetStartStyle(LineAnnotE_Slash)
320 line.SetEndStyle(LineAnnotE_ClosedArrow)
321 line.SetContents( "Slash to CArrow" )
322 line.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
323 line.SetInteriorColor(NewColorPt(0.0, 1.0, 1.0), 3)
324 line.SetShowCaption(true)
325 line.SetCaptionPosition( &LineAnnotE_Top )
326 line.RefreshAppearance()
327 page.AnnotPushBack(line)
328
329 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(270.0, 270.0, 570.0, 433.0 ))
330 line.SetStartPoint( NewPoint(300.0, 400.0 ) )
331 line.SetEndPoint( NewPoint(550.0, 300.0) )
332 line.SetStartStyle(LineAnnotE_RClosedArrow)
333 line.SetEndStyle(LineAnnotE_ROpenArrow)
334 line.SetContents( "ROpen & RClosed arrows" )
335 line.SetColor(NewColorPt(0.0, 0.0, 1.0), 3)
336 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
337 line.SetShowCaption(true)
338 line.SetCaptionPosition( &LineAnnotE_Top )
339 line.RefreshAppearance()
340 page.AnnotPushBack(line)
341
342 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(195.0, 395.0, 205.0, 505.0 ))
343 line.SetStartPoint( NewPoint(200.0, 400.0 ) )
344 line.SetEndPoint( NewPoint(200.0, 500.0) )
345 line.RefreshAppearance()
346 page.AnnotPushBack(line)
347
348 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(55.0, 299.0, 150.0, 301.0 ))
349 line.SetStartPoint( NewPoint(55.0, 300.0 ) )
350 line.SetEndPoint( NewPoint(155.0, 300.0) )
351 line.SetStartStyle(LineAnnotE_Circle)
352 line.SetEndStyle(LineAnnotE_Circle)
353 line.SetContents( "Caption that's longer than its line." )
354 line.SetColor(NewColorPt(1.0, 0.0, 1.0), 3)
355 line.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
356 line.SetShowCaption(true)
357 line.SetCaptionPosition( &LineAnnotE_Top )
358 line.RefreshAppearance()
359 page.AnnotPushBack(line)
360
361 line = LineAnnotCreate(doc.GetSDFDoc(), NewRect(300.0, 200.0, 390.0, 234.0 ))
362 line.SetStartPoint( NewPoint(310.0, 210.0 ) )
363 line.SetEndPoint( NewPoint(380.0, 220.0) )
364 line.SetColor(NewColorPt(0.0, 0.0, 0.0), 3)
365 line.RefreshAppearance()
366 page.AnnotPushBack(line)
367
368 page3 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
369 ew.Begin(page3) // begin writing to the page
370 ew.End() // save changes to the current page
371 doc.PagePushBack(page3)
372
373 circle := CircleCreate(doc.GetSDFDoc(), NewRect(300.0, 300.0, 390.0, 350.0 ))
374 circle.SetColor(NewColorPt(0.0, 0.0, 0.0), 3)
375 circle.RefreshAppearance()
376 page3.AnnotPushBack(circle)
377
378 circle = CircleCreate(doc.GetSDFDoc(), NewRect(100.0, 100.0, 200.0, 200.0 ))
379 circle.SetColor(NewColorPt(0.0, 1.0, 0.0), 3)
380 circle.SetInteriorColor(NewColorPt(0.0, 0.0, 1.0), 3)
381 dash.Add(2.0)
382 dash.Add(4.0)
383 circle.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 3.0, 0.0, 0.0, dash ) )
384 dash.Clear()
385 circle.SetPadding( 2.0 )
386 circle.RefreshAppearance()
387 page3.AnnotPushBack(circle)
388
389 sq := SquareCreate( doc.GetSDFDoc(), NewRect(10.0, 200.0, 80.0, 300.0 ) )
390 sq.SetColor(NewColorPt(0.0, 0.0, 0.0), 3)
391 sq.RefreshAppearance()
392 page3.AnnotPushBack( sq )
393
394 sq = SquareCreate( doc.GetSDFDoc(), NewRect(500.0, 200.0, 580.0, 300.0 ) )
395 sq.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
396 sq.SetInteriorColor(NewColorPt(0.0, 1.0, 1.0), 3)
397 dash.Add(4.0)
398 dash.Add(2.0)
399 sq.SetBorderStyle( NewBorderStyle( BorderStyleE_dashed, 6.0, 0.0, 0.0, dash ) )
400 dash.Clear()
401 sq.SetPadding( 4.0 )
402 sq.RefreshAppearance()
403 page3.AnnotPushBack( sq )
404
405 polygon := PolygonCreate(doc.GetSDFDoc(), NewRect(5.0, 500.0, 125.0, 590.0))
406 polygon.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
407 polygon.SetInteriorColor(NewColorPt(1.0, 1.0, 0.0), 3)
408 polygon.SetVertex(0, NewPoint(12.0,510.0) )
409 polygon.SetVertex(1, NewPoint(100.0,510.0) )
410 polygon.SetVertex(2, NewPoint(100.0,555.0) )
411 polygon.SetVertex(3, NewPoint(35.0,544.0) )
412 polygon.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 4.0, 0.0, 0.0 ) )
413 polygon.SetPadding( 4.0 )
414 polygon.RefreshAppearance()
415 page3.AnnotPushBack( polygon )
416 polyline := PolyLineCreate(doc.GetSDFDoc(), NewRect(400.0, 10.0, 500.0, 90.0))
417 polyline.SetColor(NewColorPt(1.0, 0.0, 0.0), 3)
418 polyline.SetInteriorColor(NewColorPt(0.0, 1.0, 0.0), 3)
419 polyline.SetVertex(0, NewPoint(405.0,20.0) )
420 polyline.SetVertex(1, NewPoint(440.0,40.0) )
421 polyline.SetVertex(2, NewPoint(410.0,60.0) )
422 polyline.SetVertex(3, NewPoint(470.0,80.0) )
423 polyline.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 2.0, 0.0, 0.0 ) )
424 polyline.SetPadding( 4.0 )
425 polyline.SetStartStyle( LineAnnotE_RClosedArrow )
426 polyline.SetEndStyle( LineAnnotE_ClosedArrow )
427 polyline.RefreshAppearance()
428 page3.AnnotPushBack( polyline )
429 lk := LinkCreate( doc.GetSDFDoc(), NewRect(5.0, 5.0, 55.0, 24.0) )
430 lk.RefreshAppearance()
431 page3.AnnotPushBack( lk )
432
433 page4 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
434 ew.Begin(page4) // begin writing to the page
435 ew.End() // save changes to the current page
436 doc.PagePushBack(page4)
437
438 ew.Begin( page4 )
439 font := FontCreate(doc.GetSDFDoc(), FontE_helvetica)
440 element := eb.CreateTextBegin( font, 16.0 )
441 element.SetPathFill(true)
442 ew.WriteElement(element)
443 element = eb.CreateTextRun( "Some random text on the page", font, 16.0 )
444 element.SetTextMatrix(1.0, 0.0, 0.0, 1.0, 100.0, 500.0 )
445 ew.WriteElement(element)
446 ew.WriteElement( eb.CreateTextEnd() )
447 ew.End()
448
449 hl := HighlightAnnotCreate( doc.GetSDFDoc(), NewRect(100.0, 490.0, 150.0, 515.0) )
450 hl.SetColor( NewColorPt(0.0,1.0,0.0), 3 )
451 hl.RefreshAppearance()
452 page4.AnnotPushBack( hl )
453
454 sqly := SquigglyCreate( doc.GetSDFDoc(), NewRect(100.0, 450.0, 250.0, 600.0) )
455 sqly.SetQuadPoint( 0, NewQuadPoint(NewPoint(122.0,455.0), NewPoint(240.0, 545.0), NewPoint(230.0, 595.0), NewPoint(101.0,500.0) ) )
456 sqly.RefreshAppearance()
457 page4.AnnotPushBack( sqly )
458
459 cr := CaretCreate( doc.GetSDFDoc(), NewRect(100.0, 40.0, 129.0, 69.0) )
460 cr.SetColor( NewColorPt(0.0,0.0,1.0), 3 )
461 cr.SetSymbol( "P" )
462 cr.RefreshAppearance()
463 page4.AnnotPushBack( cr )
464
465 page5 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
466 ew.Begin(page5) // begin writing to the page
467 ew.End() // save changes to the current page
468 doc.PagePushBack(page5)
469 //fs := FileSpecCreate( doc.GetSDFDoc(), (inputPath + "butterfly.png"), false )
470 page6 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
471 ew.Begin(page6) // begin writing to the page
472 ew.End() // save changes to the current page
473 doc.PagePushBack(page6)
474
475
476 txt := TextCreate( doc.GetSDFDoc(), NewPoint(10.0, 20.0) )
477 txt.SetIcon( "UserIcon" )
478 txt.SetContents( "User defined icon, unrecognized by appearance generator" )
479 txt.SetColor( NewColorPt(0.0,1.0,0.0) )
480 txt.RefreshAppearance()
481 page6.AnnotPushBack( txt )
482
483 ink := InkCreate( doc.GetSDFDoc(), NewRect(100.0, 400.0, 200.0, 550.0 ) )
484 ink.SetColor( NewColorPt(0.0,0.0,1.0) )
485 ink.SetPoint( 1, 3, NewPoint( 220.0, 505.0) )
486 ink.SetPoint( 1, 0, NewPoint( 100.0, 490.0) )
487 ink.SetPoint( 0, 1, NewPoint( 120.0, 410.0) )
488 ink.SetPoint( 0, 0, NewPoint( 100.0, 400.0) )
489 ink.SetPoint( 1, 2, NewPoint( 180.0, 490.0) )
490 ink.SetPoint( 1, 1, NewPoint( 140.0, 440.0) )
491 ink.SetBorderStyle( NewBorderStyle( BorderStyleE_solid, 3.0, 0.0, 0.0 ))
492 ink.RefreshAppearance()
493 page6.AnnotPushBack( ink )
494
495 page7 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
496 ew.Begin(page7) // begin writing to the page
497 ew.End() // save changes to the current page
498 doc.PagePushBack(page7)
499
500 snd := SoundCreate( doc.GetSDFDoc(), NewRect(100.0, 500.0, 120.0, 520.0 ) )
501 snd.SetColor( NewColorPt(1.0,1.0,0.0) )
502 snd.SetIcon( SoundE_Speaker )
503 snd.RefreshAppearance()
504 page7.AnnotPushBack( snd )
505
506 snd = SoundCreate( doc.GetSDFDoc(), NewRect(200.0, 500.0, 220.0, 520.0 ) )
507 snd.SetColor( NewColorPt(1.0,1.0,0.0) )
508 snd.SetIcon( SoundE_Mic )
509 snd.RefreshAppearance()
510 page7.AnnotPushBack( snd )
511
512 page8 := doc.PageCreate(NewRect(0.0, 0.0, 600.0, 600.0))
513 ew.Begin(page8) // begin writing to the page
514 ew.End() // save changes to the current page
515 doc.PagePushBack(page8)
516
517 ipage := 0
518 for ipage<2{
519 px := 5
520 py := 520
521 istamp := RubberStampE_Approved
522 for istamp <= RubberStampE_Draft{
523 st := RubberStampCreate(doc.GetSDFDoc(), NewRect(1.0, 1.0, 100.0, 100.0))
524 st.SetIcon( istamp )
525 st.SetContents( st.GetIconName() )
526 st.SetRect( NewRect(float64(px), float64(py), float64(px+100), float64(py+25) ) )
527 py -= 100
528 if py < 0{
529 py = 520
530 px+=200
531 }
532 if ipage == 0{
533 //page7.AnnotPushBack(st)
534 }else{
535 page8.AnnotPushBack( st )
536 st.RefreshAppearance()
537 }
538 istamp = istamp + 1
539 }
540 ipage = ipage + 1
541 }
542
543 st := RubberStampCreate( doc.GetSDFDoc(), NewRect(400.0, 5.0, 550.0, 45.0) )
544 st.SetIcon( "UserStamp" )
545 st.SetContents( "User defined stamp" )
546 page8.AnnotPushBack( st )
547 st.RefreshAppearance()
548}
549
550func main(){
551 PDFNetInitialize(PDFTronLicense.Key)
552
553 doc := NewPDFDoc(inputPath + "numbered.pdf")
554 doc.InitSecurityHandler()
555
556 // An example of using SDF/Cos API to add any type of annotations.
557 AnnotationLowLevelAPI(doc)
558 doc.Save(outputPath + "annotation_test1.pdf", uint(SDFDocE_remove_unused))
559 fmt.Println("Done. Results saved in annotation_test1.pdf")
560 // An example of using the high-level PDFNet API to read existing annotations,
561 // to edit existing annotations, and to create new annotation from scratch.
562 AnnotationHighLevelAPI(doc)
563 doc.Save((outputPath + "annotation_test2.pdf"), uint(SDFDocE_linearized))
564 doc.Close()
565 fmt.Println("Done. Results saved in annotation_test2.pdf")
566
567 doc1 := NewPDFDoc()
568 CreateTestAnnots(doc1)
569 outfname := outputPath + "new_annot_test_api.pdf"
570 doc1.Save(outfname, uint(SDFDocE_linearized))
571 fmt.Println("Saved new_annot_test_api.pdf")
572 PDFNetTerminate()
573}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales