aspose file tools
The moose likes Java in General and the fly likes How to get character's width in Java? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "How to get character Watch "How to get character New topic
Author

How to get character's width in Java?

Leon Pu
Greenhorn

Joined: Nov 02, 2005
Posts: 22
Hi all,

I'm developing a table like html output which needs pagination for printing. But the content of each line is unknown. To prevent the break in line during pagination, I need to know the exact width of each character and then do new-line. According to new-lines' amount, I can calculate what's the height of this html:tr.

I'm stuck by the algorithm of character's width. Is it possible to get it from Java GUI components likes AWT or Swing?

I'm looking forward to your reply.

Best regards,
Leon
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24041
    
  13

Hi,

Welcome to JavaRanch!

java.awt.FontMetrics is your friend.


[Jess in Action][AskingGoodQuestions]
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Sounds like you could use java.awt.font.LineBreakMeasurer. Also be aware
that the width of a String does not usually equal the sum of the widths
of its characters, because of kerning: some chararacters are smooshed together
the "ff" in "off", or "Aw" in "Away", especially in italics and certain fonts.


There is no emoticon for what I am feeling!
 
I agree. Here's the link: jrebel
 
subject: How to get character's width in Java?
 
Similar Threads
Pagination
PAGINATION
Pagination Problem
jasper-PDF pagination problem
Pagination