How to format a String resulting in a two-column display?
This is my code so far:
string s = ""; int width = 60 - name.Count(Char.IsWhiteSpace); s = s + string.Format("{0,-" + width +"}", "Name: " + name); s = s + string.Format("{0,15}","AAA: "); p1.Add(s); document.Add(p1); string p = ""; int width = 60 - surname.Count(Char.IsWhiteSpace); p = p + string.Format("{0,-"+ width +"}", "Surname: " + surname); p = p + string.Format("{0,15}","BBB: "); p2.Add(p); document.Add(p2); string r = ""; int width = 60 - school.Count(Char.IsWhiteSpace); r = r + string.Format("{0,-"+ width +"}", "School: " + school); r = r + string.Format("{0,15}","CCC: "); p3.Add(r); document.Add(p3);
Name: John Edward Jr.________________AAA: Surname: Pascal Einstein W. Alfi_______BBB: School: St. John___________________CCC:
EXPECTED OUTPUT:
Name: John Edward Jr.__________________AAA: Surname: Pascal Einstein W. Alfi_______BBB: School: St. John_______________________CCC:
int width = 60 - name.Count(Char.IsWhiteSpace);
Question posted on StackOverflow on Dec 27, 2015 by PeMaCN
Suppose that we want to render this data in tabular format:
public static final String[][] DATA = {
{"John Edward Jr.", "AAA"},
{"Pascal Einstein W. Alfi", "BBB"},
};
There are three ways to achieve this using iText.
Solution #1: use a table
Please take a look at the SimpleTable13 example:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
PdfPTable table = new PdfPTable(2);
table.setWidthPercentage(50);
table.setHorizontalAlignment(Element.ALIGN_LEFT);
table.setWidths(new int[]{5, 1});
table.getDefaultCell().setBorder(Rectangle.NO_BORDER);
table.addCell("Name: " + DATA[0][0]);
table.addCell(DATA[0][1]);
table.addCell("Surname: " + DATA[1][0]);
table.addCell(DATA[1][1]);
table.addCell("School: " + DATA[2][0]);
table.addCell(DATA[1][1]);
document.add(table);
document.close();
}
We create a table with 2 columns and we add 6 cells. This will result in a table with 2 columns and 3 rows. As we removed the border and as we told the table that the first column should be 5 times as wide as the second column, the result looks like this:
Using a PdfPTable
Advantage: should there be too much text in one of the cells, then the content will automatically wrap and the size of the table will adapt to accommodate the content.
Solution #2: use tabs
Please take a look at the TableTab example:
public void createPdf(String dest) throws FileNotFoundException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
document.add(createParagraphWithTab("Name: ", DATA[0][0], DATA[0][1]));
document.add(createParagraphWithTab("Surname: ", DATA[1][0], DATA[1][1]));
document.add(createParagraphWithTab("School: ", DATA[2][0], DATA[2][1]));
document.close();
}
public Paragraph createParagraphWithTab(String key, String value1, String value2) {
Paragraph p = new Paragraph();
p.setTabSettings(new TabSettings(200f));
p.add(key);
p.add(value1);
p.add(Chunk.TABBING);
p.add(value2);
return p;
}
In this example, we create a paragraph for which we define tabs of 200 user units. Now when we add a Chunk.TABBING
, the content will jump to that position. The result looks like this:
Using tabs
Disadvantage: when there is too much text in the first column, that text will run into the second column, and a tab will be added at the next 200 user units (could be on the same line, could be on the next line).
Solution #3: use spaces and a monospaced font
This is what you're doing, except that you don't use a monospaced font which doesn't give you the result you want.
Please take a look at the TableSpace example:
public void createPdf(String dest) throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
BaseFont bf = BaseFont.createFont(FONT, BaseFont.CP1250, BaseFont.EMBEDDED);
Font font = new Font(bf, 12);
document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Name", DATA[0][0]), DATA[0][1]));
document.add(createParagraphWithSpaces(font, String.format("%s: %s", "Surname", DATA[1][0]), DATA[1][1]));
document.add(createParagraphWithSpaces(font, String.format("%s: %s", "School", DATA[2][0]), DATA[2][1]));
document.close();
}
public Paragraph createParagraphWithSpaces(Font font, String value1, String value2) {
Paragraph p = new Paragraph();
p.setFont(font);
p.add(String.format("%-35s", value1));
p.add(value2);
return p;
}
Now we format the first String so that it always measures 35 characters. The result looks like this:
Using spaces
Disadvantages: if the text in the first column needs more than 35 characters, it will run through the second column and the text of the second column will be glued to it. You also see that I needed to use a different font. I use PTMono-Regular. Usually, text in a monospaced font takes more space than text in a proportional font.