Help setting a structured document tag based on Range.text property

Hi,
A tricky problem to explain.

I am hoping to create structured document tags in a document. These tags could either be child of a paragraph or another structured document tag.

The tricky thing is determining the position of the tag.

Ideally i would like to examine the text of a range… and then use text markers to designate the beginning and end of the SDT.

For example say i have an arbitrary node:
and if i inspect the original text range it returns: “This is the original text.”
Now just say i want to enclose the word “original” in a new Structured Document tag - is there any way to do that?

I hope i’m making sense.
I am using aspose words for python

@graeme.pillemer In general it is possible. You should get the nodes that should be wrapped into a content control. Please see Aspose.Words documentation to learn more about Aspose.Words DOM. For example see the following code:

lic = aw.License()
lic.set_license("Aspose.Words.Python.NET.lic")

doc = aw.Document()
builder = aw.DocumentBuilder(doc)

# Build a test document.
builder.write("This is the first run")
builder.font.bold = True
builder.write("this is the second run")
builder.font.bold = False
builder.write("this is the third run")

# The test document contains one paragraph that contsin 3 runs.
# let's wrap the second run into a content control.
tag = aw.markup.StructuredDocumentTag(doc, aw.markup.SdtType.RICH_TEXT, aw.markup.MarkupLevel.INLINE);
tag.remove_all_children()
nodeToWrap = doc.first_section.body.first_paragraph.runs[1]
nodeToWrap.parent_node.insert_after(tag, nodeToWrap)
tag.append_child(nodeToWrap)

doc.save("C:\\Temp\\out.docx")

Before inserting SDT document structure looks like this:


After inserting SDT the second Run is wrapped into a structured document tag:

out.docx (10.7 KB)

Thanks for the quick response.
However this isn’t exactly the problem I’m trying to solve (hence why its a little tricky to explain).
I am aware that I can wrap a Run in a structured document tag.

What i’m wanting to know is if i could add a Structured Document Tag just working with a Range - without keeping track of the Runs.

In other words - imagine you presented a user with the doc.Range.Text from the example above.

“This is the first run this is the second run this is the third run”.

And they requested that a SDT was add only around the phrase “second run this is the third”.

Working purely with runs i’d have to:

  1. Locate across which runs the text is.
  2. Split the text into a new run
  3. Wrap that run in an SDT.

I’d ideally like to be able to use markers to set a start and end postion for the tag based on the text, and use that to create my SDT (similar to the way a user in Word can create an SDT based on cursor position)

But i’m not sure if such a thing is possible

Thanks again for your help

@graeme.pillemer Thank you for additional information. Unfortunately, there is no an easy way to achieve what you need. The only way is the one you have described:

  1. Locate across which runs the text is.
  2. Split the text into a new run
  3. Wrap that runs in an SDT.

Thanks @alexey.noskov
One thought i had was to set this property based on position in an existing range StructuredDocumentTagRangeStart class | Aspose.Words for Python

But i’m not sure if that is possible
Thanks again for your time

Graeme

@graeme.pillemer Unfortunately, there is no way to achieve this using ranges. StructuredDocumentTagRangeStart and StructuredDocumentTagRangeEnd nodes are used to represent SDTs that spans several section in Aspose.Words DOM. The only way to achieve what you need using DOM.