aspose file tools*
The moose likes Java in General and the fly likes How can PrintWriter know we have reached end of page? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How can PrintWriter know we have reached end of page?" Watch "How can PrintWriter know we have reached end of page?" New topic
Author

How can PrintWriter know we have reached end of page?

Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Howdy,

If I was using PrintWriter to write some data to a plain ol' text file like so....



How can I know that I have reached the end of the current page and I am on the next page? I am asking because I also need to print page numbers.

Thanks in advance for any help,
Jahan

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

You would have to know how many lines a page can hold, keep a count, and handle it yourself. The PrintWriter has no way of knowing.
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Thanks Jeff.

Would you or anyone else who views this happen to know how many lines there are in a standard 8 x 12 page given the standard font size that Java prints in?

Thanks again.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61756
    
  67

There is no "standard font" that Java prints in. In fact, there's no concept of printing here at all. You are just creating a text file.

How many lines fit on your "page" is completely and solely dependent upon your system and what tools you use to print the page.

If you want more control over page layout, you'll need to emit something like a PDF rather than a text file.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Bear Bibeault wrote:There is no "standard font" that Java prints in. In fact, there's no concept of printing here at all.


And more importantly, no concept of font. Neither String nor PrintWriter has any font associated with it, nor any knowledge that fonts even exist. All they care about is characters.
Jehan Jaleel
Ranch Hand

Joined: Apr 30, 2002
Posts: 196
Jeff,

You told me that I would need to know how many lines a page can hold, do you have any suggestions how I can figure this from my Java code?

Thanks again for your help.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Jehan Jaleel wrote:Jeff,

You told me that I would need to know how many lines a page can hold, do you have any suggestions how I can figure this from my Java code?


I doubt it's possible for plain text. If you google for something like printing from Java, you'll find examples on how to get information about available printers. You might be able to ask those printers what their default font is, what page size they have, etc., and then you might be able to look at the metrics for that font and calculate how many lines per page.

In reality, though, that's probably an untenable approach. And you could only do it at the point when you go to print. It's absolutely impossible to do when you generate the text file, unless you know where it will be printed. But seriously, I don't think you'll be able to do all that.

As already suggested, look into something like PDF.

Or, when you go to print your text file, in the page setup, you should have the option to add page numbers, and the printer will figure it out.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can PrintWriter know we have reached end of page?