Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Mar 21, 2023 2:07 pm

I use spire.doc to import word files.

Sometimes the documents contain "content controls" which keeps my code from "seeing" the info in the document.
This happens for instance when a text is entered in ukrainian or chinese. ( a test file is attached )

My workaround is to:
- open the file in word
- select all text
- right mouse button - and press "remove content control"
- save the document

after this action, the text is "normal" again and my program can read the text in the file.

Can the "remove content control" also be done through the spire.doc api?

onetimelogininfo
 
Posts: 3
Joined: Tue Mar 21, 2023 2:01 pm

Wed Mar 22, 2023 5:43 am

Hello,

Thanks for your inquiry.
Yes, our Spire.Doc supports deleting "content controls". Please refer to the example code below.
In addition, the attachment you provided is an empty file. If the sample code cannot meet your requirements, please provide your input Word file and desired result file for our further investigation. You could attach it here or send it to us via email (support@e-iceblue.com). Thanks in advance.
Code: Select all
Document doc = new Document();
doc.LoadFromFile(@"input.docx");
//Loop through sections
for (int s = 0; s < doc.Sections.Count; s++)
{
    Section section = doc.Sections[s];
    for (int i = 0; i < section.Body.ChildObjects.Count; i++)
    {
        //Loop through contents in paragraph
        if (section.Body.ChildObjects[i] is Paragraph)
        {
            Paragraph para = section.Body.ChildObjects[i] as Paragraph;
            for (int j = 0; j < para.ChildObjects.Count; j++)
            {
                //Find the StructureDocumentTagInline
                if (para.ChildObjects[j] is StructureDocumentTagInline)
                {
                    StructureDocumentTagInline sdt = para.ChildObjects[j] as StructureDocumentTagInline;
                    //Remove the content control from paragraph
                    para.ChildObjects.Remove(sdt);
                    j--;
                }
            }
        }
        if (section.Body.ChildObjects[i] is StructureDocumentTag)
        {
            StructureDocumentTag sdt = section.Body.ChildObjects[i] as StructureDocumentTag;
            section.Body.ChildObjects.Remove(sdt);
            i--;
        }
    }
}

//Save the Word document
string output = "RemoveContentControls_out.docx";
doc.SaveToFile(output, FileFormat.Docx2013);

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Sat Apr 01, 2023 12:47 pm

Hi, thank you for your response.
Your code seems to REMOVE the content controls (and the text). This is not the desired effect.

The desired effect is to replace the text in the content controls with the "plain text" equivalent as happens when you manually use the right mouse button action "remove content control" option in word.

Sorry for the wrong attachment, a new one is attached now.

onetimelogininfo
 
Posts: 3
Joined: Tue Mar 21, 2023 2:01 pm

Sun Apr 02, 2023 8:38 am

Thank you for pointing me in the right direction.
The following code seems to work.

Code: Select all
Spire.Doc.Document d = new Spire.Doc.Document(@"c:\temp\test.docx");


            //Spire.Doc.Document doc = new Spire.Doc.Document();
            //doc.LoadFromFile(@"input.docx");
            //Loop through sections
            for (int s = 0; s < d.Sections.Count; s++)
            {
                Section section = d.Sections[s];
                for (int i = 0; i < section.Body.ChildObjects.Count; i++)
                {
                    //Loop through contents in paragraph
                    if (section.Body.ChildObjects[i] is Paragraph)
                    {
                        Paragraph para = section.Body.ChildObjects[i] as Paragraph;
                        for (int j = 0; j < para.ChildObjects.Count; j++)
                        {
                            //Find the StructureDocumentTagInline
                            if (para.ChildObjects[j] is StructureDocumentTagInline)
                            {
                                StructureDocumentTagInline sdt = para.ChildObjects[j] as StructureDocumentTagInline;
                                //Remove the content control from paragraph

                                para.ChildObjects.Remove(sdt);
                                para.AppendText(sdt.SDTContent.Text);
                                j--;
                            }
                        }
                    }
                    if (section.Body.ChildObjects[i] is StructureDocumentTag)
                    {
                        StructureDocumentTag sdt = section.Body.ChildObjects[i] as StructureDocumentTag;
                        section.Body.ChildObjects.Remove(sdt);
                        i--;
                    }
                }
            }

            //Save the Word document
            string output = @"c:\temp\test_output.docx";
            d.SaveToFile(output, FileFormat.Docx2013);

onetimelogininfo
 
Posts: 3
Joined: Tue Mar 21, 2023 2:01 pm

Mon Apr 03, 2023 6:32 am

Hello,

Glad to hear that.
If you encounter other issues related to our products in the future, please feel free to contact us.
Wish you all the best!

Sincerely,
Annika
E-iceblue support team
User avatar

Annika.Zhou
 
Posts: 1648
Joined: Wed Apr 07, 2021 2:50 am

Return to Spire.Doc