Free Support Forum - aspose.com

Setting correct font size for watermark; further watermark questions

Dear Aspose team!

Currently, I'm working on a watermark issue and due to your great code samples, it was an easy thing to create some nice samples.

Although I'm very pleased with the results, there are some advanced things which are not completely clear to me:

1. When I'm setting the font's size by Shape#TextPath.Size, the Font in the watermark seems to be bigger than the Word font size that I would expect, e.g. setting size 48 looks biggger in the watermark than in usual Word text.

Do I have to do some conversions, e.g. by Aspose.Words.ConvertUtil? All things that I tried did not work...

2. How do I have to set the correct Shape width so that the text perfectly fits?

I've done this in a "shot from the hip" way as you can see below - it seems to work fine but I don't know if this works for all fonts. Is there a better or more elegant way? Please keep in mind that the length of the text varies

3. I would like to reproduce the Word watermark features "semitrans" for text and "washout" for image watermarks.

Unfortunately, I did not find any properties in Shape, Image, TextPath etc. Do you know how to realize these features?

Here you find the sample code (TextWatermarkData is our own simple data holder class):

var textWatermarkData = (TextWatermarkData) WatermarkData;
doc = new Document(documentPath);
watermark = new Shape(doc, ShapeType.TextPlainText);

watermark.TextPath.Text = textWatermarkData.Text;
watermark.TextPath.FontFamily = textWatermarkData.Font;
watermark.Fill.Color = textWatermarkData.Color;
watermark.Stroked = false;
watermark.WrapType = WrapType.None;
watermark.BehindText = true;

watermark.TextPath.Size = textWatermarkData.Size;

watermark.Height = textWatermarkData.Size;

//START NOTE: this is a HACK that calculates the required widths for the text
watermark.Width = textWatermarkData.Text.Length * 3 * (watermark.Height/4);
//END NOTE


if(textWatermarkData.IsDiagonal)
{
watermark.Rotation = -45;
}
else
{
watermark.Rotation = -90;
}

If you require further information, please feel free to ask.

Thanks for your efforts and your excellent support,

cheers,

Stephan

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry.

1. As can see, the same thing occur when you insert WordArt object in MS Word, i.e. if you insert WordArt object and specify Arial 48 font and insert plain text with the same font, you will be able to notice difference in font size. I suppose, this occurs because text effect of WordArt objects. Also, font size does not have any effect if you specify width and height of the shape.

2. You can use an approximate formula to calculate this

Width = [text lenght]*[font size]/2.

Here is simple code example, which shows how this should be implemented:

///

/// Inserts a watermark into a document.

///

/// The input document.

/// Text of the watermark.

private static void InsertWatermarkText(Document doc, string watermarkText)

{

// Create a watermark shape. This will be a WordArt shape.

// You are free to try other shape types as watermarks.

Shape watermark = new Shape(doc, ShapeType.TextPlainText);

// Set up the text of the watermark.

watermark.TextPath.Text = watermarkText;

watermark.TextPath.FontFamily = "Arial";

// Font size does not have effect if you specify height of the shape.

// So you can just specify height instead of specifying font size.

double fontSize = 100;

watermark.Height = fontSize;

// Widht of each leter is aproximately half of font size.

// So we can use thefollowing formula to calculate widht of the shape (approximately).

watermark.Width = watermarkText.Length*fontSize/2;

// Text will be directed from the bottom-left to the top-right corner.

watermark.Rotation = 315;

// Remove the following two lines if you need a solid black text.

watermark.Fill.Color = Color.Gray; // Try LightGray to get more Word-style watermark

watermark.StrokeColor = Color.Gray; // Try LightGray to get more Word-style watermark

// Place the watermark in the page center.

watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;

watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;

watermark.WrapType = WrapType.None;

watermark.VerticalAlignment = VerticalAlignment.Center;

watermark.HorizontalAlignment = HorizontalAlignment.Center;

// Create a new paragraph and append the watermark to this paragraph.

Paragraph watermarkPara = new Paragraph(doc);

watermarkPara.AppendChild(watermark);

// Insert the watermark into all headers of each document section.

foreach (Section sect in doc.Sections)

{

// There could be up to three different headers in each section, since we want

// the watermark to appear on all pages, insert into all headers.

InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);

InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);

InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);

}

}

private static void InsertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, HeaderFooterType headerType)

{

HeaderFooter header = sect.HeadersFooters[headerType];

if (header == null)

{

// There is no header of the specified type in the current section, create it.

header = new HeaderFooter(sect.Document, headerType);

sect.HeadersFooters.Add(header);

}

// Insert a clone of the watermark into the header.

header.AppendChild(watermarkPara.Clone(true));

}

3. In case of using WordArt watermark, you can just specify shape transparency:

watermark.Fill.Opacity = 0.5;

In case of using image watermark, you can play with Brightness and Contrast.

watermark.ImageData.SetImage(@"C:\Temp\test.jpg");

watermark.ImageData.Brightness = 0.85;

watermark.ImageData.Contrast = 0.15;

Hope this helps.

Best regards.

Hi Alexey!

Thank you very much for your quick reply - this looks very good. I'll try this today and check how it works!

Thanks a lot,

cheers,

Stephan

Hi Alexey!

Excellent, your example is exactly what I needed! Thanks a lot!

If I got you right, there is currently no proper way to display the text size according to its actual size in Word because the Shape concept for texts depends on WordArt. And the WordArt feature displays sizes bigger than they are. Correct?

Thank you very much,

cheers,

Stephan

Hi Stephan,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your inquiry. WordArt shapes are used to insert text watermarks (in both in MS Word and in Aspose.Words). In WordArt, text adjusts to the shape size, so font size does not have any effect.

Best regards.

Hi Alexey!

Very good to know - thanks again!

Cheers,

Stephan