Skip to content

Instantly share code, notes, and snippets.

@groupdocs-cloud-gists
Created January 8, 2022 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groupdocs-cloud-gists/74e73a0c2f1bd1c2677f3b20a4788c1a to your computer and use it in GitHub Desktop.
Save groupdocs-cloud-gists/74e73a0c2f1bd1c2677f3b20a4788c1a to your computer and use it in GitHub Desktop.
Annotate PDF Documents using a REST API in PHP
// 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();
// 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();
// 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();
// 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();
// 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);
// 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);
// 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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment