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.

Wed Mar 29, 2017 12:45 am

Hello,

I have an existing document template with numbered paragraphs.
For example:
1. Some text
2. Some text
3. Some text
3.1 Some sub text
3.2 Some sub text
3.3 Some sub text
4. Some text

I need to generate a new document in which the 3.2, 3.3 and 4 paragraphs should be removed.

I've implemented this in the following way (placed start/end tags around each numbered paragraph to be removed in the existing template document):

Code: Select all
         Regex regEx = new Regex(@"(?<=start)[.\s\S]*(?=end)");
        TextSelection[] selections = document.FindPatternInLine(regEx);
        while(selections != null){                 
            TextRange range = selections[0].GetAsOneRange();
            //Replace with new text
            range.Text = "";
            range.CharacterFormat.Hidden = true;
            selections = document.FindPatternInLine(regEx);
        }

        document.Replace("start", "", false, true);
        document.Replace("end", "", false, true);   


The result is that the paragraphs are removed but the numbering stayed intact.
1. Some text
2. Some text
3. Some text
3.1 Some sub text
3.2
3.3
4.

Can you please advice on this? Is there some other way to achieve the desired?

Thank you,
Beatrice

baisman1
 
Posts: 1
Joined: Wed Mar 29, 2017 12:34 am

Wed Mar 29, 2017 6:28 am

Dear Beatrice,

Thanks for your inquiry.
The way you used can only clear the text range in the paragraph, it cannot remove the the numbering.
Please place the start tag on the first paragraph(3.2) and end tag on the last paragraph(4), use following code to remove those paragraphs directly.
Code: Select all
            Document document = new Document(@"F:\sample.docx");
            Section sec = document.Sections[0];
            TextSelection selection1 = document.FindString("start", false, false);
            TextRange range1 = selection1.GetAsOneRange();
            int startIndex = sec.Body.ChildObjects.IndexOf(range1.OwnerParagraph);

            TextSelection selection2 = document.FindString("end", false, false);
            TextRange range2 = selection2.GetAsOneRange();
            int endIndex = sec.Body.ChildObjects.IndexOf(range2.OwnerParagraph);
            //remove the paragraphs between two tags
            for (int i = startIndex; i <= endIndex; i++)
            {             
                sec.Body.ChildObjects.RemoveAt(i);
            }
            document.SaveToFile("10164.docx", FileFormat.Docx);

Hope this helps, if you still have issue, please provide us the sample file. We will provide you corresponding code.

Sincerely,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Fri Mar 31, 2017 7:49 am

Dear Beatrice,

Did you test the code I provided ? Has the issue been solved ?

Thanks,
Betsy
E-iceblue support team
User avatar

Betsy.jiang
 
Posts: 3099
Joined: Tue Sep 06, 2016 8:30 am

Thu Jan 23, 2020 1:06 am

How can I remove the numbering in words docx.

Like this:
(a) Inserting an element in array is more expensive as compared to linked list.

To this:
Inserting an element in array is more expensive as compared to linked list.

HongYing22
 
Posts: 1
Joined: Thu Jan 16, 2020 8:46 am

Thu Jan 23, 2020 3:10 am

Hello Hong,

Thanks for your inquiry.
The following code demonstrates how to remove the paragraph list number, please try it with the latest Spire.Doc Pack(hot fix) Version:8.1.10. If there is any question, please provide your input file as well as your desired output for further investigation. You could upload them here or send them to us via email (support@e-iceblue.com).
Code: Select all
    Document doc = new Document();
    doc.LoadFromFile("test.docx");
    foreach (Section sec in doc.Sections)
    {
        foreach (DocumentObject obj in sec.Body.ChildObjects)
        {
            if (obj is Paragraph)
            {
                Paragraph p = obj as Paragraph;
                if (p.ListText != "")
                {
                    p.ListFormat.RemoveList();
                }
            }
        }
    }
    doc.SaveToFile("result.docx", FileFormat.Docx);


Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Wed Feb 05, 2020 10:06 am

Hello Hong,

Greetings from E-iceblue!
Did my code help you? Any feedback would be greatly appreciated!

Sincerely,
Rachel
E-iceblue support team
User avatar

rachel.lei
 
Posts: 1571
Joined: Tue Jul 09, 2019 2:22 am

Return to Spire.Doc