python向pdf添加水印

from typing import Union, Tuple
from reportlab.lib import units
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont

pdfmetrics.registerFont(TTFont('msyh', r'./msyh.ttc'))

'''

用于生成包含content文字内容的水印pdf文件

content: 水印文本内容
filename: 导出的水印文件名
width: 画布宽度,单位:mm
height: 画布高度,单位:mm
font: 对应注册的字体代号
fontsize: 字号大小
angle: 旋转角度
text_stroke_color_rgb: 文字轮廓rgb色
text_fill_color_rgb: 文字填充rgb色
text_fill_alpha: 文字透明度


'''


def create_wartmark(content: str,
                    filename: str,
                    width: Union[int, float],
                    height: Union[int, float],
                    font: str,
                    fontsize: int,
                    angle: Union[int, float] = 45,
                    text_stroke_color_rgb: Tuple[int, int, int] = (0, 0, 0),
                    text_fill_color_rgb: Tuple[int, int, int] = (0, 0, 0),
                    text_fill_alpha: Union[int, float] = 1) -> None:
    # 创建PDF文件,指定文件名及尺寸,以像素为单位
    c = canvas.Canvas(f'{filename}.pdf', pagesize=(width * units.mm, height * units.mm))

    # 画布平移保证文字完整性
    c.translate(0.1 * width * units.mm, 0.1 * height * units.mm)

    # 设置旋转角度
    c.rotate(angle)

    # 设置字体大小
    c.setFont(font, fontsize)

    # 设置字体轮廓彩色
    c.setStrokeColorRGB(*text_stroke_color_rgb)

    # 设置填充色
    c.setFillColorRGB(*text_fill_color_rgb)

    # 设置字体透明度
    c.setFillAlpha(text_fill_alpha)

    # 绘制字体内容
    c.drawString(0, 0, content)

    # 保存文件

    c.save()


create_wartmark(content='陈文肯',
                filename='水印',
                width=200,
                height=200,
                font='msyh',
                fontsize=40,
                text_fill_alpha=0.3)


from typing import List
from pikepdf import Pdf, Page, Rectangle

'''
向目标pdf文件批量添加水印
target_pdf_path:目标pdf文件路径+文件名
watermark_pad_path:水印pdf文件路径+文件名
nrow:水印平铺的行数
ncol:水印平铺的列数
skip_pages:需要跳过不添加水印的页数

'''


def add_watemark(target_pdf_path: str,
                 watermark_pdf_path: str,
                 nrow: int,
                 ncol: int,
                 skip_pages: List[int] = []) -> None:
    # 选择需要添加水印的pdf文件
    target_pdf = Pdf.open(target_pdf_path)

    # 读取水印pdf文件并提取水印
    watermark_pdf = Pdf.open(watermark_pdf_path)
    watermark_page = watermark_pdf.pages[0]

    # 遍历目标pdf文件中的所有页,批量添加水印
    for idx, target_page in enumerate(target_pdf.pages):
        for x in range(ncol):
            for y in range(nrow):
                # 向目标页指定范围添加水印
                target_page.add_overlay(watermark_page,
                                        Rectangle(target_page.trimbox[2] * x / ncol,
                                                  target_page.trimbox[3] * y / nrow,
                                                  target_page.trimbox[2] * (x + 1) / ncol,
                                                  target_page.trimbox[3] * (y + 1) / nrow
                                                  ))
    # 保存PDF文件,同时对pdf文件进行重命名,从文件名第7位置写入后缀名
    target_pdf.save(r'E:\Work\_已添加水印.pdf')

add_watemark(target_pdf_path=r'E:\Work\《电子学会》2022年3月C语言一级真题(含答案).pdf',
             # 把生成的水印示例,添加到目标水印文件中
             watermark_pdf_path=r'E:\Work\水印.pdf',
             nrow=3,
             ncol=2,
             skip_pages=[0])
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ChenWenKen

你的鼓励是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值