Problems with removing watermark

Hi,

I am generating a document from a .dot file.
The problems that I am facing are that if I do the following it has no effects to the generated document:

private void RemoveWatermark()
{

NodeCollection shapes = document.GetChildNodes(NodeType.Shape, true, false);

foreach(Aspose.Words.Drawing.Shape shape in shapes)
{
if (shape.TextPath.Text == "DRAFT")
{
shape.Remove();
//shapes.Remove(shape);
break;
}
}
}

Afterwards I generate the document with that code:

using(MemoryStream ms = new MemoryStream())
{
document.Save(ms, SaveFormat.Doc);
bytes = ms.ToArray();
}


The thing is that the watermark still remains in the document.

Aspose.Words Version = 4.0.2, licenced



Any help appreciated,



Dimitrios Toulakis (RESC)


Thanks for reporting this issue to us. Please attach the processed document here. I will check what could be done.

Best regards,

After some investigation I came up that there must be somewhere a difference in the handling of files.

I have attached 2 documents, 1 where the above code is working & 1 where the above code is not working.

Maybe you can investigate the reason for the problem easier.

Dimitrios Toulakis

Please note that in not.working.dot the watermark is defined three times - one for every header type - first page, odd, and even. You are removing only the wateramrk defined for the first page. Don't use break; in your code and it will be able to remove all three watermark definitions successfully.

Best regards,

When you remove nodes from a collection while you iterate, it is better to use ToArray first and remove during iteration through the array.

NodeCollection nodeCollection = doc.GetChildNodes(...);

Node[] nodes = nodeCollection.ToArray();

foreach (Node in nodes)

{

// It is safe to do node.Remove() here.

}

The reason for that is that when you obtain NodeCollection, even a "not live" one, all matchings nodes are not prefecthed . They are retrieves from the document on demand. So when you delete a current node and attempt to iterate to next one, especially using iterator, you go nowhere after the first node.

The "live" or "non live" just determines whether the cache of prefetched nodes is flushed when document is modified.

I know its a bit complex, but that's the best we can do to provide a flat view of a tree.

Thank you for your input. That solved my problem.

Regarding using the .ToArray() method: already doing it :wink:

Dimitrios Toulakis