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

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales