Add an image watermark to a PDF

Use this action to personalize a PDF document with an image watermark.

Endpoints

POST https://api.pdfblocks.com/v1/add_watermark/image

Global endpoint (default)

POST https://eu.api.pdfblocks.com/v1/add_watermark/image

European-only endpoint

POST https://{base_url}/v1/add_watermark/image

Self-hosted server endpoint

Parameters

The endpoint accepts multipart/form-data request bodies with the folowing parameters:

file Required

The content of the input PDF document.

image Required

The content of an image to add as a watermark to each page of the PDF document.

  • The format of the image can be either PNG or JPEG.

transparency optional

An integer with the transparency level for the text watermark from 0 (opaque) to 100 (transparent)

  • Integer value between 0 and 100
  • The default value is 50

margin optional

A decimal value with the distance in inches from the border of the page to the text watermark.

  • Decimal value greater than 0.0
  • The default value is 1.0

Returns

If successful, the call returns a response with Content-Type: application/pdf with the content of the watermarked PDF document. Otherwise, this call returns an error.

Code examples

curl

curl \
-F file=@input.pdf \
-F image=@logo.png \
-F transparency=75 \
-F margin=1.0 \
-o watermarked.pdf \
-H 'X-Api-Key: your_api_key' \
https://api.pdfblocks.com/v1/add_watermark/image

Python

# pip install requests

import requests

url = 'https://api.pdfblocks.com/v1/add_watermark/image'

body = {
  'file' : open('input.pdf', 'rb'),
  'image' : open('image.png', 'rb'),
  # 'transparency' : 75,
  # 'margin' : 1.0,
}

headers = { 'X-Api-Key' : 'your_api_key' }

response = requests.post(url, files=body, headers=headers)

if response.status_code == 200:
  with open('watermarked.pdf', 'wb') as output:
    for chunk in response.iter_content():
        output.write(chunk)

Ruby

# gem install http

require 'http'

url = 'https://api.pdfblocks.com/v1/add_watermark/image'

body = {
  file: HTTP::FormData::File.new('input.pdf'),
  image: HTTP::FormData::File.new('image.png'),
  # transparency: 75,
  # margin: 1.0,
}

headers = { 'X-Api-Key' => 'your_api_key' }

response = HTTP.post(url, form: body, headers: headers)

if response.status.ok?
  File.open('watermarked.pdf', 'wb') do |output|
    response.body.each { |chunk| output.write(chunk) }
  end
end

C# (.NET Core)

// Using .NET Core 3.1

using System.IO;
using System.Net.Http;

class Program
{
    static void Main()
    {
        string url = "https://api.pdfblocks.com/v1/add_watermark/image";

        using var inputFile = new FileStream("input.pdf", FileMode.Open, FileAccess.Read);
        using var inputImage = new FileStream("image.pdf", FileMode.Open, FileAccess.Read);

        var body = new MultipartFormDataContent();
        body.Add(new StreamContent(inputFile), "file");
        body.Add(new StreamContent(inputImage), "image");
        // body.Add(new StringContent("75"), "transparency");
        // body.Add(new StringContent("1.0"), "margin");

        var message = new HttpRequestMessage(HttpMethod.Post, url);
        message.Content = body;
        message.Headers.Add("X-Api-Key", "your_api_key");

        var client = new HttpClient();
        var response = client.SendAsync(message).Result;
        if(response.IsSuccessStatusCode)
        {
            using var output = new FileStream("watermarked.pdf", FileMode.Create, FileAccess.Write);
            response.Content.CopyToAsync(output).Wait();
        }
    }
}