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 Oct 07, 2020 6:56 am

Dear Rachel,

I want to get the index of a particular Section, not the whole section.

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Wed Oct 07, 2020 7:02 am

Hello,

Thank you for your further clarification.
Below is the code for your reference. If there are any other questions, please feel free to contact us.
Code: Select all
                Section section = para.Owner.Owner as Section;
                int sectionIndex = document.Sections.IndexOf(section);


Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Oct 07, 2020 7:06 am

Thank you very much Rachel!

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Wed Oct 07, 2020 7:12 am

Hello,

You are welcome.
If you encounter any issues related to our product in the future, just feel free to contact us.
Wish you all the best!

Sincerely,
Rachel
E-iceblue support team
User avatar

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

Wed Apr 28, 2021 1:30 am

Hello Team,

Please help me why the code below cannot copy the image from source document to new document?

Code: Select all
       private static Document ExtractParagraphsToNewdocument(Document sourceDocument, Section startSection, Section endSection, int startIndex, int endIndex, int skipIndex, bool removeEmptyLine)
        {
            Document document = new Document();
            document.AddSection();
            int index_startSection = sourceDocument.Sections.IndexOf(startSection);
            int index_endSection = sourceDocument.Sections.IndexOf(endSection);
            if (index_startSection == index_endSection)
            {
                if (startIndex < endIndex)
                {
                    for (int i = startIndex + skipIndex; i < endIndex; i++)
                    {
                        if (removeEmptyLine)
                        {
                            if (startSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                if (!String.IsNullOrEmpty((startSection.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                {
                                    document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                                }
                            }
                            else
                            {
                                //copy the content
                                document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                            }
                        }
                        else
                        {
                            //copy the content
                            document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                        }
                    }
                }
                else
                {
                    if (startIndex == endIndex)
                    {
                        if (removeEmptyLine)
                        {
                            if (startSection.Body.ChildObjects[startIndex].DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                if (!String.IsNullOrEmpty((startSection.Body.ChildObjects[startIndex] as Paragraph).Text.Trim()))
                                {
                                    document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[startIndex].Clone());
                                }
                            }
                            else
                            {
                                document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[startIndex].Clone());
                            }
                        }
                        else
                        {
                            document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[startIndex].Clone());
                        }
                    }
                }
            }
            else
            {
                if (index_endSection == index_startSection + 1)
                {
                    for (int i = startIndex + skipIndex; i < startSection.Count; i++)
                    {
                        if (removeEmptyLine)
                        {
                            if (startSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                            {
                                if (!String.IsNullOrEmpty((startSection.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                {
                                    document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                                }
                            }
                            else
                            {
                                document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                            }
                        }
                        else
                        {
                            document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                        }
                    }
                    if (endIndex > 0)
                    {
                        for (int i = 0; i < endIndex; i++)
                        {
                            if (removeEmptyLine)
                            {
                                if (endSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                                {
                                    if (!String.IsNullOrEmpty((endSection.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                    {
                                        document.Sections[0].Body.ChildObjects.Add(endSection.Body.ChildObjects[i].Clone());
                                    }
                                }
                                else
                                {
                                    document.Sections[0].Body.ChildObjects.Add(endSection.Body.ChildObjects[i].Clone());
                                }
                            }
                            else
                            {
                                document.Sections[0].Body.ChildObjects.Add(endSection.Body.ChildObjects[i].Clone());
                            }
                        }
                    }
                }
                else
                {
                    if (index_endSection < index_startSection)
                    {
                        //get the last paragraph index
                        Paragraph lastPara = sourceDocument.LastParagraph;
                        int lastIndex = lastPara.Owner.ChildObjects.IndexOf(lastPara);
                        for (int i = startIndex + skipIndex; i <= lastIndex; i++)
                        {
                            if (removeEmptyLine)
                            {
                                if (startSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                                {
                                    if (!String.IsNullOrEmpty((startSection.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                    {
                                        document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                                    }
                                }
                                else
                                {
                                    //copy the content
                                    document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                                }
                            }
                            else
                            {
                                //copy the content
                                document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                            }
                        }
                    }
                    else
                    {
                        List<Section> lstSection = new List<Section>();
                        for (int i = index_startSection + 1; i < index_endSection; i++)
                        {
                            lstSection.Add(sourceDocument.Sections[i].Clone());
                        }
                        for (int i = startIndex + skipIndex; i <= startSection.Count; i++)
                        {
                            if (removeEmptyLine)
                            {
                                if (startSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                                {
                                    if (!String.IsNullOrEmpty((startSection.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                    {
                                        document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                                    }
                                }
                                else
                                {
                                    document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                                }
                            }
                            else
                            {
                                document.Sections[0].Body.ChildObjects.Add(startSection.Body.ChildObjects[i].Clone());
                            }
                        }
                        foreach (Section section in lstSection)
                        {
                            if (removeEmptyLine)
                            {
                                for (int i = 0; i < section.Body.ChildObjects.Count; i++)
                                {
                                    if (section.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                                    {
                                        if (String.IsNullOrEmpty((section.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                        {
                                            section.Body.ChildObjects.Remove(section.Body.ChildObjects[i]);
                                            i--;
                                        }
                                    }
                                }
                            }
                            document.Sections[0].Body.ChildObjects.Add(section.Clone());
                        }
                        for (int i = 0; i < endIndex; i++)
                        {
                            if (removeEmptyLine)
                            {
                                if (endSection.Body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
                                {
                                    if (!String.IsNullOrEmpty((endSection.Body.ChildObjects[i] as Paragraph).Text.Trim()))
                                    {
                                        document.Sections[0].Body.ChildObjects.Add(endSection.Body.ChildObjects[i].Clone());
                                    }
                                }
                                else
                                {
                                    document.Sections[0].Body.ChildObjects.Add(endSection.Body.ChildObjects[i].Clone());
                                }
                            }
                            else
                            {
                                document.Sections[0].Body.ChildObjects.Add(endSection.Body.ChildObjects[i].Clone());
                            }
                        }
                    }                   
                }

            }

            return document;
        }

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Wed Apr 28, 2021 2:14 am

Hello,

Thanks for your inquiry!

I simulated a Word document and tested the code you provided with the latest Spire.Doc V9.4.12, but did not reproduce your issue. Here I also attached my test file and I were using the following code to use your function.
Code: Select all
            Document source = new Document();
            source.LoadFromFile("E:\\testdoc\\test.docx");

            Document newdoc = ExtractParagraphsToNewdocument(source, source.Sections[0], source.Sections[0], 0, 6, 0, true);
            newdoc.SaveToFile("useFunction.docx", FileFormat.Docx);

test.zip

If you were not using the latest version, I recommend that you have a try. If the issue still exists, to help us investigate your issue, please provide us with your input file, target framework (E.g. .Net Framework 4.6.1), system information (E.g. Win7, 64 bit) and region setting (E.g. China, Chinese) for reference. Thanks in advance.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Wed Apr 28, 2021 7:44 am

Hello Marcia,

Please check my function with attached file.

Thank you!

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Wed Apr 28, 2021 10:04 am

Hello,

Thanks for sharing more information!

After further investigating your code, I found that you copied the paragraph based on the Paragraph.Text.Trim(). Kindly note that the picture in the Word file does not exist in the Section.Body.ChildObjects as a separate childObject, but as a ChildObjects of the Paragrph, and if there is only one picture in the Paragrph, the paragraph.Text.Trim() is empty, but the paragraph contains content in fact.

I recommend that you can judge whether the paragraph is an empty line by both the number of ChildObjects of the paragraph and paragraph.Text.Trim(). Here I attached a simple code for your reference.
Code: Select all
        private static Document ExtractParagraphsToNewdocument(Document sourceDocument, Section startSection, Section endSection, int startIndex, int endIndex, int skipIndex, bool removeEmptyLine)
        {
            Document document = new Document();
            int index_startSection = sourceDocument.Sections.IndexOf(startSection);
            int index_endSection = sourceDocument.Sections.IndexOf(endSection);
            Section newSection = document.AddSection();

            if (index_startSection <= index_endSection)
            {
                for(int i = 0; i<= index_endSection-index_startSection; i++)
                {
                    Section sourceSection = sourceDocument.Sections[index_startSection + i];
                    for (int j = startIndex + skipIndex; j < endIndex + 1; j++)
                    {
                        if (removeEmptyLine)
                        {
                            if (sourceSection.Body.ChildObjects[j].DocumentObjectType.Equals(DocumentObjectType.Paragraph))
                            {
                                Paragraph para = sourceSection.Body.ChildObjects[j] as Paragraph;
                                //no childobject means that it just a line, and no text
                                if (para.ChildObjects.Count.Equals(0))
                                {
                                    continue;
                                }
                                else
                                {
                                    bool IsAllTextRange = true;
                                    bool IsEmptyContent = false;
                                    foreach (DocumentObject ParaObject in para.ChildObjects)
                                    {
                                        if (!ParaObject.DocumentObjectType.Equals(DocumentObjectType.TextRange))
                                        {
                                            IsAllTextRange = false;
                                        }
                                        if (IsAllTextRange)
                                        {
                                            if (string.IsNullOrEmpty(para.Text.Trim()))
                                            {
                                                IsEmptyContent = true;
                                            }
                                        }
                                    }
                                    if (IsEmptyContent)
                                    {
                                            continue;
                                    }
                                }
                            }
                        }
                        newSection.Body.ChildObjects.Add(sourceSection.Body.ChildObjects[j].Clone());
                    }
                }
            }
           
            return document;
        }



If you encounter any issues during test, just feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Thu Apr 29, 2021 1:47 am

Dear Marcia,

Your code will get error if startIndex and endIndex belong to two different sections.

Ex: startIndex in Section 1 and endIndex in Section 2 / Section 4.

And please help me explain the difference between

1. Section section = _paragraph.Owner.Owner as Section;

And

2. Section section = _paragraph.OwnerTextBody.Owner as Section;

I want to get Section of Paragraph, should I use 1. or 2.?

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Thu Apr 29, 2021 9:44 am

Hello,

Thanks for your reply!

I made some adjustments with the code, please refer to the following code to handle the situation of startIndex and endIndex belong to two different sections.
code.zip


Besides, I recommend that you better use the following code to get the owner section of the paragraph.
Code: Select all
            if (paragraph.OwnerTextBody.Owner.DocumentObjectType.Equals(DocumentObjectType.Section))
            {
                Section section = paragraph.OwnerTextBody.Owner as Section;
            }


If there are still any issues, just feel free to contact us.

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Tue May 04, 2021 1:48 am

Thank you very much!

supermun
 
Posts: 24
Joined: Fri May 15, 2020 6:51 am

Thu May 06, 2021 2:02 am

Hello,

Thanks for your reply!

You are welcome!

If you encounter any issues related to our product in the future, just feel free to contact us.

Have a nice day!

Sincerely,
Marcia
E-iceblue support team
User avatar

Marcia.Zhou
 
Posts: 858
Joined: Wed Nov 04, 2020 2:29 am

Return to Spire.Doc