You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用C#和iTextSharp实现PDF全页45度重复对角水印的代码求助

C# iTextSharp 实现PDF全页45度重复对角线水印示例

依赖说明:使用前请先安装NuGet包 iTextSharp,.NET Core/.NET 5+ 环境可使用兼容包 iTextSharp.LGPLv2.Core。

效果参考:
水印示例图


完整实现代码

using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System;

/// <summary>
/// 为PDF添加全页铺满的45度重复对角线水印
/// </summary>
/// <param name="inputPdfPath">输入PDF文件路径</param>
/// <param name="outputPdfPath">输出带水印PDF文件路径</param>
/// <param name="watermarkText">水印文本内容</param>
/// <param name="fontSize">水印字体大小</param>
/// <param name="opacity">水印透明度,取值0-1,数值越小越透明</param>
/// <param name="gap">相邻水印的间距(像素)</param>
public static void AddDiagonalWatermark(string inputPdfPath, string outputPdfPath, string watermarkText, float fontSize = 30, float opacity = 0.2f, float gap = 80)
{
    using (var reader = new PdfReader(inputPdfPath))
    using (var fs = new FileStream(outputPdfPath, FileMode.Create, FileAccess.Write, FileShare.None))
    using (var stamper = new PdfStamper(reader, fs))
    {
        // 加载字体,如需支持中文请使用包含中文字符集的字体,示例为Windows自带黑体,Linux/macOS请替换对应字体路径
        BaseFont baseFont = BaseFont.CreateFont("c:/windows/fonts/simhei.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        
        // 计算水印文本尺寸
        float textWidth = baseFont.GetWidthPoint(watermarkText, fontSize);
        float textHeight = fontSize;

        // 遍历所有页面添加水印
        for (int page = 1; page <= reader.NumberOfPages; page++)
        {
            Rectangle pageSize = reader.GetPageSize(page);
            float pageWidth = pageSize.Width;
            float pageHeight = pageSize.Height;

            // GetOverContent为水印覆盖在内容上层,GetUnderContent为水印放在内容下层
            PdfContentByte content = stamper.GetOverContent(page);
            content.SaveState();

            // 设置透明度
            PdfGState gState = new PdfGState { FillOpacity = opacity, StrokeOpacity = opacity };
            content.SetGState(gState);
            content.SetColorFill(BaseColor.LightGray);
            content.BeginText();
            content.SetFontAndSize(baseFont, fontSize);

            // 循环铺满全页绘制45度水印
            for (float x = -pageHeight; x < pageWidth + pageHeight; x += textWidth + gap)
            {
                for (float y = -pageWidth; y < pageHeight + pageWidth; y += textHeight + gap)
                {
                    content.ShowTextAligned(Element.ALIGN_LEFT, watermarkText, x, y, 45);
                }
            }

            content.EndText();
            content.RestoreState();
        }
    }
}

调用示例

AddDiagonalWatermark(
    inputPdfPath: @"C:\Documents\原始文件.pdf",
    outputPdfPath: @"C:\Documents\带水印文件.pdf",
    watermarkText: "内部资料 禁止外传",
    fontSize: 28,
    opacity: 0.15f,
    gap: 70
);

注意事项

  • 中文水印乱码问题可通过替换字体路径解决,也可将字体文件嵌入项目中动态加载,避免依赖系统字体
  • 可根据实际需求调整透明度、字体大小、间距参数,适配不同的页面场景
  • 不需要水印遮挡正文时,将GetOverContent替换为GetUnderContent即可让水印显示在内容底层

内容的提问来源于stack exchange,提问作者Manish Pinjani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.17 15:54:03