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

Free Support Forum - aspose.com

CompositeNode.IndexOf throws System.ArgumentOutOfRangeException using C#

When I create a second page with any content in this document I get the following error. System.ArgumentOutOfRangeException: ‘Specified argument was out of range of valid values (Parameter ‘paraIdx’)’ If I remove the second page then the logic works again. Below is a link to the console app that will reproduce the issue.

Thanks,
John

@johnmann04

We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-20015. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Thanks for the update.

@johnmann04

Please note that DocumentBuilder.MoveToParagraph moves the cursor in scope of current section. So, it is necessary to call DocumentBuilder.MoveToSection method before navigating to paragraph from another section. Please check the following code snippet to resolve this issue.

private static void AddPageBreakAndSectionHeader(Document doc, string headerText)
{
    DocumentBuilder builder = new DocumentBuilder(doc);
    LayoutCollector collector = new LayoutCollector(doc);

    int pageIndex = 1;
    for (int index = 0; index < doc.Sections.Count; index++)
    {
        Section section = doc.Sections[index];

        builder.MoveToSection(index); // Move to a specific section.

        NodeCollection paragraphs = section.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph paragraph in paragraphs)
        {
            if (collector.GetEndPageIndex(paragraph) != pageIndex)
                continue;
            builder.MoveToParagraph(paragraphs.IndexOf(paragraph), 0);
            builder.StartBookmark("BM_Page" + pageIndex);
            builder.EndBookmark("BM_Page" + pageIndex);
            pageIndex++;
        }
    }
...
}

Thanks that seems to have fixed the issue.