PDFDocument g.NextPage

I have multiple lines of text that I want to put into a PDFDocument. The text is longer than a single page. The page height of the PDF is 792 which is held in an integer ph. Here’s what I have in an attempt to draw to a second page and third:
if y >= ph then
g.NextPage
end if
y = total height of text so far

In the resulting PDF there are three pages but the text runs off the bottom of the first one and the second and third pages are blank. How do I get the text to draw on the second and third pages?

reset y to 0 after issuying NextPage ?

Are you drawing the text as a “paragraph” or line by line?

If you do it as a paragraph, then you should calculate the height of the text prior to drawing it into the page. If it fits, that’s ok; if not, you need to create two “sections” from that paragraph, draw the first one, call g.NextPage, and draw the second one.

If you’re drawing the text line by line, then you need to keep a counter of the accumulated text height and, as soon it is longer than the page height, you need to issue a g.NextPage, reset the counter and continue to draw the text.