Add/Remove Watermark more Intelligently...?

Hi all,

We are using the following class to remove watermarks from documents. The problem is that the business wants to put an Image in the header of the document for branding using the Word Templates, but when our code runs through the document it removes the watermark, aswell as the branding image!

Can someone suggest a good way to add/remove the watermark more intelligently to it doesn’t add/remove other items apart from the watermark?

private static void RemoveWatermark(Document doc)
{
    NodeCollection headerColl = doc.GetChildNodes(NodeType.HeaderFooter, true, false);

    if (headerColl == null || headerColl.Count == 0)
        return;

    Node[] headerNodes = headerColl.ToArray();

    int headerCount = headerNodes.Length;
    for (int i = 0; i <headerCount; i++)
    {
        HeaderFooter header = (HeaderFooter) headerNodes[i];
        if (header.IsHeader)
        {
            NodeCollection shapeColl = header.GetChildNodes(NodeType.Shape, true, false);
            if (shapeColl.Count> 0)
            {
                Node[] shapeNodes = shapeColl.ToArray();

                foreach(Node shape in shapeNodes)
                {
                    if (((Shape) shape).IsWordArt)
                        if (header.ParentSection != null)
                            header.Remove();
                }
            }
        }
    }
}

private static void InsertWatermarkText(Document doc, string watermarkText)
{
    // Create a watermark shape. This will be a WordArt shape.
    // You are free to try other shape types as watermarks.
    Shape watermark = new Shape(doc, ShapeType.TextPlainText);

    // Set up the text of the watermark.
    watermark.TextPath.Text = watermarkText;
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 400;
    watermark.Height = 190;
    // Text will be directed from the bottom-left to the top-right corner.
    watermark.Rotation = -45;
    // Remove the following two lines if you need a solid black text.
    watermark.FillColor = System.Drawing.Color.LightGray; // Try LightGray to get more Word-style watermark
    watermark.StrokeColor = System.Drawing.Color.LightGray; // Try LightGray to get more Word-style watermark

    // Place the watermark in the page center.
    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);

    // Insert the watermark into all headers of each document section.
    foreach(Section sect in doc.Sections)
    {
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);

        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);

        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
    }
}

Hi

Thanks for your inquiry. I think you can use Shape.Name to achieve this. You can set name of watermark shape during inserting and then remove watermark shape by this name.
You can use code like the following to set name of watermark shape:

// Set name of watermark shape.
// It is needed to be able to remove watermark programmatically.
watermark.Name = string.Format("WaterMark_{0}", Guid.NewGuid());

Here is code, which allow to remove watermark:

private static void RemoveWatermark(Document doc)
{
    NodeCollection shapesColl = doc.GetChildNodes(NodeType.Shape, true, false);
    Node[] shapesArray = shapesColl.ToArray();
    for (int i = 0; i <shapesArray.Length; i++)
    {
        Shape shape = (Shape) shapesArray[i];
        if (shape.Name.Contains("WaterMark"))
            shape.Remove();
    }
}

Hope this helps.
Best regards.