Programmatically find column breaks and section breaks

Hello, I have got one of my tasks in hand to identify all the column breaks and section breaks in some MS Word Documents programmatically in C#. Can you please let me know how can I count the number of column breaks and section breaks?

@varun.arora,

You can calculate total number of Sections and (Text) Columns in Word document by using the following C# Code of Aspose.Words for .NET:

Document doc = new Document("C:\\temp\\sec col breaks.docx");

Console.WriteLine("Total Sections in Word document = " + doc.Sections.Count);

int column_Count = 0;
foreach (Section sec in doc.Sections)
    column_Count += sec.PageSetup.TextColumns.Count;

Console.WriteLine("Total Columns in Word document = " + column_Count);

Thank you. Its working very nicely.

Can you please correct my below assumptions?:

  1. Section break is number of sections that means every section has a section break.
  2. If any section has more than 2 TextColumns that means it has a column break.

Thanks in Advance,
Varun

@varun.arora,

Yes, your understanding is correct i.e. every Section has a Section break. However, in addition to TextColumns.Count you can use the following code to detect explicit column breaks:

Document doc = new Document("C:\\temp\\column breaks.docx");

int count_column_breaks = 0;
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
    // Check all Runs in the Paragraph for Column Breaks.
    foreach (Run run in para.Runs)
        if (run.Text.Contains(ControlChar.ColumnBreak))
            count_column_breaks++;
}

Console.WriteLine("Explicit Columns Breaks in Word document = " + count_column_breaks);

Thank you!!! That works and more precise to my current situation.