Footer handling from code (preferably C#, but not necessarily)

This c# function gets the xComponent and tries to change the footer for every page but it only displays on the first page, in my rtf file the page styles are:

  • standard - first page

  • convert 1

  • convert 2

  • convert 11

      private static void AddPageNumbers(ref XComponent xComponent)
      {
          // Cast the component to a text document
          XTextDocument xTextDocument = (XTextDocument)xComponent;
    
          // Access the document's style families
          XStyleFamiliesSupplier xStyleFamiliesSupplier = (XStyleFamiliesSupplier)xTextDocument;
          XNameAccess xStyleFamilies = xStyleFamiliesSupplier.getStyleFamilies();
    
          // Get the PageStyles
          XNameAccess xPageStyles = (XNameAccess)xStyleFamilies.getByName("PageStyles").Value;
    
          // Get the Default Page Style
          var styleNames = xPageStyles.getElementNames();
          for (int i = 0; i < styleNames.Length; i++)
          {
              XStyle xPageStyle = (XStyle)xPageStyles.getByName(styleNames[i]).Value;
    
              // Get the footer text from the PageStyle
              XPropertySet xPropertySet = (XPropertySet)xPageStyle;
              XText xFooterText = (XText)xPropertySet.getPropertyValue("FooterText").Value;
              if (xFooterText != null)
              {
                  XTextCursor xFooterCursor = xFooterText.createTextCursor();
                  xFooterCursor.setString("hello");
              }
          }
      }
    

how do i make it so that the function will work on all footers of all pages in the rtf? thanks.

Apparently, your document is not a pure .odt one. You list page styles as Converted99 which means Writer has applied some conversion/translation when opening your document. Consequently what is inside memory is not what is stored on disk.

And the more open-save cycles you have, the more degraded your document will be.

Remember that RTF is an outdated obsolete format which active support has even been dropped by Micro$oft, that many advanced primitives in Writer cannot be represented in RTF (they eventually are stored as rough equivalences using elementary bricks but will never be restored exactly when read back into Writer), that you have no direct access to RTF in Writer.

Unless you have a stringent reason for using RTF (such as retrieving old documents), don’t work in this format. Translate once your document to ODF then save in this native supported format (extension .odt).

Persisting with RTF will damage your documents beyond repair.

PS: when asking here, always mention OS name and LO version.

Cross posted on Reddit

If you cross post, as a courtesy please let us know that you have done so, otherwise it leads to several discussions and a waste of time because several identical answers may be posted by different users.

It seems that the rtf file I had was corrupted, I fixed the rtf file by using TE Edit and the code above suddenly worked on all footers of all pages and not only the first page.