in reply to Formatting Text
You may want to consider looping through @ARGV so you can get more than one input argument.
Showing you how to do it would be far easier then pointing you in the right direction, but here's my thoughts...
You can't build a table of contents until you know what page the sections are on.
You can't build an index to key words until you know what page the keywords are on.
And finally you don't know what page you're on until you squeeze the words into the given line length and squeeze the lines into the given lines per page and account for new sections.
So off the top, you're going to have to read the entire file before you can write the contents, so you should be thinking about how you're going to store the file data in variables.
I would think about reformatting each line as you read it to fit the chars/line restriction.
I would think about keeping a running page counter as you read through the file.
I would think about counting words and keeping a list of page numbers for each word.