Retrieving Watermark Information

Hi,

I have created a document with simple watermark. How to take the watermark content and properties using Aspose words for java. Please help me.

Note:
I will process each node in the document. What type of node the watermark is?.After visiting the node how should i access the watermark properties?

Hi
Thanks for your inquiry. Watermark in MS Word is a simple shape inserted into the header. However, you can indentify this shape in order to get its properties. You can use Shape.Name to achieve this because when you insert a watermark in Ms Word, it sets name of shape like this – “PowerPlusWaterMarkObject357476642”. So if the name of shape contains “WaterMark” substring, you can suppose that this shape is a watermark.
So you can use code like the following to get a watermark shape:

// Open document.
Document doc = new Document("C:\\Temp\\watermark.docx");
// Get all shapes formthe document.
NodeCollection <Shape> shapes = doc.getChildNodes(NodeType.SHAPE, true);
// Look for watermark shape.
for (Shape shape: shapes)
{
    if (shape.getName().contains("WaterMark"))
    {
        // Here you can access watermark shape properties.
        // ..............................................
    }
}

Hope this helps.
Best regards.

Hi,
Thanks for your reply. I should not take all the shapes at a time. I have to visit each node of the document in sequence. While i performing such process, i couldn’t visit the node Shape.It shows me that document has only paragraph.

here this.node refers body node

NodeCollection nodes = ((CompositeNode) this.node).getChildNodes();
for (int i = 0; i <nodes.getCount(); i++)
{
    Node node = (Node) nodes.get(i);
    System.out.println("Node Type = " + node.getNodeType());
}

Result: Node Type = paragraph

Hi
Thanks for your request. Shape nodes are usually children of Paragraph nodes. In your code you just iterate over node on one level, but do not iterate through nested nodes.
I suppose, in your case, it would be better to use DocumentVisitor. Please follow the link for more information:
https://reference.aspose.com/words/java/com.aspose.words/DocumentVisitor
Hope this helps.
Best regards,

Hi,

Thanks for your reply. I tried the following code to visit the Nodes in Document. But the shape is not available as a child of paragraph.

Document doc = new Document("src/input/watermark1.doc");
NodeCollection bodyNodes = doc.getChildNodes(NodeType.BODY, true);
for (int i = 0; i <bodyNodes.getCount(); i++)
{
    if (bodyNodes.get(i).isComposite())
    {
        System.out.println("Main Node Name= " + bodyNodes.get(i));
        NodeCollection childNodes = ((CompositeNode) bodyNodes.get(i)).getChildNodes();
        for (int j = 0; j <childNodes.getCount(); j++)
        {
            if (childNodes.get(j).isComposite())
            {
                System.out.println("Composite Node Name= " + childNodes.get(i));
                NodeCollection paraChildNodes = ((CompositeNode) childNodes.get(i)).getChildNodes();
                for (int k = 0; k <paraChildNodes.getCount(); k++)
                {
                    System.out.println("Inner Child Node Name= " + paraChildNodes.get(k));
                }
            }
        }
    }
}

Result:

Main Node Name = com.aspose.words.Body @181edf4
Composite Node Name = com.aspose.words.Paragraph @12d3205
Inner Child Node Name = com.aspose.words.Run @2f1921
Inner Child Node Name = com.aspose.words.Run @1adc30
Inner Child Node Name = com.aspose.words.Run @16df84b

Please correct me if i am wrong anywhere.

Hi
Thanks for your inquiry. You can check the structure of your document using DocumentExplorer (Aspose.Word demo application). Please see the attached screenshot.
As I mentioned earlier watermark is a shape inserted into the header of the document, but in your code you are iterating through nodes in the document’s body.
Also, have you tried using DocumentVisitor? I think this approach will really simplify the task.
Best regards,