Free Support Forum - aspose.com

How to assign StructuredDocumentTag text Property to different values according to its tag using .NET

How to assign StructuredDocumentTag text Property to different values according to its tag。

@jianmin459

You need to add Paragraph and Run nodes to content control (StructuredDocumentTag) to add text to it. Please refer to the following article about working with content control.

If you face any issue, please ZIP and attach your input and expected output Word documents. We will then provide you code example according to your requirement.

My word template like this :
image.png (23.0 KB)

Then,I want update the text of the content control to user information,like this:
image.png (21.5 KB)

When I add Paragraph and Run nodes to content control (StructuredDocumentTag) to add text to it,the original font and style are not reserved,which has troubled me for a few days。

@tahir.manzoor
The detail has been uploaded, can you see how to deal with it?

Thanks for your help,

My word template is the Email attachment named “Test05.docx” ,like this :
Catch.jpg

Then,I want update the text of the content control to user information,the result file is the Email attachment named “Test05.docx” ,like this:

CatchB7BD.jpg

When I add Paragraph and Run nodes to content control (StructuredDocumentTag) to add text to it,the original font and style are not reserved,which has troubled me for a few days。

Test05 - 1.docx (28.5 KB)

Test05.docx (28.5 KB)

@jianmin459

Unfortunately, the screenshots are not available in your post. Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

We will investigate the issue and provide you more information on your issue.

PS: To attach these resources, please zip and upload them.

Sorry,
My last email attachments is too large to send, I make it smaller, and send it again.
Thanks for your support.

Catch.jpg (107 KB)

CatchB7BD.jpg (96.5 KB)

(Attachment Aspose.PrintDemo.Email.rar is missing)

Hi,
In order to solve the problem that the attachment is too large and failed to send, I remove “Aspose.Words.dll” from source code. If you want run it ,please add it first.
Thanks for your support.

Catch.jpg (107 KB)

CatchB7BD.jpg (96.5 KB)

(Attachment Aspose.PrintDemo.Email.rar is missing)

@jianmin459

Thanks for sharing the screenshots. We have not found the font issues in shared images. Could you please share some more detail about your query along with screenshots of problematic sections of output document.

We have not found any issue with these input and output document.

We have not found the source code in your post. Please remove all DLLs from your application, ZIP and attach it here for testing.

I use the template “Test05.docx” to update content control (StructuredDocumentTag) text, the purpose is to achieve the effect of document “Test05 - 1.docx”. But, the issue is original font and style are not reserved,and the effect of running the code is like that:
image.png (84.6 KB)

The update text function:
image.png (69.0 KB)

@jianmin459

Following code example shows how to update the text of content control. You can use the same approach to update the content control according to your desired text.

Document doc = new Document(MyDir + "input sdt.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    UpdateContentControl(sdt, "New Text");
}
doc.Save(MyDir + "21.6.docx");
private static void UpdateContentControl(StructuredDocumentTag sdt, String txtVal)
{
    if (sdt.Level == MarkupLevel.Inline)
    {
        Run run = (Run)sdt.GetChild(NodeType.Run, 0, true).Clone(true);
        run.Text = txtVal; 
        sdt.RemoveAllChildren();
        sdt.ChildNodes.Add(run);
                
    }
    else if (sdt.Level == MarkupLevel.Block)
    {
        Paragraph paragraph = (Paragraph)sdt.GetChild(NodeType.Paragraph, 0, true).Clone(true);
        if (paragraph.Runs.Count > 0)
        {
            Run run = (Run)paragraph.GetChild(NodeType.Run, 0, true).Clone(true);
            run.Text = txtVal;
            paragraph.RemoveAllChildren();
            paragraph.Runs.Add(run);
        }
                
        sdt.RemoveAllChildren();
        sdt.ChildNodes.Add(paragraph);
    }
}