Text watermark with automatic width?

I want to add a text watermark that is centered in the middle of the page and is displayed with a known point size. I can get everything working except the correct width. Here is my current code for attempting to get this working, it is based on the code in the help documentation…

Dim shape As New Aspose.Words.Drawing.Shape(_Document, Drawing.ShapeType.TextPlainText)

' Place the watermark shape in the page center
shape.WrapType = WrapType.None
shape.RelativeHorizontalPosition = RelativeHorizontalPosition.Page
shape.RelativeVerticalPosition = RelativeVerticalPosition.Page
shape.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center
shape.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center

' Show red text in Arial, 36pt
shape.StrokeColor = Colors.Red
shape.FillColor = Colors.Red
shape.TextPath.Text = "Confidential"
shape.TextPath.FontFamily = "Arial"
shape.TextPath.Size = 36
shape.Height = 36
shape.Width = (What to put here)
' Use code form help documentation to add to headers
InsertWatermarkIntoHeader(Paragraph, HeaderFooterType.HeaderPrimary)
InsertWatermarkIntoHeader(Paragraph, HeaderFooterType.HeaderFirst)
InsertWatermarkIntoHeader(Paragraph, HeaderFooterType.HeaderEven)

This works in that the text is displayed and is in the center of the page. But I do not know how to get the width correct for the known point size. I cannot use a fixed size for the shape.Width because although this example uses a fixed “Confidential” string in practice the string could be anything when I need to build the document. Any ideas?

Hi
Thanks for your request. There is no direct way to calculate width of text. However, nothing is impossible. You can approximately calculate width of each character as half of font size. Also, you can try using System.Drawing to measure text. For instance, the following code approximately calculate width of text of specified font and size:

///
/// Method calculate an aproximate width of text.
///
private double CalculateWidhtOfText(string fontName, float size, string text)
{
    double width = 0;
    using(Bitmap bmp = new Bitmap(1, 1))
    {
        bmp.SetResolution(96, 96);
        using(Graphics g = Graphics.FromImage(bmp))
        {
            using(System.Drawing.Font f = new System.Drawing.Font(fontName, size))
            {
                SizeF textSize = g.MeasureString(text, f);
                width = textSize.Width;
            }
        }
    }
    return width;
}

Hope this code could be useful for you.
Best regards,

Hi there,
Thanks for your inquiry.
I think in your case you may just want to center the text within the watermark shape and then make the width of the shape as wide as the page. As long as there is no background on the shape the width shouldn’t matter.
If you are having any troubles could you please attach your document here for testing?
Thanks,

Thanks, I have fixed the issue by simply remembering the actual width that is used in my GUI application and then reusing that width when generating the output document.

Hi
It is perfect that you managed to resolve the problem. Please let us know if you need more assistance, we will be glad to help you.
Best regards,