Annotate PDF Documents using REST API in PHP

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

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:

Annotate PDF Documents using a REST API in PHP

We can annotate PDF documents on the cloud by following the simple steps given below:

  1. Upload the PDF file to the cloud
  2. Annotate PDF Document
  3. Download the annotated file

Upload the Document

Firstly, we will upload the PDF file to the cloud using the following code sample:

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.

Annotate PDF Document using PHP

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:

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.

Annotate with Link Annotations using PHP.

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.

Add Image Annotation using PHP.

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.

Add Text Field Annotation using PHP.

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.

See Also