iText pdf library
Website search

Is it possible to set a different alignment for text on the same line in the same cell?

In one cell and on the same line, I must add two text (name and date). The first snippet of text must be on the left page side, the second one on the right, and everything must be in one line. I've tried used Paragraphs, Chunks and Phrases but I don't know how to do it.

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));;
    Chunk glue = new Chunk(new VerticalPositionMark());
    PdfPTable table = new PdfPTable(1);
    Phrase p = new Phrase();

The result looks like this:

As you can see, the special Chunk we've created separates the Strings "left" and "right".

Ready to use iText?

Try our iText 7 Library and add-ons FREE for 30 days. Test your proof of concept, and see if our solution is right for you.

Get my FREE trial

Still have questions? 

We're happy to answer your questions. Reach out to us and we'll get back to you shortly.

Contact us
Stay updated

Join 11,000+ subscribers and become an iText PDF expert by staying up to date with our new products, updates, tips, technical solutions and happenings.

Subscribe Now