Pdf增加水印

我需要给pdf文件增加水印,但是官方的示例代码只能增加单个字符串作为水印,我想知道如何让水印均匀重复在整个pdf页面里呢?在API中没有找到相应的参数和方法,提前感谢回复

我正在使用谷歌翻译来制作这段文字,以防万一读错了。

您希望水印是文本还是图像?

如果是文本,是否要从左下角到右上角沿对角线穿过页面?

I would like to add text watermark in pdf, but the example code about adding watermark could only add one line, is there any way to add text watermark which are repeated everywhere on the page ?

@wanghq09,

The code sample:

private void Logic()
{
    var doc = new Document($"{PartialPath}_input.pdf");

    foreach (var page in doc.Pages)
    {

        WatermarkArtifact artifact = new WatermarkArtifact();
        artifact.SetTextAndState(
            "My watermark centered!",
            new TextState()
            {
                FontSize = 85,
                ForegroundColor = Color.Red,
                BackgroundColor = Color.Red,
                Font = FontRepository.FindFont("Arial")
            });

        artifact.ArtifactVerticalAlignment = VerticalAlignment.Bottom;
        artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Left;

        artifact.Rotation = 55;
        artifact.Opacity = 0.5;
        artifact.LeftMargin = 70;

        artifact.IsBackground = false;

        page.Artifacts.Add(artifact);
    }

    doc.Save($"{PartialPath}_output.pdf");
}

The input and output files:
AddWatermarkToAllPages_input.pdf (267.3 KB)
AddWatermarkToAllPages_output.pdf (334.5 KB)

Thanks for reply. I didn’t express it clearly, what I want is many little text watermarks repeated on one page, not a single line text on every page. is there any way to achieve this?

@wanghq09,

If you need multiple lines, you can use the method artifact.SetLinesAndState instead.

If you want different watermark just add more artifacts.

image.jpg (169.8 KB)
is there any way to add watermark like this?

It works, thanks a lot.