How to Add a Text Watermark to a PDF using PHP
Watermarks are key to ensuring that your intellectual property can’t be easily stolen and used by an unlicensed third party. Thankfully, using the ready-to-run PHP code provided below, you can easily implement a PDF Watermark API to automate the addition of watermarks in your file processing workflow.
You’ll be able to customize the font name, size, color, and degree of transparency (0.0 = completely transparent; 1.0 = fully opaque; 0.5 = default setting) of your watermark text, and the operation will return the encoding for your newly watermarked PDF document.
To download the SDK, run this command:
composer require cloudmersive/cloudmersive_document_convert_api_client
Then, to structure your API call, simply copy from the PHP code examples below and provide an API key to authorize your request (you can get one by registering a free account here):
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');
$apiInstance = new Swagger\Client\Api\EditPdfApi(
new GuzzleHttp\Client(),
$config
);
$watermark_text = "watermark_text_example"; // string | Watermark text to add to the PDF (required)
$input_file = "/path/to/inputfile"; // \SplFileObject | Input file to perform the operation on.
$font_name = "font_name_example"; // string | Font Family Name for the watermark text; default is Times New Roman
$font_size = 8.14; // float | Font Size in points of the text; default is 150
$font_color = "font_color_example"; // string | Font color in hexadecimal or HTML color name; default is Red
$font_transparency = 8.14; // float | Font transparency between 0.0 (completely transparent) to 1.0 (fully opaque); default is 0.5
try {
$result = $apiInstance->editPdfWatermarkText($watermark_text, $input_file, $font_name, $font_size, $font_color, $font_transparency);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling EditPdfApi->editPdfWatermarkText: ', $e->getMessage(), PHP_EOL;
}
?>
That’s all there is to it. Easy!