Free Support Forum - aspose.com

Get Page Numbers of Run Paragraph Nodes in Word Document using C# .NET | LayoutCollector

We are using following code to find page numbers of various elements in document. For some documents, we are getting an exception while finding page numbers. Same document worked correctly in Aspose.Words 20.7. However exception is raised in 21.3. Exception raised is “System.ArgumentNullException: 'Value cannot be null. Parameter name: key’”. Also attached screenshot and input document from Visual Studio for your reference.

    public static void RunPageNumbers(String sourcePath, String OutputPath)
       {
           Document document = new Document(sourcePath);
           LayoutCollector layout = new LayoutCollector(document);
           int pageNumber;
           foreach (Section sct in document)
           {
               NodeCollection Paragraphs = sct.Body.GetChildNodes(NodeType.Paragraph, true);
               foreach (Paragraph para in Paragraphs)
               {
                   NodeCollection run1 = para.GetChildNodes(NodeType.Run, true);                    
                   foreach (Run run in run1)
                   {
                       pageNumber = layout.GetStartPageIndex(run);
                       Console.WriteLine(pageNumber);
                   }
               }
           }
           Console.ReadLine();
           //document.Save(OutputPath);
       }

image.png (14.0 KB)
sample_input.zip (21.6 KB)

@crshekharam,

You are right; the latest 21.3 version of Aspose.Words for .NET throws following exception upon calculating the start page number of a Run node:

System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
Parameter name: key
  Source=mscorlib
  StackTrace:
   at System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   at System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   ...
   at Aspose.Words.Layout.LayoutCollector.GetStartPageIndex(Node node)
   at AwNetTestApp_New.Program.RunPageNumbers(String sourcePath, String OutputPath)

For the sake of any corrections in Aspose.Words API, we have logged this problem in our issue tracking system with ID WORDSNET-22017. We will further look into the details of this problem and will keep you updated on the status of this issue. We apologize for your inconvenience.

@awais.hafeez
Same error is appearing even for calculating page number of paragraph node and this error is occurring only for a few documents.

public static void RunPageNumbers(String sourcePath, String OutputPath)
{
    Document document = new Document(sourcePath);
    LayoutCollector layout = new LayoutCollector(document);
    int pageNumber;
    foreach (Section sct in document)
    {
        NodeCollection Paragraphs = sct.Body.GetChildNodes(NodeType.Paragraph, true);
        foreach (Paragraph para in Paragraphs)
        {
            pageNumber = layout.GetStartPageIndex(para);
            Console.WriteLine(pageNumber);
        }
    }
    Console.ReadLine();
}

@crshekharam,

Please ZIP and upload the Word document you are getting this problem with here for testing. We will then investigate the issue on our end and provide you more information.

Please find file.
sample_input.zip (21.6 KB)

@crshekharam,

To address this problem, we have logged a separate issue with ID WORDSNET-22033. We will notify you here when these issues will get resolved in future. We apologize for your inconvenience.

@awais.hafeez,
Can you please update status on this. This issue became critical for us as this feature worked correctly in previous versions. This issue is not resolved even in 21.4. Is there any alternate way to find page numbers for Paragraphs/Runs?

@crshekharam,

Your issue (WORDSNET-22017) is not resolved yet. We have logged your concerns in our issue tracking system and will keep you posted here on any further updates. We apologize for your inconvenience.

@awais.hafeez
As we are facing issue with layoutcollector to find page number, is there any alternate method to find page number of a paragraph/run

@crshekharam,

We are currently doing analysis of this issue (WORDSNET-22017) to determine the root cause. There are no workarounds available at the moment. Once the analysis of this issue is completed and the root cause is determined, we may then be able to provide you a workaround. We apologize for your inconvenience.

@crshekharam,

We have good news for you i.e. WORDSNET-22017 has now been resolved. The fix of this issue will be included in the next 21.5 version of Aspose.Words. We will inform you via this thread as soon as the next version containing the fix of this issue will be released at the start of next month.