Is it possible to split a document into sections before the start of the title?

Hello,

I need to split the text into sections, cannot do it by using any of special chars. Is it possible to section the text before the start of the title? I need a nice formatting.
Thanks

Hi Marina,

Unfortunately, I’m not sure what you want to achieve.

Nevertheless, here is how you can find the titles:

var document = DocumentModel.Load("input.docx");

Paragraph previousTitle = null;
foreach (var currentTitle in document.GetChildElements(true, ElementType.Paragraph)
    .Cast<Paragraph>()
    .Where(p => p.ParagraphFormat.Style != null && p.ParagraphFormat.Style.Name.StartsWith("heading", StringComparison.OrdinalIgnoreCase)))
{
    if (previousTitle != null)
    {
        ContentRange range = new ContentRange(previousTitle.Content.End, currentTitle.Content.Start);

        Console.WriteLine("Title: {0}", previousTitle.Content.ToString().TrimEnd());
        Console.WriteLine("Body:");
        Console.WriteLine(range.ToString().Trim());
        Console.WriteLine(new string('-', 50));
    }

    previousTitle = currentTitle;
}

if (previousTitle != null)
{
    ContentRange range = new ContentRange(previousTitle.Content.End, document.Content.End);

    Console.WriteLine("Title: {0}", previousTitle.Content.ToString().TrimEnd());
    Console.WriteLine("Body:");
    Console.WriteLine(range.ToString().Trim());
    Console.WriteLine(new string('-', 50));
}

I presume you want to do something with those range objects, perhaps move them to new Section elements?

Anyway, can you please send us your input DOCX file and the desired output DOCX file?
You can create the output manually using Microsoft Word.

Regards,
Mario

Hello,

Thank you for getting back to me. Unfortunately, I do not have an input.doc file. I was hoping that I can actually create a doc file with gembox, e.g. write long string with special chars in it ,read by a controller, into a file. Is it possible?

Hi Marina,

Yes, that is possible to do.

Please check the following examples:

I hope this helps.

Regards,
Mario