We're sorry Aspose doesn't work properply without JavaScript enabled.

Check If Section Break after TOC Exists in Word Document using C# .NET

I need code to check TOC existed in word document or not,
if existed, Check is section break after toc or not.
if no section break available after toc, then i have to add section break.
please hafeez, help me to this, i am facing lot of pressure from my superiors.

could you please respond with solution…!1

@HarishGali

You can use following code example to achieve your requirement. If you still face problem, please ZIP and attach your input and expected output Word documents here for our reference. We will then provide you more information about your query.

Document doc = new Document(MyDir + @"input.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
FieldToc fieldToc = (FieldToc)doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldTOC).ToList().FirstOrDefault<Field>();

if (fieldToc != null)
{
Node node = fieldToc.End.ParentParagraph;
while (node != null)
{
node = node.NextPreOrder(doc);
if (node.GetText().Contains(ControlChar.SectionBreak))
{
break;
}
else if (node.ToString(SaveFormat.Text).Trim().Length > 0)
{
node = fieldToc.End.NextPreOrder(doc);
builder.MoveTo(node);
builder.InsertBreak(BreakType.SectionBreakNewPage);
break;
}
}
}
doc.UpdateFields();
doc.Save(MyDir + "21.11.docx");


Could you please tell me the difference of section break and page break, i am unable to read both. because in code both were showing same as “\f”,
The requirement is to add section break after toc if not existed, also delete the page break after toc and replace with section break.

Could you please tell me the difference of section break and page break, i am unable to read both. because in code both were showing same as “\f”,
The requirement is to add section break after toc if not existed, also delete the page break after toc and replace with section break.

@HarishGali Page break is a character that forces page break in Word document. Section break is end of section node. Please see Aspose.Words Document Object Model for more information.
If you need to replace page break with section break, you can use Range.Replace method and metacharacters. See the code below:

Document document = new Document(@"C:\Temp\in.docx");
document.Range.Replace("&m", "&b");
document.Save(@"C:\Temp\out.docx");


Could you please tell me the difference of section break and page break, i am unable to read both. because in code both were showing same as “\f”,
The requirement is to add section break after toc if not existed, also delete the page break after toc and replace with section break.

This Topic is created by awais.hafeez using Email to Topic tool.

is that works with paragraph.range.replace and run.range.replace??

also please tell me how to check paragraph/node/run contains ("&m")…,if work with Controlchar.sectionbreak then iam getting ppage breaks also. please help me out from this issue.

@HarishGali As I mentioned earlier Section break is not a character, but end of section. You can use DocumentExplorer demo to investigate document structure. See the screenshot attached. image.png (98.1 KB)
You can use code like the following to check whether Run contains page break character and replace it with section break:

if (run.Text.Contains(ControlChar.PageBreak))
{
Console.WriteLine("Page break found");
run.Range.Replace("&m", "&b");
}


in paragraph.range.text iam getting text like “\u0015\fLIST OF TABLES\r”.
at my knowledge \f is section break and List of tables is the paragraph text, please let me know what is this “\u0015”, and while doing paragraph.Range.Replace("&m","&b") iam getting stack empty error, could you please tell me the reason behind that stack empty error and also about this “\u0015” text. its very urgent requirement, i am waiting for ur reply asap.

@HarishGali '\f' character is used for both page break and section break. Most likely in your case it means page break. Could you please attach a sample document that will allow us to reproduce stack empty error? We will check the issue and provide you more information.