Issues in handling content controls using Aspose.Words

Hi

I am working on a document with nested content controls/ StructedDocumentTags. There is one outer control and within that outer control there are several controls which i want to add/update/hide/lock depending upon my scenarios. Moreover, there are some tables too within that outer control which i am using to keep some of content control in a specific place.

Now I am facing below issues in Aspose Java API (ver 20.12) whereas I was able to do all of them using VBA.

1- Adding a content control is easy but I cannot add a content control within another content control, it is giving me error i.e. Cannot add node at this place. I want to add a content control either in bottom of outer control or in start of outer control.

2- Hiding a content control doesn’t work either it is standalone or it is in nested structure without updating its text or simply I want a solution to hide a content control in nested structure.

3- Update font color/size/underline etc properties of a content control without updating its text.

4- Get selected text either it is in one content control or multiple content controls and perform highlighting on it.

Urgent help would be appreciated.

Regards

@muniryousafzai

  1. Structured document tags can have different markup level.
    https://reference.aspose.com/words/java/com.aspose.words/markuplevel/
    So you cannot insert inline SDT inside inline SDT for example. Also, please see the following article to learn more about working with SDTs:
    https://docs.aspose.com/words/java/working-with-content-control-sdt/

  2. Could you please clarify the requirement. As I can see in MS Word there is no option to hide SDTs.


    If possible please attach a simple required output documents here for our reference.

  3. in case of plain text SDT you can change SDT content fonts by setting StructuredDocumentTag.ContentsFont properties. In case of other controls, you can change formatting of each child Run in SDT.

  4. The same answer as 3. You can set highlight color using code like the following:

Document doc = new Document("C:\\Temp\\in.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
for(Run r : (Iterable<Run>) sdt.getChildNodes(NodeType.RUN, true))
    r.getFont().setHighlightColor(Color.YELLOW);
doc.save("C:\\Temp\\out.docx");

Thanks Alexey for quick response.

1- Ok, It means I cannot use nested structure in my document, can you please suggest what I can use for content protection because I was handling content protection, alignment and styling through nested structure.

2- You cannot hide the content control from here, all you do is that you hide it through font properties, screenshot is attached for reference. By hiding it doesn’t take any space in the document.

I tried to hide it through run.getFont().setHidden(true); but again in nested control it didn’t happen.

3- Yeah I can edit it through run but i used to update the text too, I will try it with this way now.

4- I want to select it from multiple content controls, shortly I want to get the selected text through mouse either it is from one content control or multiple content controls or half text of one content control and half from other content control.

thanks

@muniryousafzai

  1. Of course, you can create nested SDTs using Aspose.Words. For example see the following code:
Document doc = new Document();

StructuredDocumentTag block = new StructuredDocumentTag(doc, SdtType.GROUP, MarkupLevel.BLOCK);
StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
StructuredDocumentTag sdt2 = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);

block.appendChild(sdt1);
block.appendChild(sdt2);

doc.getFirstSection().getBody().appendChild(block);

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

But you should take MarkupLevel into account. This code produces a document with the following structure:

  1. You should set Hidden flag to all runs inside SDT to hide it.

  2. MS Word provides a Range API to work with content. Aspose.Words provides nodes oriented API. Please see Aspose.Words documentation to learn more:
    https://docs.aspose.com/words/java/logical-levels-of-nodes-in-a-document/
    Also, DocumentExplorer demo might be useful to investigate document structure.

  3. Unfortunately, there is no such functionality to select text in Aspose.Words. You can edit nodes using Aspose.Words according to Aspose.Words Document Object Model. All text in MS Word documents are represented by Run nodes. If you need to apply formatting to a part of the Run you should split it into parts and pply the formatting to the created Run nodes.

I have a StructuredDocumentTag in my word Document, which has 3 lines content. Now I want to assign 1 line content to that SDT, but I am unable to do it.

there are 3 runs which cannot be removed and if I assign empty string to it then it shows me empty lines. Can you suggest me possible solution for it please.

@muniryousafzai Could you please attach your sample input document and expected output? We will check the issue and provide you more information.

Row 1 o f table.docx (17.0 KB)

I am using 20.12 version of aspose. I have attached the sample document, which I want to update again and again and the “Sdt4” tag may contain single line or multi-line content.

Now what I am doing is clear all runs of “Sdt4” by assigning them “” so it leaves empty spaces for each run. Secondly I tried to remove runs but I am unable to remove them. The structure of the document should remain intact(ref attached) i.e. can’t remove StructuredDocumentTags / tables from it.

@muniryousafzai Empty spaces you are seeing in the output are empty paragraphs:


If you cannot remove any nodes from the document, the only possible solution is to make the empty paragraph hidden. You can achieve this by setting ParagraphBreakFont.Hidden property. In this case the empty paragraph will ne be shown if “Show paragraph marks and other hidden formatting symbols” option is disabled in MS Word.

It worked, thanks

1 Like