2

I'm creating a pdf document with a grid table based on the PdfPTable in itextpdf. The input data arrives as a java String[][] with all the cells filled. For each column, I iterate over all the rows to identify the maximum columns required to display the data for that column. That becomes the column width. All of the column widths are summed to determine the maximum number of columns for the entire table. At this point, my intent is to calculate the optimum point size for a monospace font to fully occupy the width of the columns. The objective is maximum readability in the face of very dynamic input data. For wide columns, I'm ending up with lots of trailing whitespace instead of filling the column nicely from left to right. A left justified appearance inside the column is desirable. The runtime environment is openjdk-1.6.0 on RHEL5.X. Any fonts in use must be resident on the system as it has no outside connectivity.

The code is fully up and running, but the appearance could be better if the column text fully occupied the column fieids.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165

2 Answers2

3

I've read your question three times, and I don't understand it.

Let me try to rephrase it: Given a certain available width, how do I define the font size for a snippet of text, so that it matches the available width.

First you need a BaseFont object. For instance:

BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);

Now you can ask the base font for the width of this String in 'normalized 1000 units' (these are units used in 'Glyph space'; see ISO-32000-1 for more info):

float glyphWidth = bf.getWidth("WHAT IS THE WIDTH OF THIS STRING?");

Now we can convert these 'normalized 1000 units' to an actual size in points (actually user units, but let's assume that 1 user unit = 1 pt for the sake of simplicity).

For instance: the width of the text "WHAT IS THE WIDTH OF THIS STRING?" when using Courier with size 16pt is:

 float width = glyphWidth * 0.001f * 16f;

Your question is different: you want to know the font size for a given width. That's done like this:

 float fontSize = 1000 * width / glyphwidth;

I hope this answers your question. If not, please rephrase.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • The glyph width is a characteristic I have not included in my calculations. I will insert that change to see if it addresses the problem. Thanks for the quick response. – Mark Edwards Apr 14 '13 at 05:33
  • No joy. Example case: 1 row table, 5 columns, values inserted with addCell("One"), addCell("Two")...addCell("Five"). Integer column width for each column set to the length of the inserted string. Table width set to the width of the page. Text displays aligned left in each table column with lots of space to the right.What must be done to get the value to fill the column fully from left to right? – Mark Edwards Apr 14 '13 at 09:15
  • "Integer column width set to the length of the inserted string" = probably wrong, because you're setting relative widths, not absolute widths! Did you read chapter 4 of my book? "Table width set to the width of the page" = Why are you doing that? Why not set to the sum of the widths of all columns? Moreover, if you set the absolute widths of the columns instead of the relative width, you don't need to set the width of the table. – Bruno Lowagie Apr 14 '13 at 09:56
  • I did read your book, but I had a very pressing deadline. I missed some fine details. Thanks again for your insights. It helps to have feedback from the author of the library :-) – Mark Edwards Apr 14 '13 at 19:48
-1

Simple use follow syntax.

PdfPTable table = new PdfPTable(1);
table.setWidthPercentage(100);
table.addCell(new Phrase("Name", FontFactory.getFont(
                    FontFactory.HELVETICA, 8, Font.BOLD)));
Zealous System
  • 2,080
  • 11
  • 22