Hi team,
I am creating content contol with below code.
SDT is getting created successfully and visible in WPS but same is not visible in MS word.
Code Snippet:
public void createSDT(Document document, CustomNode customNode){
var sectionSDT = this.sdtCreationService.createNewStructureDocumentTag(document);
this.sdtCreationService.makeParaChildOfSDTSectionParsing(customNode, sectionSDT);
}
public class StructureDocumentTagCreationService {
@Autowired
private NodeUtil nodeUtil;
private static final String TAG_CREATION_ERROR_MESSAGE = "Not able to create structureDocumentTag in document due to : %s";
private static final String SDT_INSERTION_ERROR_MESSAGE = "Not able to insert SDT before para due to : %s";
public StructuredDocumentTag createNewStructureDocumentTag(Document document) {
return this.createNewStructureDocumentTag(document, SdtType.RICH_TEXT);
}
public StructuredDocumentTag createNewStructureDocumentTag(Document document, Integer sdtType) {
StructuredDocumentTag structuredDocumentTag;
try {
structuredDocumentTag = new StructuredDocumentTag(document, sdtType, MarkupLevel.BLOCK);
structuredDocumentTag.removeAllChildren();
var uuId = UUID.randomUUID().toString();
structuredDocumentTag.setTitle(uuId);
structuredDocumentTag.setTag(uuId);
structuredDocumentTag.isShowingPlaceholderText(false);
} catch (Exception ex) {
throw new CustomException(TAG_CREATION_ERROR_MESSAGE.formatted(ex.getMessage()), null);
}
return structuredDocumentTag;
}
public void makeParaChildOfStructuredDocumentTag(Paragraph para, StructuredDocumentTag structuredDocumentTag, boolean isContentControl, boolean docTaggingEnabled) {
var parentNode = para.getParentNode();
if (!isContentControl && Objects.nonNull(parentNode) && docTaggingEnabled) {
try {
parentNode.insertBefore(structuredDocumentTag, para);
structuredDocumentTag.appendChild(para);
} catch (Exception ex) {
throw new CustomException(SDT_INSERTION_ERROR_MESSAGE.formatted(ex.getMessage()), null);
}
}
}
public void makeParaChildOfSDTSectionParsing(CustomNode customNode, StructuredDocumentTag structuredDocumentTag) {
Paragraph para = customNode.getParaNode();
var parentNode = para.getParentNode();
if (Objects.nonNull(parentNode)) {
try {
parentNode.insertBefore(structuredDocumentTag, para);
structuredDocumentTag.appendChild(para);
if(!customNode.isLastNodeInSection()) { //if it is the last node, the non-para nodes need not be inserted in the section.
var nonParaNodeList = this.getNonParaNodeListToAppend(para, structuredDocumentTag);
for (Node node : nonParaNodeList) {
parentNode.insertBefore(structuredDocumentTag, node);
structuredDocumentTag.appendChild(node);
}
}
} catch (Exception ex) {
throw new CustomException(SDT_INSERTION_ERROR_MESSAGE.formatted(ex.getMessage()), null);
}
}
}
private List<Node> getNonParaNodeListToAppend(Paragraph paragraph, StructuredDocumentTag structuredDocumentTag){
Node node = Objects.isNull(paragraph.getNextSibling()) ? structuredDocumentTag.getNextSibling() : paragraph.getNextSibling();
if (Objects.isNull(node)) {
return List.of();
}
List<Node> nonParaNodeList = new ArrayList<>();
try {
while (Objects.nonNull(node)) {
if (node instanceof Paragraph && this.nodeUtil.isSkipNode((Paragraph) node, new Stack<>())) {
nonParaNodeList.add(node);
node = node.getNextSibling();
continue;
}
if (node.getNodeType() == NodeType.PARAGRAPH) {
break;
}
var parentNode = node.getParentNode();
if (Objects.nonNull(parentNode)) {
if (node.getNodeType() == NodeType.TABLE) {
nonParaNodeList.add(node);
}
}
node = node.getNextSibling();
}
} catch (Exception ex) {
throw new CustomException(SDT_INSERTION_ERROR_MESSAGE.formatted(ex.getMessage()), null);
}
return nonParaNodeList;
}
}
In the attached document:
Tagged_doc.docx (17.8 KB)
Paragraph with text “NOW, THEREFORE, for and in consideration of the agreements set forth below, the MSA Contracting Parties agree as follows:” is showing content control in WPS but not in MS word.