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();