Exception when try to read watermark text from word Document

Hi,

Iam using the below code (shared by Aspose Developer) to read watermark text from document.

Code is :

Shape shape = (Shape)primaryHeader.LastParagraph.LastChild;

But the below exception is raised when i use the above code.

Exception is “Unable to cast object of type ‘Aspose.Words.BookmarkEnd’ to type 'Aspose.Words.Drawing.Shape”

Sending source document as an attachment this post.

Kindly resolve the issue and send me the generic solution. I mean if there is no watermark text in the document then the exception should not raise.

Regards

Naveen

Hi Naveen,

Thanks for your inquiry. The exception occurs because there is no Shape node inside Header/Footer of this document. However, we will consider providing Shape.IsWatermark property to determine whether a Shape is watermark or not so that you can read content/text of Shape. Your thread has been linked to the appropriate issue (WORDSNET-12057) and you will be notified as soon as it is resolved. Sorry for the inconvenience.

Best regards,

Hi Naveen,

Thanks for being patient. Regarding WORDSNET-12057, our product team has completed the work on your issue and has come to a conclusion that they will most likely won’t be able to implement the fix to your issue. Your issue (WORDSNET-12057) may be closed with ‘Won’t Fix’ resolution.

In MS Word there is no any special property for watermark. Watermark in MS Word is a simple shape inserted into the header. However, you can identify this shape in order to get/set 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(MyDir + @"watermark.docx");
// Get all shapes form the document.
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);
// Look for watermark shape.
foreach (Shape shape in shapes)
{
    if (shape.Name.Contains("WaterMark"))
    {
        // Here you can access watermark shape properties.
        // ..............................................
        // For example printe text of the watermark.
        Console.WriteLine(shape.TextPath.Text);
    }
}

I hope, this helps.

Best regards,

Hi Naveen,

Regarding WORDSNET-12057, our product team has completed the work on your issue and has come to a conclusion that they won’t be able to implement the fix to your issue. Your issue (WORDSNET-12057) has now been closed with ‘Won’t Fix’ resolution. Please use the code from my previous post as a workaround.

Best regards,