Print Previews

Posted on 1998-10-19
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
  • 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


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 120 total points
ID: 1225946

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSumClump challenge 9 115
oracle 11g 23 73
fibonacci ten numbers 4 38
java jdbc batch example 8 29
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

815 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now