@krushna.barhate The following code generates multiple watermarks on the page:
Document doc = new Document("input.docx");
WatermarkHelper.InsertWatermarkText(doc, "Watermark");
doc.Save("output.docx");
public static class WatermarkHelper
{
private const int WATERMARK_FONT_WIDTH = 30;
private const int WATERMARK_FONT_HEIGHT = 30;
private const int WATERMARK_FONT_ROTATION = -45;
private static readonly Color watermark_color = Color.FromArgb(128, 128, 128, 128);
public static void InsertWatermarkText(Document doc, string watermarkText)
{
if (string.IsNullOrWhiteSpace(watermarkText))
{
throw new Exception("Watermark text is not configured, cannot add watermark to document");
}
foreach (Section section in doc.Sections)
{
InsertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(doc, watermarkText, section, HeaderFooterType.HeaderEven);
}
}
private static Shape BuildTextShape(Document doc, string watermarkText, double left, double top)
{
Shape watermark = new Shape(doc, ShapeType.TextPlainText)
{
TextPath = {
Text = watermarkText,
FontFamily = "Arial"
},
Width = watermarkText.Length * WATERMARK_FONT_WIDTH,
Height = WATERMARK_FONT_HEIGHT * 3,
Rotation = WATERMARK_FONT_ROTATION,
Fill = { Color = watermark_color },
StrokeColor = watermark_color,
Left = left,
Top = top,
WrapType = WrapType.None,
BehindText = true
};
return watermark;
}
private static void InsertWatermarkIntoHeader(Document doc, string watermarkText, Section section, HeaderFooterType headerType)
{
HeaderFooter header = section.HeadersFooters[headerType];
if (header == null)
{
header = new HeaderFooter(section.Document, headerType);
section.HeadersFooters.Add(header);
}
Random random = new Random();
Shape waterShape;
int beginLeft = -320 - random.Next(50);
for (int top = 50; top < 800; top += 250)
{
for (int left = beginLeft; left < 700; left += 300)
{
if (left == beginLeft)
{
int splitNo = new Random().Next(0, watermarkText.Length);
string firstWater = watermarkText.Substring(splitNo) + new string(' ', splitNo);
waterShape = BuildTextShape(doc, firstWater, left, top);
}
else
{
waterShape = BuildTextShape(doc, watermarkText, left, top);
}
Paragraph headerPara = (Paragraph)header.GetChild(NodeType.Paragraph, 0, true);
if (headerPara == null)
{
Paragraph watermarkPara = new Paragraph(doc);
watermarkPara.AppendChild(waterShape);
header.AppendChild(watermarkPara.Clone(true));
}
else
{
headerPara.AppendChild(waterShape);
}
}
}
}
}