Hi team,
I have one table in that there are sdt in cell based on that I want to clear whole cell and again want to apply sdt on that cell after inserting text.(I have sdt tag so based on that tag I want to find that sdt on whole document and want to traverse on cell Level)
Like I have value $ 1,000 but I just want give sdt on number only like 1,000 .
How can I insert whole text first and then insert sdt on particular text
here is some kind of example
[
{
"text": "$ 1,000",
"sdtvalue": 1000
},
{
"text": "(2,000)",
"sdtvalue": 2000
},
{
"text": "- 3,000",
"sdtvalue": 3000
},
{
"text": "4,000.00 %",
"sdtvalue": 4000
},
{
"text": "(5,000) $",
"sdtvalue": 5000
}
]
sdtValue is the value on that I want to give sdt.
@Jayshiv
To insert a Structured Document Tag (SDT) in a specific cell of a Word table, you can follow these steps:
-
Clear the Cell Content: First, you need to clear the existing content of the cell where you want to insert the SDT. You can use the Clear
method of the StructuredDocumentTag
class to achieve this.
-
Insert Text: After clearing the cell, you can insert the desired text into the cell.
-
Insert SDT: Finally, you can create a new SDT and insert it into the cell, specifically targeting the text you want to associate with the SDT.
Here is a code example demonstrating how to perform these steps in C#:
using Aspose.Words;
using Aspose.Words.Tables;
using Aspose.Words.Saving;
public void InsertSdtInTableCell(Document doc, int rowIndex, int cellIndex, string text, int sdtValue)
{
// Get the table from the document
Table table = (Table)doc.GetChild(NodeType.Table, 0, true); // Assuming the first table
// Access the specific cell
Cell cell = table.Rows[rowIndex].Cells[cellIndex];
// Clear the cell content
cell.Clear();
// Insert the text into the cell
cell.AppendChild(new Paragraph(doc) { ParagraphFormat = { StyleName = "Normal" } });
cell.FirstParagraph.AppendChild(new Run(doc, text));
// Create a new SDT
StructuredDocumentTag sdt = new StructuredDocumentTag(doc, SdtType.PlainText, MarkupLevel.Block);
sdt.Title = "SDT for value";
sdt.SetText(sdtValue.ToString());
// Insert the SDT into the cell
cell.AppendChild(sdt);
}
// Usage
Document doc = new Document("YourDocument.docx");
InsertSdtInTableCell(doc, 0, 0, "$ 1,000", 1000);
doc.Save("UpdatedDocument.docx");
Explanation:
- The
InsertSdtInTableCell
method takes a Document
, the row and cell indices, the text to insert, and the SDT value.
- It retrieves the first table in the document, clears the specified cell, and inserts the text.
- A new SDT is created and added to the cell after the text.
This approach allows you to manage the content of the cell effectively while associating the SDT with the specific text you want to track.
For more detailed information on working with SDTs, you can refer to the official documentation on Working with Content Control SDT.
Sources:
[1]: Working with Content Control SDT|Aspose.Words for .NET
@Jayshiv If you already have SDT in the table cell, you can simply update it’s content, so there is no need to remove it, insert text and then wrap text into SDT again.
If possible, could you please attach your input, output and expected output documents here for our reference? We will check them and provide you more information.
@alexey.noskov Here is my sample table doc.
I understand to replace text in sdt but there is some requirement for that I have to do this If you have any alternate way I can do that also.
Here is my scenario.
Second row $ 1,735 is there and only 1,735 has sdt $ is not cover in sdt.(as per our need we only apply sdt on numeric)
Now I got object in that I got updated value for that cell so I have to update that and update value is Like $ (2,000) so how can I replace sdt text only because its updating like $ 2,000 “(” & “)” will be missed because I have replacing sdt text so that Other value can’t be updated.
(2) As you can see in 4th row there is only text 191 now new update value is 200 % or 200$
so either my % or $ is cover in sdt or its update text how can I insert other text which I don’t want to cover in sdt.
there vise verse scenario for 5 & 6 row like value is (42,250) and - 51,996 and sdt apply on 42,250 and 51,996 bracket and - is not in sdt.
Now in my updated value this bracket or “-” is remove. If I replace text then how can I remove those things.
Currently Using sdt Tag I’m finding that sdt in doc which give me sdt node so using ancestor I’m going at cell node and removing children and then inserting text.
here is my docx file which has original data and expected output attached
Input.docx (28.0 KB)
@Jayshiv You can use code like the following to insert simple text content and content in SDT into the document:
Document doc = new Document();
Paragraph targetParagraph = doc.FirstSection.Body.FirstParagraph;
// Create a simple text run
targetParagraph.AppendChild(new Run(doc, "text before SDT "));
// Insert SDT
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
tag.RemoveAllChildren();
tag.IsShowingPlaceholderText = false;
tag.AppendChild(new Run(doc, "text inside SDT"));
targetParagraph.AppendChild(tag);
// Add some simple text after SDT.
targetParagraph.AppendChild(new Run(doc, " text after SDT"));
doc.Save(@"C:\Temp\out.docx");
Thank you so much for quick response.
Just want to confirm that I have to do in table for that I’m doing this for getting cell and clearing it.
var sdt = doc.GetChildNodes(Aspose.Words.NodeType.StructuredDocumentTag, true).Cast<StructuredDocumentTag>().ToArray();
var sdtcell = sdt.FirstOrDefault(s => s.Tag == "tagID");
var cell = sdtcell.GetAncestor(Aspose.Words.NodeType.Cell);
cell.RemoveAllChildren();
then I will create para and run and append this.
@Jayshiv Yes, in your case you can remove old cell’s content. Create a paragraph in the cell and then append runs and sdts to the created paragraph.
1 Like