How to print correct pagenumbers in a PDF file (TAdvPDFLib)?

We have a configuration tool in C++ and i want to create a PDF documentation (using TAdvPDFLib) depending on the active user configuration. It has to contain a frontpage, table of contents and multiple chapters whose content depends on the options en settings selected in the configuration tool. This means that i do not know the size and content the document at the point where you normally would add the table of contents. This means the table of contents needs te be created after all the other chapters are created. I was able to do this using de InsertPage(); function, but it messes up the page numbering. For example instead of page 1 to 7, the page numbering looks like 1-7-2-3-4-5-6. Also the table of contents varies from 1 to 4 pages, so i do not know the page numbers untill i create te table of contents.

Below is a simplified version of the PDF creation (with messed up page numbers):

class pdf_document : public TAdvPDFLib
{
   public:
      pdf_document() {}
      ~pdf_document() {}

      void CreateDoc(UnicodeString file_name);
};

void vri_specification::create_specification()
{
   pdf_document* pdf_file = new pdf_document();

   pdf_file->CreateDoc("C:\\Temp\\test.pdf");

   delete pdf_file;
}

void pdf_document::CreateDoc(UnicodeString file_name)
{
   BeginDocument(file_name);

   PageSize = TAdvPDFLibPageSize::psA4;
   PageOrientation = TAdvPDFLibPageOrientation::poPortrait;

   Header = "";
   Footer = "";
   PageNumber = pnFooter;
   PageNumberFormat = "page %d";
   PageNumberAlignment = gtaTrailing;

   Graphics()->Font->Name = "Arial";
   Graphics()->Font->Size = 10;
   Graphics()->Font->Color = clBlack;

   NewPage();
   Graphics()->DrawText("This is the title page", RectF(25, 25, 200, 200));

   for (int i = 1; i <= 5; ++i)
   {
      NewPage();
      Graphics()->DrawText(UnicodeString("Create new chapter: ") + UnicodeString(i), RectF(25, 25, 200, 200));
   }

   InsertPage(0);
   Graphics()->DrawText("Create table of contents", RectF(25, 25, 200, 200));


   EndDocument(true);
}

A solution would be to manualy add the page numbers on each page after all pages (including the table of content) are created. I found a topic where multiple pdf documents are merged (Merge pdf in Nativepdflib). So i tried to write de chapters into a TMemoryStream and then create a new PDF document starting with the frontpage and table of contents. Then i wanted to merge it with the pages of the TMemoryStream in the same way as the other topic. The point is that OpenDocument(), GetPageInfo() and DrawPage() doesn't seem to do anything (GetPageCount() returns 0 or the number of pages of the active document). I tried different things like a separate object and first writing it to a file, but noting seems to work. I put in a TMemoryStream.SaveToFile() to check if the content was vallid and that all looked fine.

Below is a simplified version of the PDF creation with a TMemoryStream:

void pdf_document::CreateDoc(UnicodeString file_name)
{
   BeginDocument();

   PageSize = TAdvPDFLibPageSize::psA4;
   PageOrientation = TAdvPDFLibPageOrientation::poPortrait;

   Header = "";
   Footer = "";
   PageNumber = pnFooter;
   PageNumberFormat = "page %d";
   PageNumberAlignment = gtaTrailing;

   Graphics()->Font->Name = "Arial";
   Graphics()->Font->Size = 10;
   Graphics()->Font->Color = clBlack;

   for (int i = 1; i <= 5; ++i)
   {
      NewPage();
      Graphics()->DrawText(UnicodeString("Create new chapter: ") + UnicodeString(i), RectF(25, 25, 200, 200));
   }

   TMemoryStream* pdf_stream = EndDocument();

   BeginDocument(file_name);

   NewPage();
   Graphics()->DrawText("This is the title page", RectF(25, 25, 200, 200));

   NewPage();
   Graphics()->DrawText("Create table of contents", RectF(25, 25, 200, 200));

   OpenDocument(pdf_stream);

   int nr_of_pages = GetPageCount();

   for (int i = 0; i < nr_of_pages; i++)
   {
      GetPageInfo(i);
      NewPage();
      DrawPage(i);
      Graphics()->DrawText("Draw some page numbers here", RectF(50, 25, 200, 200));
   }

   CloseDocument();

   pdf_stream->SaveToFile("C:\\Temp\\pdf_stream.pdf"); // Only for testing the validity of TMemoryStream* pdf_stream

   EndDocument(true);
}

Is there any way to solve this issue or another way to write on existing pages?

Merging is only available on the native PDF libaries on TMS iCL (iOS). For TAdvPDFLib on Windows, it's not possible to manipulate text after the page has been created. You need to know the page number before writing them on the page.