We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Page centered watermark

Hi,

We need some help on how to correctly apply a page centered watermark (both vertical and horizontal). We are able to make the watermark centered on horizontally but we cannot do it vertically.

Details:
1. Here is the code we are using:

var textStamp = new AsposePdf.TextStamp(watermark)
{
Background = false,
HorizontalAlignment = AsposePdf.HorizontalAlignment.Center,
VerticalAlignment = AsposePdf.VerticalAlignment.Center,
TextAlignment = AsposePdf.HorizontalAlignment.Left,
Rotate = AsposePdf.Rotation.None,
Opacity = .2f,
Width = page.Rect.Width - 80,
Height = page.Rect.Height - 200,
WordWrap = true,
Scale = true,
YIndent = 200,
};

textStamp.TextState.Font = FontRepository.FindFont(“Arial”);
textStamp.TextState.FontSize = 50.0F;
textStamp.TextState.FontStyle = FontStyles.Bold;
textStamp.TextState.ForegroundColor = AsposePdf.Color.FromRgb(Color.Red);
textStamp.TextState.HorizontalAlignment = AsposePdf.HorizontalAlignment.Center;

pdfDocument.Pages[i].AddStamp(textStamp);

2. We are using version 17.3 of Aspose.Pdf library.

3. We attached the result. You can clearly see that the watermark is not vertically centered.

How can we achieve this?

Thank you.

Hi Panos,


Thanks for contacting support.

You do not need to set YIndent, Height and Width of the TextStamp, when you are setting it Vertical and Horizontal Alignment. Which is why the watermark was not appearing in the center as it was taking other position parameters separately.

Please check following code snippet which I have used to add watermark in the PDF file which you have shared. For your reference, I have also attached resultant file.

using (Document pdfDocument = new Document(dataDir + “Page+centered+watermark.pdf”))<o:p></o:p>

{

foreach (Page page in pdfDocument.Pages)

{

TextStamp textStamp = new TextStamp("Test Watermark")

{

Background = false,

HorizontalAlignment = HorizontalAlignment.Center,

VerticalAlignment = VerticalAlignment.Center,

TextAlignment = HorizontalAlignment.Left,

Rotate = Rotation.None,

Opacity = .2f,

//Width = page.Rect.Width - 80,

//Height = page.Rect.Height - 200,

WordWrap = true,

Scale = true

//YIndent = 200,

};

textStamp.TextState.Font = FontRepository.FindFont("Arial");

textStamp.TextState.FontSize = 50.0f;

textStamp.TextState.FontStyle = FontStyles.Bold;

textStamp.TextState.ForegroundColor = Aspose.Pdf.Color.Purple;

page.AddStamp(textStamp);

}

pdfDocument.Save(dataDir + "CenteredWatermark.pdf");

}



In case of any further assistance, please feel free to contact us.


Best Regards,

Hi,

Thank you for the quick response!

The code you send works perfect with a small watermark. With a watermark having 100 chars it will behave awkward. I can barely see the watermark now. Please check the attached PDF.

What I did:
1. Used the code you sent.
2.
Used the watermark ‘Controlled Document, 1. Controlled Document, 2.
Controlled Document, 3. Controlled Document, 4. Cont’ that has 100
chars.

We need to support big watermarks as well as they are managed by our users and are dynamically added in the PDF.

Are there any other ways to add watermarks?

Thank you!

Hi Panos,

Thanks for sharing more details.

I have tested the scenario with long text and observed the issue which you have mentioned. I have also tried to add watermark with Artifact Class, provided by Aspose.Pdf for .NET, but the issue remained same. For your reference, please check following code snippet which I have tried.

Document doc = new Document();

Page p = doc.Pages.Add();

Artifact af = new Artifact(Artifact.ArtifactType.Background, Artifact.ArtifactSubtype.Watermark);

//af.SetImage(dataDir + "aspose.jpg");

//af.SetText(new Facades.FormattedText("Controlled Document, 1. Controlled Document, 2. Controlled Document, 3. Controlled Document, 4. Cont"));

af.ArtifactVerticalAlignment = VerticalAlignment.Center;

af.ArtifactHorizontalAlignment = HorizontalAlignment.Center;

af.SetTextAndState("Controlled Document, 1. Controlled Document, 2. Controlled Document, 3. Controlled Document, 4. Cont", new TextState() { FontSize = 50.0f, Font = FontRepository.FindFont("Arial"), ForegroundColor = Color.Purple });

af.Opacity = .2f;

p.Artifacts.Add(af);

doc.Save(dataDir + "Artifacts.pdf");



I have logged an issue as PDFNET-42906 in our issue tracking system for the purpose of investigation. We will further look into the details and keep you informed with status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.


Best Regards,