Add Watermark via PHP

Add watermarks to PDF document programmatically using Aspose.PDF for PHP via Java

Add Watermark to PDF File Using PHP via Java Tool

In order to add Watermark to PDF file, we’ll use Aspose.PDF for PHP via Java API which is a feature-rich, powerful and easy-to-use document manipulation tool in php-java. Install Tomcat 9.0 version on any location, add Aspose.PDF.war, for more details check the GitHub page.

Add Watermark via PHP


To try the code in your environment, you need Aspose.PDF for PHP via Java.

  1. Load the PDF with an instance of Document.
  2. Create an instance of WatermarkArtifact.
  3. Set properties of WatermarkArtifact object.
  4. Add watermark using method Add of Aspose.Pdf.Page.Artifacts collection class.
  5. Save PDF file

Add Watermark to PDF - PHP

    // Open document
    $document = new Document($inputFile);
            
    $formattedText = new facades_FormattedText("Watermark", 
        (new Color())->getBlue()->toRgb(),
        (new facades_FontStyle())->Courier, 
        (new facades_EncodingType())->Identity_h, 
        true, 72.0);

    $horizontalAlignment = new HorizontalAlignment();
    $verticalAlignment = new VerticalAlignment();

    $artifact = new WatermarkArtifact();        
    $artifact->setText($formattedText);        
    $artifact->setArtifactHorizontalAlignment ($horizontalAlignment->Center);
    $artifact->setArtifactVerticalAlignment ($verticalAlignment->Center);
    $artifact->setRotation(45);
    $artifact->setOpacity(0.5);
    $artifact->setBackground(true);
    $document->getPages()->get_Item(1)->getArtifacts()->add($artifact);
    
    // Save output document
    $document->save($outputFile);
    $document->close();