Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Print Previews

Posted on 1998-10-19
Medium Priority
Last Modified: 2013-12-03
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.
Question by:conick
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3

Expert Comment

ID: 1225941
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.


Author Comment

ID: 1225942
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.


Author Comment

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

Thanks again
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.


Expert Comment

ID: 1225944
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.

Author Comment

ID: 1225945

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

Accepted Solution

diakov earned 480 total points
ID: 1225946

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question