HI Gembox Team,
We have to develop an enhancement for word’s content control.
Requirement is below,
Extract the content control data from word, Process business logic on it and replace the content control with processed data by maintaining same formatting which content control had.
Refer attached content control word document.
Refer above screenshot in which we have primary content control marked in blue box, Primary content control has nested content control marked in red box.
So we want to-
**Extract content control data of primary and nested from the Word document.
**Apply business logic to the extracted content.
**Replace the original content control with the processed result.
**Retain the original formatting (font, size, color, bold, etc.) from the content control in the new text.
Could you please help with sample console project of above functionality.
Nested Contant Control 1.docx (39.3 KB)
Thank you.
Somnath.
@Somnath.kamthe
To achieve the functionality of extracting content control data from a Word document, processing it, and then replacing the original content control with the updated data while retaining the original formatting, you can follow the steps outlined below.
Steps to Implement the Functionality
-
Extract Content Control Data: You will need to identify and extract the data from both the primary and nested content controls in the document.
-
Process the Extracted Data: Apply your business logic to the extracted data as required.
-
Replace the Original Content Control: Insert the processed data back into the document, ensuring that the formatting (font, size, color, bold, etc.) of the original content control is preserved.
Sample Code
Here is a sample Java code snippet that demonstrates how to extract content between nodes, which can be adapted to work with content controls:
import com.aspose.words.*;
public class ContentControlExample {
public static void main(String[] args) throws Exception {
// Load the document
Document doc = new Document("path/to/your/document.docx");
// Assuming you have a way to identify your content controls
// For example, using bookmarks or specific node types
Node startNode = doc.getRange().getBookmarks().get("StartBookmark").getBookmarkStart();
Node endNode = doc.getRange().getBookmarks().get("EndBookmark").getBookmarkEnd();
// Extract content between the specified nodes
ArrayList<Node> extractedNodes = ExtractContentHelper.extractContent(startNode, endNode, true);
// Process the extracted data (your business logic here)
String processedData = processData(extractedNodes);
// Replace the original content control with the processed data
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveTo(startNode);
builder.write(processedData);
// Save the updated document
doc.save("path/to/your/updated_document.docx");
}
private static String processData(ArrayList<Node> extractedNodes) {
// Implement your business logic here
// For demonstration, we will just concatenate the text
StringBuilder sb = new StringBuilder();
for (Node node : extractedNodes) {
sb.append(node.toString(SaveFormat.TEXT));
}
return sb.toString();
}
}
Explanation of the Code
- Document Loading: The document is loaded using the
Document
class.
- Node Identification: You can identify the start and end nodes of the content control using bookmarks or other methods.
- Content Extraction: The
ExtractContentHelper.extractContent
method is used to extract the content between the specified nodes.
- Data Processing: The extracted data is processed according to your business logic.
- Content Replacement: The
DocumentBuilder
is used to insert the processed data back into the document at the location of the original content control.
- Document Saving: Finally, the updated document is saved.
This approach allows you to maintain the formatting of the original content control while replacing it with new data. You can further customize the processData
method to implement your specific business logic.
Additional Resources
For more detailed examples and methods related to content extraction and manipulation in Aspose.Words, you can refer to the official documentation and code examples available on the Aspose GitHub repository.
If you have any further questions or need additional assistance, feel free to ask!
Thank you for quick response.
It looks provided code is in Java, Could you please share C# code.
We also try to convert it into C# but unable to find implementation for ExtractContentHelper.extractContent menthod. Refer below line.
ArrayList extractedNodes = ExtractContentHelper.extractContent(startNode, endNode, true);
please share c# code.
Thank you.
Somnath.
@Somnath.kamthe You can use code like the following to update content of SDTs in your document:
Document doc = new Document(@"C:\Temp\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
// Get all content controls in the document.
NodeCollection sdts = doc.GetChildNodes(NodeType.StructuredDocumentTag, true);
// Loop through content controls and update their values.
// To keep original formatting and for convenience used DocumentBuilder.
foreach(StructuredDocumentTag tag in sdts)
{
// Get old child nodes of SDT.
List<Node> oldContent = tag.GetChildNodes(NodeType.Any, true).ToList();
// Move document builder into SDT.
builder.MoveToStructuredDocumentTag(tag, 0);
// insert some content.
builder.Write("This is updated content.");
// Reset old content of SDT if it does not contain nested SDTs.
if (!oldContent.Any(n => n.NodeType == NodeType.StructuredDocumentTag))
oldContent.Where(n => n.NodeType == NodeType.Run).Cast<Run>().ToList()
.ForEach(r => r.Text = "");
}
doc.Save(@"C:\Temp\out.docx");