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

Free Support Forum - aspose.com

Watermark must be on top of all elements

I added watermark to existed document using aspose words, but i need watermark on top. All the text and images should be behind it. If any one knows how to achieve it please help me



See the code i have used

private void InsertWatermarkText(Document doc)
{
Shape watermark = new Shape(doc, ShapeType.TextPlainText);
watermark.TextPath.Text = “Watermark Text added using aspose”;
watermark.TextPath.FontFamily = “Arial”;
watermark.BehindText = false;
watermark.Width = 500;
watermark.Height = 45;
watermark.ZOrder = 1000;
watermark.Rotation = -30;
watermark.Fill.Color = Color.Gray;
watermark.StrokeColor = Color.Gray;
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);

foreach (Section sect in doc.Sections)
{
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
}



private 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);
}
header.AppendChild(watermarkPara.Clone(true));
}
private void InsertWatermarkText(Document doc)
{
DocumentBuilder builder = new DocumentBuilder(doc);
Shape watermark = new Shape(builder.Document, ShapeType.TextPlainText);
watermark.TextPath.Text = "Watermark Text added using aspose";
watermark.TextPath.FontFamily = "Arial";
watermark.BehindText = false;
watermark.Width = 500;
watermark.Height = 45;
watermark.ZOrder = 35;
watermark.Rotation = -30;
watermark.Fill.Color = Color.Gray;
watermark.StrokeColor = Color.Gray;
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Center;
watermark.HorizontalAlignment = HorizontalAlignment.Center;
builder.InsertNode(watermark);

Hi Pavan,

Thanks for your inquiry. The problem occurs because watermark shape resides inside header footer story and main content/elements are inside body story (please see Story class). If you insert a watermark using Microsoft Word 2013 in source document, you will observe the same behaviour. However, you can overcome this problem by manually inserting watermarks in each Page. You can achieve this by moving the cursor to the first Paragraph in each Page of your document and then making those Paragraphs as an anchor points for your watermarks. Here is how you can achieve this:

public static void InsertWatermarkTextAtEachPage(Document doc, string watermarkText)

{

DocumentBuilder builder = new DocumentBuilder(doc);

PageSetup ps = builder.PageSetup;

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

LayoutCollector collector = new LayoutCollector(doc);

Paragraph anchorPara = null;

int pageIndex = 1;

foreach (Paragraph para in paragraphs)

{

if (collector.GetStartPageIndex(para) == pageIndex && para.GetAncestor(NodeType.GroupShape) ==null)

{

anchorPara = para;

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

watermark.TextPath.Text = watermarkText;

watermark.TextPath.FontFamily = "Arial";

watermark.Width = 300;

watermark.Height = 70;

watermark.Left = 100;

watermark.Top = 100;

watermark.Rotation = -40;

watermark.Fill.Color = Color.Gray;

watermark.StrokeColor = Color.Gray;

watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());

watermark.WrapType = WrapType.None;

anchorPara.AppendChild(watermark);

pageIndex++;

}

}

}


I hope, this helps.

Best regards,