
Annotations provide extra information about any specific part of the document. We can mark up the documents with feedback and reviews using annotations. We can add images, comments, notes, or other types of external remarks to PDF documents as annotations. In this article, we will learn how to annotate PDF documents using a REST API in PHP.
The following topics shall be covered in this article:
- PDF Annotation REST API and PHP SDK
- Annotate PDF Documents using a REST API in PHP
- Annotate with Link Annotation using PHP
- Add Image Annotation using PHP
- Add Text Field Annotation using PHP
PDF Annotation REST API and PHP SDK
For annotating PDF documents, we will be using the PHP SDK of GroupDocs.Annotation Cloud API. It allows adding annotations, watermark overlays, text replacements, redactions, and text markups to the supported document formats. Please install it using the following command in the console:
composer require groupdocscloud/groupdocs-annotation-cloud
After installation, please use the Composers’ autoload to use the SDK as shown below:
require_once('vendor/autoload.php');
Please get your Client ID and Secret from the dashboard before following the mentioned steps. Once you have your ID and secret, add in the code as shown below:
// This code example demonstrates how to add client id and secret in the code. | |
static $ClientId = '659fe7da-715b-4744-a0f7-cf469a392b73'; | |
static $ClientSecret = 'b377c36cfa28fa69960ebac6b6e36421'; | |
static $ApiBaseUrl = 'https://api.groupdocs.cloud'; | |
static $MyStorage = ''; | |
// Intializing the configuration | |
$configuration = new GroupDocs\Annotation\Configuration(); | |
// Setting the configurations | |
$configuration->setAppSid(self::$ClientId); | |
$configuration->setAppKey(self::$ClientSecret); | |
$configuration->setApiBaseUrl(self::$ApiBaseUrl); |
Annotate PDF Documents using a REST API in PHP
We can annotate PDF documents on the cloud by following the simple steps given below:
- Upload the PDF file to the cloud
- Annotate PDF Document
- Download the annotated file
Upload the Document
Firstly, we will upload the PDF file to the cloud using the following code sample:
// This code example demonstrates how to upload a PDF file to the cloud. | |
// Initialize an instance of the API | |
$apiInstance = new GroupDocs\Annotation\FileApi($configuration); | |
// File path | |
$file = "C:\\Files\\Annotation\\sample.pdf"; | |
// Create upload file request | |
$request = new GroupDocs\Annotation\Model\Requests\UploadFileRequest("sample.pdf", $file, self::$MyStorage, null); | |
// Upload file | |
$response = $apiInstance->uploadFile($request); |
As a result, the uploaded PDF file will be available in the files section of the dashboard on the cloud.
Annotate PDF Document using PHP
Now, we will add multiple annotations to the uploaded PDF document programmatically by following the steps given below:
- Firstly, create an instance of the AnnotateApi.
- Next, set annotation properties e.g., position, type, text, etc.
- Then, repeat the above step to add multiple annotations.
- Next, provide the input file path.
- Then, initialize the AnnotateOptions object and set the output file path.
- After that, create the AnnotateRequest with AnnotateOptions as argument.
- Finally, annotate PDF using the AnnotateApi.annotate() method.
The following code sample shows how to add multiple annotations to a PDF document using a REST API in PHP.
// This code example demonstrates how to add multiple annotations to the PDF. | |
// Initialize AnnotationAPI instance | |
$apiInstance = new GroupDocs\Annotation\AnnotateApi($configuration); | |
// Add distance annotation | |
$a = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt = new GroupDocs\Annotation\Model\Point(); | |
$pt->setX(1); | |
$pt->setY(1); | |
$a->setAnnotationPosition($pt); | |
$box = new GroupDocs\Annotation\Model\Rectangle(); | |
$box->setX(100); | |
$box->setY(100); | |
$box->setWidth(200); | |
$box->setHeight(100); | |
$a->setBox($box); | |
$a->setPageNumber(0); | |
$a->setPenColor(1201033); | |
$a->setPenStyle(GroupDocs\Annotation\Model\AnnotationInfo::PEN_STYLE_SOLID); | |
$a->setPenWidth(1); | |
$a->setOpacity(0.7); | |
$a->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_DISTANCE); | |
$a->setText("This is distance annotation"); | |
$a->setCreatorName("Anonym A."); | |
// Add area annotation | |
$a1 = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt1 = new GroupDocs\Annotation\Model\Point(); | |
$pt1->setX(1); | |
$pt1->setY(1); | |
$a1->setAnnotationPosition($pt1); | |
$box1 = new GroupDocs\Annotation\Model\Rectangle(); | |
$box1->setX(80); | |
$box1->setY(400); | |
$box1->setWidth(200); | |
$box1->setHeight(100); | |
$a1->setBox($box1); | |
$a1->setPageNumber(0); | |
$a1->setPenColor(1201033); | |
$a1->setPenStyle(GroupDocs\Annotation\Model\AnnotationInfo::PEN_STYLE_SOLID); | |
$a1->setPenWidth(1); | |
$a1->setOpacity(0.7); | |
$a1->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_AREA); | |
$a1->setText("This is area annotation"); | |
$a1->setCreatorName("Anonym A."); | |
// Add point annotation | |
$a2 = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt2 = new GroupDocs\Annotation\Model\Point(); | |
$pt2->setX(100); | |
$pt2->setY(100); | |
$a2->setAnnotationPosition($pt2); | |
$box2 = new GroupDocs\Annotation\Model\Rectangle(); | |
$box2->setX(450); | |
$box2->setY(150); | |
$box2->setWidth(100); | |
$box2->setHeight(30); | |
$a2->setBox($box2); | |
$a2->setPageNumber(0); | |
$a2->setOpacity(0.7); | |
$a2->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_POINT); | |
$a2->setText("This is point annotation"); | |
$a2->setCreatorName("Anonym A."); | |
// Add arrow annotation | |
$a3 = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt3 = new GroupDocs\Annotation\Model\Point(); | |
$pt3->setX(1); | |
$pt3->setY(1); | |
$a3->setAnnotationPosition($pt3); | |
$box3 = new GroupDocs\Annotation\Model\Rectangle(); | |
$box3->setX(350); | |
$box3->setY(350); | |
$box3->setWidth(200); | |
$box3->setHeight(100); | |
$a3->setBox($box3); | |
$a3->setPageNumber(0); | |
$a3->setPenColor(1201033); | |
$a3->setPenStyle(GroupDocs\Annotation\Model\AnnotationInfo::PEN_STYLE_SOLID); | |
$a3->setPenWidth(1); | |
$a3->setOpacity(0.7); | |
$a3->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_ARROW); | |
$a3->setText("This is arrow annotation"); | |
$a3->setCreatorName("Anonym A."); | |
// Input file path | |
$fileInfo = new GroupDocs\Annotation\Model\FileInfo(); | |
$fileInfo->setFilePath("sample.pdf"); | |
// Define annotate options | |
$options = new GroupDocs\Annotation\Model\AnnotateOptions(); | |
$options->setFileInfo($fileInfo); | |
$options->setAnnotations([$a, $a1, $a2, $a3]); | |
$options->setOutputPath("MultipleAnnotation.pdf"); | |
// Create annotate request | |
$request = new GroupDocs\Annotation\Model\Requests\annotateRequest($options); | |
// Annotate | |
$result = $apiInstance->annotate($request); | |
echo "AddMultipleAnnotations: Multiple Annotations added: " . $result->getHref(); |

Annotate PDF Document using PHP.
You can read more about supported annotation types under adding annotations section in the documentation.
Download the Annotated File
The above code sample will save the annotated PDF file on the cloud. It can be downloaded using the following code sample:
// This code example demonstrates how to download a PDF file from the cloud. | |
// Intializing an instance of the API | |
$apiInstance = new GroupDocs\Annotation\FileApi($configuration); | |
// Create download file request | |
$request = new GroupDocs\Annotation\Model\Requests\DownloadFileRequest("MultipleAnnotation.pdf", self::$MyStorage, null); | |
// Download file | |
$response = $apiInstance->downloadFile($request); |
Annotate with Link Annotation using PHP
We can also add hyperlink annotation in the PDF document by following the steps given below:
- Firstly, create an instance of the AnnotateApi.
- Next, set annotation properties e.g., position, text, etc.
- Then, set annotation type as Link.
- Next, provide the input file path.
- Then, initialize the AnnotateOptions object and set the output file path.
- After that, create the AnnotateRequest with AnnotateOptions as argument.
- Finally, annotate PDF using the AnnotateApi.annotate() method.
The following code sample shows how to add hyperlink annotation in the PDF document using a REST API. Please follow the steps mentioned earlier to upload and download a file.
// This code example demonstrates how to add link annotation to PDF | |
// Initialize AnnotationAPI instance | |
$apiInstance = new GroupDocs\Annotation\AnnotateApi($configuration); | |
// Add link annotation | |
$a = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt1 = new GroupDocs\Annotation\Model\Point(); | |
$pt1->setX(80); | |
$pt1->setY(710); | |
$pt2 = new GroupDocs\Annotation\Model\Point(); | |
$pt2->setX(240); | |
$pt2->setY(710); | |
$pt3 = new GroupDocs\Annotation\Model\Point(); | |
$pt3->setX(80); | |
$pt3->setY(650); | |
$pt4 = new GroupDocs\Annotation\Model\Point(); | |
$pt4->setX(240); | |
$pt4->setY(650); | |
$a->setPoints([$pt1, $pt2, $pt3, $pt4]); | |
$a->setUrl("https://www.groupdocs.com/"); | |
$a->setPageNumber(0); | |
$a->setFontColor(1201033); | |
$a->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_LINK); | |
$a->setText("This is link annotation"); | |
$a->setCreatorName("Anonym A."); | |
// Input file path | |
$fileInfo = new GroupDocs\Annotation\Model\FileInfo(); | |
$fileInfo->setFilePath("sample.pdf"); | |
// Define annotate options | |
$options = new GroupDocs\Annotation\Model\AnnotateOptions(); | |
$options->setFileInfo($fileInfo); | |
$options->setAnnotations([$a]); | |
$options->setOutputPath("AddLinkAnnotation.pdf"); | |
// Create annotate request | |
$request = new GroupDocs\Annotation\Model\Requests\annotateRequest($options); | |
// Annotate | |
$result = $apiInstance->annotate($request); | |
echo "AddLinkAnnotation: Link Annotation added: " . $result->getHref(); |

Annotate with Link Annotations using PHP.
Add Image Annotation using PHP
We can add image annotation in the PDF document by following the steps mentioned earlier. However, we just need to set the annotation type to image as shown below:
$a->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_IMAGE);
The following code sample shows how to add image annotation in the PDF document using a REST API in PHP. Please follow the steps mentioned earlier to upload and download a file.
// This code example demonstrates how to add image annotation to PDF | |
// Initialize AnnotationAPI instance | |
$apiInstance = new GroupDocs\Annotation\AnnotateApi($configuration); | |
// Add Image annotation | |
$a = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt = new GroupDocs\Annotation\Model\Point(); | |
$pt->setX(1); | |
$pt->setY(1); | |
$a->setAnnotationPosition($pt); | |
$box = new GroupDocs\Annotation\Model\Rectangle(); | |
$box->setX(200); | |
$box->setY(0); | |
$box->setWidth(200); | |
$box->setHeight(100); | |
$a->setBox($box); | |
$a->setPageNumber(0); | |
$a->setImagePath("groupdocs.png"); | |
$a->setPenStyle(GroupDocs\Annotation\Model\AnnotationInfo::PEN_STYLE_SOLID); | |
$a->setOpacity(0.7); | |
$a->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_IMAGE); | |
$a->setText("This is image annotation"); | |
$a->setCreatorName("Anonym A."); | |
// Input file path | |
$fileInfo = new GroupDocs\Annotation\Model\FileInfo(); | |
$fileInfo->setFilePath("sample.pdf"); | |
// Define annotate options | |
$options = new GroupDocs\Annotation\Model\AnnotateOptions(); | |
$options->setFileInfo($fileInfo); | |
$options->setAnnotations([$a]); | |
$options->setOutputPath("AddImageAnnotation.pdf"); | |
// Create annotate request | |
$request = new GroupDocs\Annotation\Model\Requests\annotateRequest($options); | |
// Annotate | |
$result = $apiInstance->annotate($request); | |
echo "AddImageAnnotation: Image Annotation added: " . $result->getHref(); |

Add Image Annotation using PHP.
Add Text Field Annotation using PHP
We can also add text field annotation in the PDF document by following the steps mentioned earlier. However, we just need to set the annotation type to text field as shown below:
$a->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_TEXT_FIELD);
The following code sample shows how to add text field annotation in the PDF document using a REST API. Please follow the steps mentioned earlier to upload and download a file.
// This code example demonstrates how to add text field annotation to PDF | |
// Initialize AnnotationAPI instance | |
$apiInstance = new GroupDocs\Annotation\AnnotateApi($configuration); | |
// Add TextField annotation | |
$a = new GroupDocs\Annotation\Model\AnnotationInfo(); | |
$pt = new GroupDocs\Annotation\Model\Point(); | |
$pt->setX(1); | |
$pt->setY(1); | |
$a->setAnnotationPosition($pt); | |
$box = new GroupDocs\Annotation\Model\Rectangle(); | |
$box->setX(380); | |
$box->setY(300); | |
$box->setWidth(100); | |
$box->setHeight(50); | |
$a->setBox($box); | |
$a->setPageNumber(0); | |
$a->setFontColor(1201033); | |
$a->setFontSize(12); | |
$a->setOpacity(0.7); | |
$a->setType(GroupDocs\Annotation\Model\AnnotationInfo::TYPE_TEXT_FIELD); | |
$a->setText("Text field text"); | |
$a->setCreatorName("Anonym A."); | |
// Input file path | |
$fileInfo = new GroupDocs\Annotation\Model\FileInfo(); | |
$fileInfo->setFilePath("sample.pdf"); | |
// Define annotate options | |
$options = new GroupDocs\Annotation\Model\AnnotateOptions(); | |
$options->setFileInfo($fileInfo); | |
$options->setAnnotations([$a]); | |
$options->setOutputPath("AddTextFieldAnnotation.pdf"); | |
// Create annotate request | |
$request = new GroupDocs\Annotation\Model\Requests\annotateRequest($options); | |
// Annotate | |
$result = $apiInstance->annotate($request); | |
echo "AddTextFieldAnnotation: Text Field Annotation added: " . $result->getHref(); |

Add Text Field Annotation using PHP.
Try Online
Please try the following free online PDF annotation tool, which is developed using the above API. https://products.groupdocs.app/annotation/pdf
Conclusion
In this article, we have learned how to:
- add multiple annotations to a PDF using PHP;
- annotate PDF with link, image, and text field annotation in PHP;
- programmatically upload the PDF file to the cloud;
- download the annotated PDF file from the cloud.
Besides, you can learn more about GroupDocs.Annotation Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser. In case of any ambiguity, please feel free to contact us on the forum.