How to Add a Watermark to PDF Document and Center It in C#?

Hi can i add water mark pdf, and convert file pptx ppt to pdf and add water this ? do you have libery ?

i add watermark text pdf my rotation 30 or 52 my text watermark is email user
watermarkText = “phamquocvu19992@gmail.com” how i watermark is center pdf
image.png (81.4 KB)
my watermark
image.png (68.1 KB)

mywatermark
image.png (78.9 KB)

@chetdi117,
Please try using the following code example:

var watermarkText = "phamquocvu19992@gmail.com";

using (var presentation = new Presentation())
{
    var slideSize = presentation.SlideSize.Size;

    // Add the watermark shape.
    var watermarkShape = presentation.Slides[0].Shapes.AddAutoShape(
        ShapeType.Rectangle, 0, 0, slideSize.Width, slideSize.Height);

    // Set properties for the shape.
    watermarkShape.FillFormat.FillType = FillType.NoFill;
    watermarkShape.LineFormat.FillFormat.FillType = FillType.NoFill;
    watermarkShape.TextFrame.TextFrameFormat.AnchoringType = TextAnchorType.Center;
    watermarkShape.Rotation = -52;

    // Lock the shape.
    watermarkShape.ShapeLock.SelectLocked = true;
    watermarkShape.ShapeLock.SizeLocked = true;
    watermarkShape.ShapeLock.TextLocked = true;
    watermarkShape.ShapeLock.PositionLocked = true;
    watermarkShape.ShapeLock.GroupingLocked = true;

    // Add a new paragraph for the text.
    watermarkShape.TextFrame.Paragraphs.Clear();
    var parapraph = new Paragraph();
    watermarkShape.TextFrame.Paragraphs.Add(parapraph);

    // Set the text for the watermark shape.
    parapraph.Text = watermarkText;
    parapraph.ParagraphFormat.Alignment = TextAlignment.Center;

    // Set properties for the text.
    var portionFormat = parapraph.ParagraphFormat.DefaultPortionFormat;
    portionFormat.LatinFont = new FontData("Times New Roman");
    portionFormat.FontHeight = 40;
    portionFormat.FillFormat.FillType = FillType.Solid;
    portionFormat.FillFormat.SolidFillColor.Color = Color.LightGray;

    presentation.Save(folderPath + "output.pptx", SaveFormat.Pptx);
    presentation.Save(folderPath + "output.pdf", SaveFormat.Pdf);
}

Output: output.zip (40.9 KB)

Documents: Watermark
API Reference: IAutoShape interface

I mean watermark in pdf auto center this page have rotation 30 or 52 watermarkText = “phamquocvu19992@gmail.com” and not power point
This is my code can you suport me
image.png (81.4 KB)

@chetdi117,
I’ve moved this forum thread to Aspose.PDF forum. My colleagues will assist you soon.

@chetdi117,

Is this what you wanted to achieve?

private void Logic()
{
    var doc = new Document();

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

    artifact.ArtifactVerticalAlignment = VerticalAlignment.Center;
    artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
    
    artifact.Rotation = 30;
    artifact.Opacity = 0.5;           
    
    artifact.IsBackground = false;

    var page = doc.Pages.Add();

    page.Artifacts.Add(artifact);

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

The output file:
AddingWatermarkArtifactCentered_output.pdf (78.5 KB)

i mean my watermark
image.png (29.7 KB)

look like that
screencapture-localhost-44309-Home-DownloadReportAttachment-1062-2023-04-07-10_14_30.png (29.2 KB)

@chetdi117,

Can you clarify what you want? Because I already provide the code to do it, you only have to change the text.

So I am a bit confused. Sorry.

Im try it not wrok form my the text out of doc no center

this is my result
image.png (23.1 KB)
I wanant like this
screencapture-localhost-44309-Home-DownloadReportAttachment-1062-2023-04-07-10_14_30.png (29.2 KB)

@chetdi117,

In order to successfully move around the watermark, think that the positioning is where it starts.

So first change will be:

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

Keep in mind that a page is not a square so the Rotation cannot be 45.

So play around with the rotation value:

artifact.Rotation = 45; //Just an example

When giving a rotation and if the watermark is too close to the borders, sometimes part falls outside the page. To avoid this, use the margins properties. The bigger the font, the bigger the margin.

Code Sample

private void Logic()
{
    var doc = new Document();

    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;

    var page = doc.Pages.Add();

    page.Artifacts.Add(artifact);

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

The output:
AddingWatermarkArtifactCentered_output.pdf (78.5 KB)