Pdf Watermark Removal logo

Pdf Watermark Removal

Community
MikeWuPing
pdf-watermark-removal

Use when PDF contains watermarks that need removal — covers XObject-embedded watermarks drawn via Do commands and content-stream text watermarks (text drawn directly in the page stream with a distinctive embedded subset font), or when asked to clean/remove watermarks from PDF documents. Not for scanned image watermarks or encrypted PDFs.

Overview

PublisherMikeWuPing
Repositorypdf-watermark-removal
Skill namepdf-watermark-removal
Stars
31
Forks
3
Bundled files
Instructions only
Links
  • Markdown instructions

    A SKILL.md file the model loads on demand, so it only costs tokens when a request actually matches.

  • Works with any LLM

    AI skills are plain Markdown, not provider-specific code, so this works with GPT, Claude, Gemini, Grok, or a local model.

  • Self-contained

    Everything the model needs lives in the instructions — no extra files to sync.

  • Open source

    Published by MikeWuPing on GitHub. Read the source before you install it.

Installation

Install the Pdf Watermark Removal AI skill in TypingMind to use it with any LLM, or drop it into another agent that reads SKILL.md.

1

Install in TypingMind

TypingMind installs a skill straight from its GitHub folder — it reads SKILL.md, bundles the resource files, and stores the result locally.

  1. Open the app and go to Plugins → Skills.
  2. Choose "Install from GitHub".
  3. Paste the skill folder URL below and confirm.
    https://github.com/MikeWuPing/pdf-watermark-removal/tree/main/Skill/pdf-watermark-removal
  4. Enable the skill in any chat where you want it available.
Plugins → Skills → Add skill → From GitHub URL, then paste the folder URL and press Continue.
2

Install in another agent

Any agent that reads the Agent Skills format can use this skill — copy the folder into that agent's skills directory.

Claude Code — .claude/skills
git clone --depth 1 https://github.com/MikeWuPing/pdf-watermark-removal.git /tmp/pdf-watermark-removal
mkdir -p .claude/skills
cp -r /tmp/pdf-watermark-removal/Skill/pdf-watermark-removal .claude/skills/pdf-watermark-removal
Restart Claude Code after copying so it picks up the new skill.

Use it in TypingMind

Enable Pdf Watermark Removal in any TypingMind chat and the model takes it from there. Its name and description sit in the system prompt, and the moment a request matches, the model loads the full instructions itself — you never invoke it by hand, and it costs no tokens until it is actually used.

The model loads Pdf Watermark Removal on its own as soon as a request matches it.

Works with any AI model

AI skills are plain Markdown instructions rather than provider-specific code, so Pdf Watermark Removal is not tied to the model it was written for. Install it once in TypingMind and use it with GPT-5, Claude, Gemini, Grok, DeepSeek, Mistral, Llama, or a local model you run yourself — all on your own API keys.

  • Loaded only when it is needed

    The system prompt carries just the name and description. The instructions are fetched on the first matching request, so an idle skill costs nothing.

  • Switch models mid-chat

    Because the skill is instructions rather than code, changing model does not break it — the next model reads the same SKILL.md.

Skill instructions

This is the SKILL.md content the model loads. Read it before installing — a skill is instructions your model will follow.

PDF 水印移除

概述

水印在 PDF 内部有两种常见形态:XObject 型(水印是 Form XObject,页面内容流通过 Do 命令绘制)和内容流文本型(水印文本用特征字体直接写在页面内容流中)。两者共性的可识别特征:每页重复出现、且使用独立于正文的嵌入子集字体(BaseFont 常有 AAAA/AAAAAA 等子集前缀,如 AAAAAB+Helvetica-Bold)。移除原则:定位水印字体 → 找到文字绘制的作用域 → 仅删除该作用域,正文内容流保持原样。

识别类型(第一步,必做)

误判类型是"报告无水印但水印还在"最常见的原因。先统计每页字体分布:

python
from collections import Counter
with pdfplumber.open(pdf_path) as pdf:
    for i, page in enumerate(pdf.pages[:3]):
        print(f'第{i+1}页:', dict(Counter(c['fontname'] for c in page.chars)))

水印字体特征:每页字符数恒定 × 使用非正文的嵌入子集字体。

类型内部特征水印字体位置移除点
XObject 型水印字体藏在 Form XObject 的 /Resources 中XObject 资源树页面内容流中的 q ... /Name Do Q 块
文本型水印字体在页面顶级 /Resources,BT...ET 直接绘制页面资源内容流中含特征字体 Tf 的最外层 q ... Q 段

扫描件水印(图像层)和加密 PDF 不适用本方法。

XObject 型移除

  1. 递归扫描 XObject 树,找包含水印字体的叶子(Form XObject 内 /Font 有特征 BaseFont),深度上限 20
  2. 追溯引用链,找到引用水印叶子的页面级 XObject 名(如 /_0)
  3. 从页面内容流删除 q ... cm /Name Do Q 块:
python
import re
# XObject 名已含斜杠,直接使用,勿再加 /
pattern = rf'(?:Q\r\n)?q\r\n(?:[\d.\-]+ ){{5}}[\d.\-]+ cm\r\n{re.escape(xobj_name)}\s+Do\r\nQ'
  1. 注意 XObject 名成对出现(如 _0/_1),检测到任一就同时移除两个

文本型移除(核心)

python
import pikepdf, re

pdf = pikepdf.open(pdf_path)
# 1. 从字体资源找特征字体名(匹配子集前缀或唯一非正文字体)
wm_name = None
for name, ref in pdf.pages[0].Resources['/Font'].items():
    font = ref.get_object() if hasattr(ref, 'get_object') else ref
    if 'AAAA' in str(font.get('/BaseFont', '')):  # 嵌入子集特征
        wm_name = name
assert wm_name, '未找到特征字体'

for page in pdf.pages:
    c = page.Contents
    data = c.read_bytes().decode('latin-1', errors='ignore')
    tfs = [m.start() for m in re.finditer(re.escape(wm_name) + r' [\d.]+ Tf', data)]
    if not tfs:
        continue
    # 2. 定位包围全部水印文本块的最外层 q...Q
    q_pos = [m.start() for m in re.finditer(r'(?<![a-zA-Z0-9])q(?![a-zA-Z0-9])', data)]
    Q_pos = [m.start() for m in re.finditer(r'(?<![a-zA-Z0-9])Q(?![a-zA-Z0-9])', data)]
    start = [p for p in q_pos if p < tfs[0]][-1]
    end = [p for p in Q_pos if p > tfs[-1]][0]
    new_data = data[:start] + data[end+1:]
    # 3. 结构安全断言
    assert new_data.count('BT') == new_data.count('ET')
    assert len(re.findall(r'(?<![a-zA-Z0-9])q(?![a-zA-Z0-9])', new_data)) == \
           len(re.findall(r'(?<![a-zA-Z0-9])Q(?![a-zA-Z0-9])', new_data))
    c.write(new_data.encode('latin-1', errors='ignore'))
pdf.save(output_path)

水印块常见外部包裹:q + 旋转矩阵(如 0.86603 0.5 -0.5 0.86603 0 0 cm)+ 若干 BT 文本块 + Q,整段删除即可。

验证方法

python
import pdfplumber, fitz

orig = pdfplumber.open(input_path); out = pdfplumber.open(output_path)
wm_o = sum(1 for p in orig.pages for c in p.chars if 'AAAA' in c.get('fontname', ''))
body_o = sum(1 for p in orig.pages for c in p.chars if 'AAAA' not in c.get('fontname', ''))
wm_n = sum(1 for p in out.pages for c in p.chars if 'AAAA' in c.get('fontname', ''))
body_n = sum(1 for p in out.pages for c in p.chars if 'AAAA' not in c.get('fontname', ''))
print(f'水印字符: {wm_o} -> {wm_n}')   # 应归零
print(f'正文字符: {body_o} -> {body_n}')  # 应完全一致
# 渲染确认无损坏
fitz.open(output_path)[0].get_pixmap(dpi=80)

常见陷阱

问题原因解决方案
报告无水印但水印可见误判类型:XObject 检测扫不到文本型先做字体分布分析再选分支
正则匹配失败资源名含 +/. 等元字符re.escape(资源名)
内容流乱码/损坏非 ASCII 字符读写统一用 latin-1 + errors='ignore'
删除后 PDF 损坏q/Q 或 BT/ET 不配对删除后断言配对平衡
保存 PermissionError输出文件带只读属性(shutil.copy2 遗留)先删除旧文件再 pdf.save
水印嵌套多层 XObject引用链长递归检查,max_depth 到 20;用内容流含叶子名做二次验证
/F2+0 类资源名匹配困难内容流里有 +0 后缀区分水印字体精确匹配字体资源名,勿宽泛匹配 /F2

依赖

bash
pip install pikepdf pdfplumber PyMuPDF
  • pikepdf:结构操作与内容流读写(stream.write() 直接改写,无需手工重建流)
  • pdfplumber:字体分布分析、字符级验证
  • PyMuPDF (fitz):渲染完整性检查

Frequently asked questions

What does the Pdf Watermark Removal AI skill do?

Use when PDF contains watermarks that need removal — covers XObject-embedded watermarks drawn via Do commands and content-stream text watermarks (text drawn directly in the page stream with a distinctive embedded subset font), or when asked to clean/remove watermarks from PDF documents. Not for scanned image watermarks or encrypted PDFs.

Why use Pdf Watermark Removal on TypingMind?

Because you install it once and use it with any model. Pdf Watermark Removal is plain Markdown rather than provider-specific code, so the same skill runs on GPT-5, Claude, Gemini, Grok, or a local model — and you can switch model mid-chat without it breaking. TypingMind runs on your own API keys, so you pay providers directly instead of a per-seat subscription, and your skills and chats stay in your own storage.

How do I install Pdf Watermark Removal in TypingMind?

Open Plugins → Skills → Install from GitHub in TypingMind and paste https://github.com/MikeWuPing/pdf-watermark-removal/tree/main/Skill/pdf-watermark-removal. TypingMind reads its SKILL.md and installs it as a skill you can enable per chat.

Which AI models can use Pdf Watermark Removal?

Any model you connect in TypingMind. AI skills are plain Markdown instructions rather than provider-specific code, so GPT, Claude, Gemini, Grok, and local models can all load this skill when a request matches it.

How many AI models can I use with Pdf Watermark Removal?

As many as you like. As long as a model supports skills, you can use Pdf Watermark Removal with it — GPT, Claude, Gemini, Grok, DeepSeek, Mistral, Llama and more — all on TypingMind with your own API keys.

Is the Pdf Watermark Removal AI skill free?

It is published on GitHub by MikeWuPing. Check the repository for licensing terms. You only pay your own AI provider for the tokens you use.

What are AI skills?

An AI skill is a reusable instruction bundle that teaches an AI model how to do one specific task. It follows the open Agent Skills format: a SKILL.md file with a name and description, plus any scripts, templates or reference files the model may need. The model reads the instructions only when your request matches the skill, so an installed skill costs nothing until it is used.

How are AI skills different from plugins or MCP servers?

A plugin or MCP server gives a model new tools to call — code that runs somewhere and returns a result. An AI skill gives the model knowledge and process instead: how to approach a task, which steps to follow, what good output looks like. Skills are plain Markdown, so they need no server, no API key and no runtime, and they work with any model.

View all

Set up your own AI workspace now

Get notified about new features and future giveaways by subscribing to our newsletter 👇