Dear Rachel,
I want to get the index of a particular Section, not the whole section.
Section section = para.Owner.Owner as Section;
int sectionIndex = document.Sections.IndexOf(section); 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;
} 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);
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 (paragraph.OwnerTextBody.Owner.DocumentObjectType.Equals(DocumentObjectType.Section))
{
Section section = paragraph.OwnerTextBody.Owner as Section;
}