GetStartPageIndex breaks node text replacement

Hello,

I am trying to replace a tag in a word document with a page number using this code.

     LayoutCollector layout = new LayoutCollector(doc);
            foreach (var node in doc.GetChildNodes(NodeType.Any, true))
            {
                int pagenum = layout.GetStartPageIndex(node);
                 
                if (node.GetText().Contains("PageToBeReplaced"))
                {
                    node.Range.Replace("PageToBeReplaced", pagenum.ToString());
                }
            }

when saved as a pdf the tags “PageToBeReplaced” are not replaced at all.

If I replace pagenum.ToString() with “random string” but keep everything else the same it is still not replacing the tags, If I comment out the following line it replaces the tags with the random string

int pagenum = layout.GetStartPageIndex(node);

it apears that layout.GetStartPageIndex(node) is making doc lose reference to the node.

@ROIAdmin,

Please call Document.UpdatePageLayout Method before saving to PDF format. In case the problem still remains, then please ZIP and attach the following resources here for testing:

  • Your simplified input Word document you are getting this problem with
  • Aspose.Words version 21.3 generated output PDF file showing the undesired behavior
  • Your expected PDF file showing the desired output. You can create this document manually by using MS Word.
  • A standalone simple Console application (source code without compilation errors) that helps us to reproduce your current problem on our end and attach it here for testing. Please do not include Aspose.Words DLL files in it to reduce the file size.

As soon as you get these pieces of information ready, we will start investigation into your scenario/issue and provide you more information.

That worked, Thank you for your help.