PDF に画像透かしを追加する
PNG または JPEG 画像を PDF の各ページに付与します。透明度、余白、対象ページを指定できます。
PDF ドキュメントに画像透かしを追加します。PNG または JPEG を指定し、透明度と余白を制御できます。デフォルトでは透かしはすべてのページに付与されます。特定のページだけを対象にするには、pages パラメーターを使用します。API はステートレスです。ドキュメントはリージョン内で処理され、保存されることはありません。
エンドポイント
/v1/add_image_watermarkすべてのリージョンで利用できます。ルーティングとデータレジデンシーについては、リージョンとデータレジデンシーを参照してください。
| リージョン | URL |
|---|---|
| グローバル | https://api.pdfblocks.com/v1/add_image_watermark |
| 日本 | https://jp.api.pdfblocks.com/v1/add_image_watermark |
| 米国 | https://us.api.pdfblocks.com/v1/add_image_watermark |
| HIPAA 米国 | https://hipaa.api.pdfblocks.com/v1/add_image_watermark |
| 欧州連合 | https://eu.api.pdfblocks.com/v1/add_image_watermark |
| 英国 | https://uk.api.pdfblocks.com/v1/add_image_watermark |
| カナダ | https://ca.api.pdfblocks.com/v1/add_image_watermark |
| オーストラリア | https://au.api.pdfblocks.com/v1/add_image_watermark |
| インド | https://in.api.pdfblocks.com/v1/add_image_watermark |
| ブラジル | https://br.api.pdfblocks.com/v1/add_image_watermark |
認証
すべてのリクエストは、HTTPS 経由で X-API-Key ヘッダーにシークレット API キーを設定して認証してください。キーの作成と管理はダッシュボードから行います。詳細は認証を参照してください。
リクエスト
このエンドポイントは multipart/form-data 形式のリクエストボディを受け付けます。
filefilerequired入力する PDF ドキュメント。
imagefilerequired各ページに付与する透かし画像。PNG または JPEG 形式に限ります。添付方法についてはファイルの操作を参照してください。
transparencyintegerdefault:50透明度のレベル。0(完全に不透明)から 100(完全に透明)までの値を指定します。
margindecimaldefault:1.0ページの端から透かしまでの距離をインチで指定します。0 以上の値を指定します。
pagesstring透かしを付与するページを、1..3,5 のようなページ範囲として指定します。省略した場合、透かしはすべてのページに適用されます。最大1000文字です。
ページの選択
pages パラメーターには、1から始まるページ番号と範囲をカンマ区切りで指定します。これは集合として扱われるため、順序や重複は無視され、ページは常にドキュメントの順序で透かしが付与されます。
| パターン | 選択されるページ |
|---|---|
| (省略) | すべてのページ |
1 |
最初のページのみ |
1..3,5 |
ページ1、2、3、5 |
2.. |
ページ2から最後のページまで |
..-2 |
最初のページから最後から2番目のページまで |
-1 |
最後のページ |
完全なリファレンスについてはページの選択を参照してください。
例
透明度60%ですべてのページにロゴを付与する例:
curl https://api.pdfblocks.com/v1/add_image_watermark \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F image=@logo.png \
-F transparency=60 \
-o watermarked.pdf# pip install requests
import requests
with open('input.pdf', 'rb') as file, open('logo.png', 'rb') as image:
response = requests.post(
'https://api.pdfblocks.com/v1/add_image_watermark',
headers={'X-API-Key': 'your_api_key'},
files={'file': file, 'image': image},
data={'transparency': 60},
)
response.raise_for_status()
with open('watermarked.pdf', 'wb') as output:
output.write(response.content)// Node.js 18+
import { readFile, writeFile } from 'node:fs/promises';
const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('image', new Blob([await readFile('logo.png')]), 'logo.png');
body.set('transparency', '60');
const response = await fetch('https://api.pdfblocks.com/v1/add_image_watermark', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('watermarked.pdf', Buffer.from(await response.arrayBuffer()));<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_image_watermark');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'image' => new CURLFile('logo.png', 'image/png'),
'transparency' => '60',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('watermarked.pdf', $pdf);
}# gem install http
require 'http'
response = HTTP
.headers('X-API-Key' => 'your_api_key')
.post('https://api.pdfblocks.com/v1/add_image_watermark', form: {
file: HTTP::FormData::File.new('input.pdf'),
image: HTTP::FormData::File.new('logo.png'),
transparency: '60',
})
File.write('watermarked.pdf', response.body) if response.status.success?package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
file, _ := os.Open("input.pdf")
defer file.Close()
filePart, _ := form.CreateFormFile("file", "input.pdf")
io.Copy(filePart, file)
image, _ := os.Open("logo.png")
defer image.Close()
imagePart, _ := form.CreateFormFile("image", "logo.png")
io.Copy(imagePart, image)
form.WriteField("transparency", "60")
form.Close()
req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_image_watermark", &buf)
req.Header.Set("Content-Type", form.FormDataContentType())
req.Header.Set("X-API-Key", "your_api_key")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("watermarked.pdf")
defer out.Close()
io.Copy(out, res.Body)
}using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
using var form = new MultipartFormDataContent
{
{ new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
{ new ByteArrayContent(File.ReadAllBytes("logo.png")), "image", "logo.png" },
{ new StringContent("60"), "transparency" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_image_watermark", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"watermarked.pdf", await response.Content.ReadAsByteArrayAsync());レスポンス
成功すると、透かしを付与した PDF を本文とする 200 OK が返されます:
HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 51820出力は入力と同じページ数・寸法を保持し、追加されるのは透かしのみです。上記の例のように、レスポンスの本文はそのままファイルにストリーミングしてください。サーバー側には何も保存されません。
エラー
リクエストが失敗した場合は、application/problem+json 形式のボディが返されます。このエンドポイントで最も多いのは 400 で、パラメーターが無効な場合や image がサポートされていない形式の場合に返され、errors オブジェクトに該当するフィールド名が示されます:
{
"type": "https://www.pdfblocks.com/docs/api/v1/error/400",
"title": "One or more validation errors occurred.",
"status": 400,
"errors": {
"image": ["The image must be a PNG or JPEG file."]
}
}X-API-Key が指定されていないか無効な場合は 401 が返されます。すべてのステータスコードとレスポンスの完全な形式については、エラーを参照してください。
レシピ
よく使われるバリエーションです。展開すると、各言語のコード例を確認できます。
すべてのページに薄いロゴを付与
curl https://api.pdfblocks.com/v1/add_image_watermark \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F image=@logo.png \
-F transparency=85 \
-o faint.pdfimport requests
with open('input.pdf', 'rb') as file, open('logo.png', 'rb') as image:
response = requests.post(
'https://api.pdfblocks.com/v1/add_image_watermark',
headers={'X-API-Key': 'your_api_key'},
files={'file': file, 'image': image},
data={'transparency': 85},
)
response.raise_for_status()
with open('faint.pdf', 'wb') as output:
output.write(response.content)import { readFile, writeFile } from 'node:fs/promises';
const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('image', new Blob([await readFile('logo.png')]), 'logo.png');
body.set('transparency', '85');
const response = await fetch('https://api.pdfblocks.com/v1/add_image_watermark', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('faint.pdf', Buffer.from(await response.arrayBuffer()));<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_image_watermark');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'image' => new CURLFile('logo.png', 'image/png'),
'transparency' => '85',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('faint.pdf', $pdf);
}require 'http'
response = HTTP
.headers('X-API-Key' => 'your_api_key')
.post('https://api.pdfblocks.com/v1/add_image_watermark', form: {
file: HTTP::FormData::File.new('input.pdf'),
image: HTTP::FormData::File.new('logo.png'),
transparency: '85',
})
File.write('faint.pdf', response.body) if response.status.success?package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
file, _ := os.Open("input.pdf")
defer file.Close()
filePart, _ := form.CreateFormFile("file", "input.pdf")
io.Copy(filePart, file)
image, _ := os.Open("logo.png")
defer image.Close()
imagePart, _ := form.CreateFormFile("image", "logo.png")
io.Copy(imagePart, image)
form.WriteField("transparency", "85")
form.Close()
req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_image_watermark", &buf)
req.Header.Set("Content-Type", form.FormDataContentType())
req.Header.Set("X-API-Key", "your_api_key")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("faint.pdf")
defer out.Close()
io.Copy(out, res.Body)
}using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
using var form = new MultipartFormDataContent
{
{ new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
{ new ByteArrayContent(File.ReadAllBytes("logo.png")), "image", "logo.png" },
{ new StringContent("85"), "transparency" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_image_watermark", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"faint.pdf", await response.Content.ReadAsByteArrayAsync());表紙のみにロゴを付与
curl https://api.pdfblocks.com/v1/add_image_watermark \
-H 'X-API-Key: your_api_key' \
-F file=@input.pdf \
-F image=@logo.png \
-F pages='1' \
-o cover-stamped.pdfimport requests
with open('input.pdf', 'rb') as file, open('logo.png', 'rb') as image:
response = requests.post(
'https://api.pdfblocks.com/v1/add_image_watermark',
headers={'X-API-Key': 'your_api_key'},
files={'file': file, 'image': image},
data={'pages': '1'},
)
response.raise_for_status()
with open('cover-stamped.pdf', 'wb') as output:
output.write(response.content)import { readFile, writeFile } from 'node:fs/promises';
const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('image', new Blob([await readFile('logo.png')]), 'logo.png');
body.set('pages', '1');
const response = await fetch('https://api.pdfblocks.com/v1/add_image_watermark', {
method: 'POST',
headers: { 'X-API-Key': 'your_api_key' },
body,
});
if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('cover-stamped.pdf', Buffer.from(await response.arrayBuffer()));<?php
$ch = curl_init('https://api.pdfblocks.com/v1/add_image_watermark');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => [
'file' => new CURLFile('input.pdf', 'application/pdf'),
'image' => new CURLFile('logo.png', 'image/png'),
'pages' => '1',
],
]);
$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
file_put_contents('cover-stamped.pdf', $pdf);
}require 'http'
response = HTTP
.headers('X-API-Key' => 'your_api_key')
.post('https://api.pdfblocks.com/v1/add_image_watermark', form: {
file: HTTP::FormData::File.new('input.pdf'),
image: HTTP::FormData::File.new('logo.png'),
pages: '1',
})
File.write('cover-stamped.pdf', response.body) if response.status.success?package main
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"os"
)
func main() {
var buf bytes.Buffer
form := multipart.NewWriter(&buf)
file, _ := os.Open("input.pdf")
defer file.Close()
filePart, _ := form.CreateFormFile("file", "input.pdf")
io.Copy(filePart, file)
image, _ := os.Open("logo.png")
defer image.Close()
imagePart, _ := form.CreateFormFile("image", "logo.png")
io.Copy(imagePart, image)
form.WriteField("pages", "1")
form.Close()
req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/add_image_watermark", &buf)
req.Header.Set("Content-Type", form.FormDataContentType())
req.Header.Set("X-API-Key", "your_api_key")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
out, _ := os.Create("cover-stamped.pdf")
defer out.Close()
io.Copy(out, res.Body)
}using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");
using var form = new MultipartFormDataContent
{
{ new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
{ new ByteArrayContent(File.ReadAllBytes("logo.png")), "image", "logo.png" },
{ new StringContent("1"), "pages" },
};
var response = await client.PostAsync(
"https://api.pdfblocks.com/v1/add_image_watermark", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
"cover-stamped.pdf", await response.Content.ReadAsByteArrayAsync());