I Create watermark in pdf file auto center have role

I have two type pdf i want add water mark center like this, my water is email user my mail “phamquocvu19992@gmail.com” maybe long or sort then
pdf type 1:
image.png (37.2 KB)
pdf type 2:
image.png (27.3 KB)
how i can auto center role water mark diagonal line pdf
like this
image.png (11.7 KB)
and this
image.png (16.3 KB)
my file
pdf-file.zip (62.2 KB)

@chetdi117,

You cannot autocenter a watermark in a diagonal. You need to take into consideration page size, font, margins, etc.

There is multiple ways to stamp or watermark documents like Stamping with Aspose.PDF using C#|Aspose.PDF for .NET or PDF Annotations|Aspose.PDF for .NET

But this is my favorite one for text:

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");
}

You can play around with this code sample. Keep in mind that when you want the diagonal text, the angle must be precise to the page size, and some of the text will be put outside of the screen because of the angle. This is where the Left margin comes in place to push it into the page so we can see the whole watermark.

not work for me my water mark is email user login no fixed length
i set font size too high can overflow the diagonal border, your client is right on the diagonal and automatically center the page diagonal, can you help me

@chetdi117,

I do not understand what you need help with.

I give you the tools to do everything. Change the font, the angle, the opacity, alignment, etc. All you have to do is modify the values.

If you have a question about functionality and how it works, I can help with that. But you have to tell me specifically what you do not understand.

If it is about centering, modify this line: artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Left;
to the alignment that you need