Is it possible to set a different alignment for text on the same line in the same cell?
Posted on StackOverflow on Sep 15, 2015 by user2866205
If you want to separate two pieces of text in the same Phrase
or Paragraph
, you have to create a Chunk
I often refer to as glue:
Chunk glue = new Chunk(new VerticalPositionMark());
You can use this glue
like this:
public void createPdf(String dest) throws IOException, DocumentException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Chunk glue = new Chunk(new VerticalPositionMark());
PdfPTable table = new PdfPTable(1);
Phrase p = new Phrase();
p.add("Left");
p.add(glue);
p.add("Right");
table.addCell(p);
document.add(table);
document.close();
}
The result looks like this:
As you can see, the special Chunk
we've created separates the Strings
"left"
and "right"
.