I am doing something similar to adjust the font size for individual text fields (so that they all fit). Basically, I create a TextBlock in the code, set all my properties and check the ActualWidth and ActualHeight properties. Here are some pseudo codes to help solve your problem:
public static String PageText(TextBlock txtPage, String BookText) { TextBlock t = new TextBlock(); t.FontFamily = txtPage.FontFamily; t.FontStyle = txtPage.FontStyle; t.FontWeight = txtPage.FontWeight; t.FontSize = txtPage.FontSize; t.Text = BookText; Size Actual = new Size(); Actual.Width = t.ActualWidth; Actual.Height = t.ActualHeight; if(Actual.Height <= txtPage.ActualHeight) return BookText; Double hRatio = txtPage.ActualHeight / Actual.Height; return s.Substring((int)((s.Length - 1) * hRatio)); }
The above is untested code, but hopefully you can get started. Basically, he sees if the text can fit in the box, if that's good for you. If not, he will find out what percentage of the text can fit and return it. This does not take into account word breaks and may not be a perfect match, but should close you.
You can modify this code to return the length, not the actual substring, and use it as the size of your page. Creating a text block in code (without display) actually does pretty well (I do this in some table views without noticeable lag). I would not send all 120,000 words to this function, but a reasonable subset of some kind.
Once you reach your ideal length, you can use RegEx to split your book into pages. There are examples on this RegEx site that break word boundaries after a certain length.
Another option is to calculate the page size in advance for each potential fontsize (and hard code using the switch statement). This can easily go crazy if you allow any fonts and any combination of sizes, and it would be awful if you allowed mixed fonts / sizes, but you did very well. Most likely, you have a certain range of readable sizes and just a few fonts. Creating a test application to calculate the page text length for each of these combinations would not be so difficult and, possibly, would make your life easier - even if it is not "correctly" perceived as a programmer :)
source share