Free Support Forum - aspose.com

'MoveToParagraph' moves to the wrong paragraph

Hi,

i use Aspose.Words to find a specific paragraph and move to the paragraph in the DocumentBuilder.
Unfortunately the function moves to the wrong paragraph although i get the index from himself.

I add a sample project so that you can verify it.

Kind regards,
Guido

Hi Guido,


Thanks for your inquiry. This is because by default the cursor (DocumentBuilder) is positioned at Body’s scope (see Story class) and when you move the cursor to a Paragraph indexed at 10th number, your program will print the text of 11th Paragraph i.e. within the Document’s Body. Please execute the following code to clarify the concept:

Document doc = new
Document(@“C:\Temp\test.docx”);

DocumentBuilder db = new DocumentBuilder(doc);

db.MoveToParagraph(0, 0);

Console.WriteLine(db.CurrentParagraph.Range.Text);

db.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);

db.MoveToParagraph(0, 0);

Console.WriteLine(db.CurrentParagraph.Range.Text);


Best Regards,

Hi Awais,

my problem is that i want to move to the paragraph by index and that
the function ‘indexOf’ gives me an index that i cannot use for
MoveToParagraph. This seems to be unsteady for me.

Why does ‘indexOf’ gives me the wrong index ?

Kind regards.


Hi Guido,


Thanks for your inquiry. Just to clarify, suppose you have 13 Paragraph nodes in your document. Suppose, three of them are contained inside Header and the remaining ten are inside Body (Header and Body are at two different Stories in the Document). When you use DocumentBuilder.MoveToParagraph, the navigation is performed inside the current story of the current section. That is, if you moved the cursor to the primary header of the first section, then paragraphIndex specifies the index of the paragraph inside that header of that section (in our example, the index should be between 0, 1 and 2). Similarly, if you moved the cursor to the Body (it’s the default position), then paragraphIndex specifies the index relative to the Body (although there are 13 Paragraphs in total, but for referring the Paragraphs inside Body, the the valid range would be 0, 2 3 … 9). So, in our example, when you’re using DocumentBuilder.MoveToParagraph method, the 10th, 11th and 12th indexes would be invalid.

I hope, this helps.

Best Regards,