Error creating a new ContentRange

Hello,
I have a ContentRange variable: a, this works: a.ToString(), then I have a ContentRange variable b, this also works: b.ToString()(this is for example: ‘\r\n’). I need to know why this fails: new ContentRange(a.Start, b.End).ToString() with the following error:

The end of current content range couldn’t be found!

Hi Mirel,

Please create a small Visual Studio project that reproduces your issue so I can investigate it.
I’ll need to check the content of those ContentRange objects.

Regards,
Mario

Please see this example and use this file: Word file

using System;
using GemBox.Document;

class Program
{
    static void Main()
    { 
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        ComponentInfo.FreeLimitReached += (sender, e) => e.FreeLimitReachedAction = FreeLimitReachedAction.ContinueAsTrial;

        DocumentModel document = DocumentModel.Load("Word2 copy.docx");

        Console.WriteLine(document.Content.ToString());

        var paragraphs = document.GetChildElements(true, ElementType.Paragraph).ToList();
        var firstContent = paragraphs[0].Content;
        var secondContent = paragraphs[1].Content;

        var currentRange = new ContentRange(firstContent.Start, secondContent.End);
        Console.WriteLine(currentRange.ToString());  
    }
}

Hi Mirel,

The problem occurs because the document has only one Paragraph in the body and one Paragraph in the header.
So, I’m afraid this cannot be represented with such a ContentRange object.

Can you tell me more about what you’re trying to accomplish here?

Regards,
Mario

Hi Mario,

I sent you only a part of the real Word file. I need to capture only body paragraphs, but in this example the first paragraph is the one in the body; why is the second one in a header?. How can I look for the body paragraphs?

The document.GetChildElements(true, ElementType.Paragraph) will return all the paragraphs in the document, including the ones in the header and footer.
To exclude those, you could use this:

var paragraphs = document.Sections
    .SelectMany(section => section.Blocks)
    .SelectMany(block => block is Paragraph p 
        ? new[] { p } 
        : block.GetChildElements(true, ElementType.Paragraph).Cast<Paragraph>())
    .ToList();

I hope this helps.

1 Like

Thank you very much. Could you check why the library detects only 1 paragraph in this file and the Microsoft library detects 2? File: https://we.tl/t-AldHJsdaYp

There is a strange layout here, the paragraph end is actually located inside the Field element.
In other words, if you press ALT + F9 you’ll notice this:

The problem with this is that GemBox.Document doesn’t support block-level elements inside a Field element. But note that we’re currently working on this feature.

1 Like

Hi. How can I follow updates on the progress of that feature? Thanks.

Hi Mirel,

You can subscribe to this feature request:
https://support.gemboxsoftware.com/community/view/enhance-gembox-document-content-model

I hope this helps.

Regards,
Mario

1 Like