6

I want to display two pieces of content (it may a paragraph or text) to the left and right side on a single line. My output should be like

 Name:ABC                                                               date:2015-03-02

How can I do this?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Mohan Lal
  • 153
  • 1
  • 1
  • 10
  • 1
    @Jongware I don't think the question is about justifying text. I think it's about right and left aligning strings. I have provided an answer with several options, including the one you refer to, and I have explained why I think your pointer doesn't answer the question. – Bruno Lowagie Apr 11 '15 at 11:11
  • @BrunoLowagie: correct; OP clarfied his original post afterwards. – Jongware Apr 11 '15 at 11:17
  • @Jongware Aha, I missed that part. I wondered why he didn't use any spaces in his left and right string ;-) I just checked the original question and I am happy to see that the bad code was removed. – Bruno Lowagie Apr 11 '15 at 11:18

2 Answers2

15

Please take a look at the LeftRight example. It offers two different solutions for your problem:

enter image description here

Solution 1: Use glue

By glue, I mean a special Chunk that acts like a separator that separates two (or more) other Chunk objects:

Chunk glue = new Chunk(new VerticalPositionMark());
Paragraph p = new Paragraph("Text to the left");
p.add(new Chunk(glue));
p.add("Text to the right");
document.add(p);

This way, you will have "Text to the left" on the left side and "Text to the right" on the right side.

Solution 2: use a PdfPTable

Suppose that some day, somebody asks you to put something in the middle too, then using PdfPTable is the most future-proof solution:

PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100);
table.addCell(getCell("Text to the left", PdfPCell.ALIGN_LEFT));
table.addCell(getCell("Text in the middle", PdfPCell.ALIGN_CENTER));
table.addCell(getCell("Text to the right", PdfPCell.ALIGN_RIGHT));
document.add(table);

In your case, you only need something to the left and something to the right, so you need to create a table with only two columns: table = new PdfPTable(2).

In case you wander about the getCell() method, this is what it looks like:

public PdfPCell getCell(String text, int alignment) {
    PdfPCell cell = new PdfPCell(new Phrase(text));
    cell.setPadding(0);
    cell.setHorizontalAlignment(alignment);
    cell.setBorder(PdfPCell.NO_BORDER);
    return cell;
}

Solution 3: Justify text

This is explained in the answer to this question: How justify text using iTextSharp?

However, this will lead to strange results as soon as there are spaces in your strings. For instance: it will work if you have "Name:ABC". It won't work if you have "Name: Bruno Lowagie" as "Bruno" and "Lowagie" will move towards the middle if you justify the line.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
0

I did this to work and its working

            Document document = new Document(PageSize.A4, 30, 30, 100, 150);
            document.SetPageSize(iTextSharp.text.PageSize.A4);
            PdfWriter writer = PdfWriter.GetInstance(document, fs);
            writer.PageEvent = new ITextEvents();
            document.Open();
            iTextSharp.text.Font fntHead2 = new iTextSharp.text.Font(bfntHead, 11, 1, BaseColor.BLACK);
            Paragraph para = new Paragraph();
            Chunk glue = new Chunk(new VerticalPositionMark());
            Phrase ph1 = new Phrase();

            Paragraph main = new Paragraph();
            ph1.Add(new Chunk("Left Side", fntHead2)); 
            ph1.Add(glue); // Here I add special chunk to the same phrase.    
            ph1.Add(new Chunk("Right Side", fntHead2)); 
            para.Add(ph1);
            document.Add(para);
Abi Chhetri
  • 1,131
  • 10
  • 15