Multiline TextWatermark

Skip Navigation LinksHome  /  Support  /  Forums  /  DynamicPDF CoreSuite for .NET (v12)  /  Multiline TextWatermark

DynamicPDF CoreSuite for .NET (v12) Forum

 Aug 02 2024 6:17 AM
Hi,
I'm considering migration from the old PDF generation library to Dynamic PDF and currently testing existing functionality based on the trial Dynamic PDF license.|
During the testing of multiline text watermark I faced the issue that the watermark is not centered(it's shifted to the left top corner of the page).

Let's consider code sample:

var document = new Document();
document.Pages.Add(new Page(PageSize.Letter));
var watermarks = new List<string>
{
    "This is an example of Draft watermark",
    "This is an example of Draft watermark",
    "This is an example of Draft watermark"
};

var watermarkText = string.Join(Environment.NewLine, watermarks);

foreach (Page page in document.Pages)
{
    var textWatermark = new TextWatermark(watermarkText)
    {
        Angle = 45,
        Opacity = 0.2f,
        Font = Font.Helvetica,
        FontSize = 12,
        Position = WatermarkPosition.Center,
        TextColor = RgbColor.Blue,

    };
    page.Elements.Add(textWatermark);
}

document.Draw("output.pdf");

Everything works as expected to me, except the location of the watermark.
Could you please help me to resolve this issue?
 Sep 10 2024 1:36 AM
 Oct 09 2024 5:45 PM
If you remove the line breaks and have a single string of text, it will be lined up as you expect. That text length gets taken into account when displaying the watermark. (Could be a bug... maybe not sure?) Most of the watermarks I used just needed a single line of text.

To get around it, trying using multiple watermarks and use the y offset to get what you desire.

var watermarkText = "This is an example of Draft watermark";

            foreach (Page page in document.Pages)
            {
                var textWatermark = new TextWatermark(watermarkText)
                {
                    //Angle = 0,
                    Opacity = 0.2f,
                    Font = Font.Helvetica,
                    FontSize = 30,
                    Position = WatermarkPosition.Center,
                    TextColor = RgbColor.Blue,
                    RelativeTo = RelativeTo.TopCenter,
                    IgnoreMargins = false,
                    AutoScale=true
                };
                page.Elements.Add(textWatermark);


                var textWatermark2 = new TextWatermark(watermarkText)
                {
                    //Angle = 0,
                    Opacity = 0.2f,
                    Font = Font.Helvetica,
                    FontSize = 30,
                    Position = WatermarkPosition.Center,
                    TextColor = RgbColor.Blue,
                    RelativeTo = RelativeTo.TopCenter,
                    IgnoreMargins = false,
                    AutoScale = true,
                    YOffset = 100
                   
                };
                textWatermark2.YOffset = 100;
                page.Elements.Add(textWatermark2);

                var textWatermark3 = new TextWatermark(watermarkText)
                {
                    //Angle = 0,
                    Opacity = 0.2f,
                    Font = Font.Helvetica,
                    //FontSize = 30,
                    Position = WatermarkPosition.Center,
                    TextColor = RgbColor.Blue,
                    RelativeTo = RelativeTo.TopCenter,
                    IgnoreMargins = false,
                    AutoScale = true,
                    YOffset = 200

                };
                page.Elements.Add(textWatermark3);
            }

All times are US Eastern Standard time. The time now is 7:27 PM.