Print Previews

I'm writing a preview report program as an add on to a DOS program.  My delima
is how to calculate font sizes for the report data.  Im using Java2D for my
printing capabilities (the preview module wont actually be released for 6
months or so)
Is there a standard way to calculate what the font sizes should be given a
report width (whether the user wants to print landscape/portrait and 8.5 X 11/
8.5/14 etc)
What Ive started to do is pretty much go through a while loop and start with a
big font and test to see if the font works for how long the line is and decrement
the font until it does work.
This would be ok for fixed width fonts but I would like to use variable width fonts
and be able to still line up the columns for financial data.
I also normally deal in cpi (characters per inch) so I would like to know how the
point size system works (how it is calculated...especially with variable width fonts)

Ive done some research on JTable and pretty much found that it is hard print anything
unless you print the component itself (including the GUI itself.. not just the data)
I need to show the report onscreen probably using the same Graphics object that I
calculated given the PageFormat.. I think I can handle that if I only knew how to
figure out the calculations.

I dont necessarily need code.. just a good description of how things work

I would appreciate any ideas anyone has for this.
Who is Participating?
diakovConnect With a Mentor Commented:
How about using

g.setFont(new Font(face, style, point));

If in s you have the String you are going to print, and g is the printing Graphics?

From my experience with this I know that Java is quite immature with the printing. Anyway, what I suggest should return at least an approximation how many points the string will take.

Tell me if this works.

conickAuthor Commented:
Possibly I didnt explain myself very well...
I do have the string (and consequently its width) also I have the width of the page (I can get that from PageFormat with getImageableX())

With those two values I can get the font size of a monospaced font that will work...
I can use a while loop similar to:

int fontSize=25;
int maxStringWidth=pf.getImageableX();
FontMetrics currFontMetrics= currFont.getFontMetrics();
currStringWidth= currFontMetrics.stringWidth(s);
while (currStringWidth >= maxStringWidth)  {  
               fontSize -= 2;
               currFont= new Font("SansSerif",Font.BOLD,fontSize);
               currFontMetrics= this.getFontMetrics(currFont);

That will approximate the upper bound of the font size depending on the length of the string.
I would have to use the longest string in the report (all the reports will be of a tabular type so
approximately the same width)

But I still have a couple of problems:

1) I would like to use a Serif or SansSerif font (not monospaced).  If I use a font that is not monospaced I cant predict how the columns will appear (they may not be right underneath the heading (and for financial data lining up the decimal points would be a nightmare).  If necessary I can use a monospaced font for the data and have the heading Serif.

2)  I would like to know how the point sizes in most graphical based fonts are calculated (not cpi).
cpi (characters per inch) are bigger as the numbers get lower (10 cpi is bigger than 12 cpi)
Obviously graphical fonts do not work like that (the bigger the number the bigger the font)

3) How is everyone else going about this?  This should be a fairly common (although new) practice.  With the new Java2D printing capabilities (bundled with JDK1.2) printing CAN be done with java.  I dont even know how its done in windows programming so Im kind of at a disadvantage here.

After reading my initial question I can see how it could be confusing with where Im at in the process.  I hope this comment helps.

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to and use offer code ‘EXPERTS’ to get 10% off your first purchase.

conickAuthor Commented:
The line
FontMetrics currFontMetrics= currFont.getFontMetrics();
should be
FontMetrics currFontMetrics= this.getFontMetrics(currFont);
in the code above

Thanks again
1) Doesn't the max row length calculation work with the Serif and SansSerif fonts? Quote from the docs "...The advance width is the sum of the advance widths of each of the characters in the array..." This is when you use FontMetrics of the printing Graphics.

2) I point in the fonts is meant to be 1/72 of inch


Isuppose this link might be usefull to you.
conickAuthor Commented:

1) It would work for the fonts however I couldnt predict where each data element would start with a nonmonospaced font.  Say the line is 80 char long, the 60th char would probably be on a different x coordinate in one line than it would be in the next line in a Serif or SansSerif font.  In a monospaced font the 60th character would always be on the same x coordinate.
What I could probably do is break the strings down into individual elements and calculate where each column would start and then dump the "substring" into that position.  In fact I'll just write the source file that way and use StringTokenizer()... Anyway I think that question is taken care of.

2) Ah HA ... thats why the docs always measure things in 1/72 of an inch (i thought that was kinda strange)... mystery solved!

3)I completely forgot about the online help at JDC.. I'll check into that.  It looks as though I could get quite a few ideas from that.

Well I think you answered all my current questions (and probably some I would have asked in the future)

Leave an answer and I'll give you a well deserved A
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.