How to add watermark under the text of document using .NET

I have searched and tried so many different ways to stick a watermark onto a document that I doubt it’s even possible…

I have a header, a body, and a footer, which all print to the document fine, but… when trying to add a watermark, it simply sticks the watermark under my existing header when actually I want it to “float” at the top left of the page.

I’ve tried not having any existing header, then letting this be my first, and still it simply sticks it into the header.

Basically I think it’s just outputting the image inline.

I want the image to be a proper watermark, floating beneath any content, to where I specifiy.

Can anyone help?

The Code:

private static void insertWatermarkImage(Aspose.Words.Document doc, String watermarkImagePath)
{
Shape watermark = new Shape(doc, ShapeType.Image);

// Set up the the watermark.
watermark.ImageData.SetImage(watermarkImagePath);
watermark.Width = 500;
watermark.Height = 100;

// Place the watermark in the page center.
watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
watermark.WrapType = WrapType.None;
watermark.VerticalAlignment = VerticalAlignment.Top;
watermark.HorizontalAlignment = HorizontalAlignment.Left;

// 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 (Aspose.Words.Section sect in doc.Sections)
{
insertWatermarkIntoHeader(watermarkPara, sect, (int)HeaderFooterType.HeaderPrimary);
//insertWatermarkIntoHeader(watermarkPara, sect, (int)HeaderFooterType.HeaderFirst);
//insertWatermarkIntoHeader(watermarkPara, sect, (int)HeaderFooterType.HeaderEven);
}

}

private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Aspose.Words.Section sect, int headerType)
{

HeaderFooter header = sect.HeadersFooters[HeaderFooterType.HeaderPrimary];

if (header == null)
{
// There is no header of the specified type in the current section, create it.
header = new HeaderFooter(sect.Document, HeaderFooterType.HeaderPrimary);
sect.HeadersFooters.Add(header);
}

// Insert a clone of the watermark into the header.
header.AppendChild(watermarkPara.Clone(true));
}

I seem to be having a little more joy by using a background shape but when it saves to PDF I cannot stop it from repeating nor stop the image from stetching… :frowning:

Shape shape = new Shape(doc, ShapeType.Rectangle);// ShapeType.Image);
shape.ImageData.SetImage(imgPath);//, 450, 84);
shape.WrapType = WrapType.None;
shape.BehindText = true;

doc.BackgroundShape = shape;

It looks ideal in .doc form, the only difference from my end is the extension

Here’s an example of the bad PDF save:


Hi David,

Thanks for your query. Please use the latest version of Aspose.Words for .NET. I have used your code and have not found any issue with water mark in
PDF file. Please find the PDF file generated with your code in attachment.