Insert Watermark Fails

Hi, I have used the following sample code to insert a watermark into a document, but this does not appear. Can you have a look at the code below and let me know if there are any issues you see? I have attached the document template and image.

Thanks, Rod.

//Create a document builder that will allow us to modify the document.
DocumentBuilder builder = new DocumentBuilder(doc);
//The cursor is in the first section already, navigate to the header as this is where
//we want to insert the watermark.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
System.Drawing.Image image = System.Drawing.Image.FromFile(Server.MapPath(".") + @"\Images\brandBHPBilliton.gif");
//Need to calculate page width and height without margins because we use margin-relative
//positioning in this example. It would be easier to use page-relative positioning, but
//MS Word does not seem to recognize it, will improve later.
double useablePageWidth = builder.PageSetup.PageWidth - builder.PageSetup.LeftMargin - builder.PageSetup.RightMargin;
double useablePageHeight = builder.PageSetup.PageHeight - builder.PageSetup.TopMargin - builder.PageSetup.BottomMargin;
//Calculate image width and height in points.
const int PointsPerInch = 72;
double imageWidth = (image.Width / image.HorizontalResolution) * PointsPerInch;
double imageHeight = (image.Height / image.VerticalResolution) * PointsPerInch;
//Calculate image left and top position so it appears in the centre of the page.
double imageLeft = (useablePageWidth - imageWidth) / 2;
double imageTop = (useablePageHeight - imageHeight) / 2;
//Insert a floating picture.
builder.InsertImage(
image,
RelativeHorizontalPosition.Margin,
imageLeft,
RelativeVerticalPosition.Margin,
imageTop,
imageWidth,
imageHeight,
WrapType.None, //Don’t need wrapping
WrapSide.Both, //Does not matter in this case
true,
null);
//Insert a table with other office information in the footer.
builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

Border topBorder = builder.CellFormat.Borders[BorderType.Top];
topBorder.LineStyle = LineStyle.Single;
topBorder.LineWidth = 1;
builder.InsertCell();
builder.CellFormat.Width = 3 * 72;
builder.Write(string.Format("Processed on: { 0}", DateTime.Today.ToLongDateString()));
builder.InsertCell();
builder.ParagraphFormat.Alignment = ParagraphAlignment.Right;
builder.Write("Version 1.00");
builder.EndRow();
builder.EndTable();

Hi,

Thank you for considering Aspose.

The point is that you are inserting the watermark into primary header of the current (first) document section. However, your document consists of two sections and the primary header of the first section is not shown so the inserted image is invisible.

If you want the watermark for the whole document, you should insert the image into all the headers of all the sections. You can extract the code to a separate method and pass it a DocumentBuilder that preliminarily moves to a section using DocumentBuilder.MoveToSection and then moves to all types of header using DocumentBuilder.MoveToHeaderFooter, something like this:

for (int sectionIndex = 0; sectionIndex < doc.Sections.Count; sectionIndex++)
{
    builder.MoveToSection(sectionIndex);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
    InsertWatermark(builder, image);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderFirst);
    InsertWatermark(builder, image);
    builder.MoveToHeaderFooter(HeaderFooterType.HeaderEven);
    InsertWatermark(builder, image);
}