Right Align part of a Chunk with iTextSharp

14,874

Solution 1

Please take a look at the following examples: chapter 4. They introduce the concept of a PdfPTable. Instead of creating Chunk objects like this "789456|Test", and then do the impossible to have the separate parts of the content of these Chunks align correctly, you'll discover that it's much easier to create a simple PdfPTable with 2 columns, adding "789456|" and "Test" as content of borderless cells. All other workarounds will inevitably lead to code that is more complex and error-prone.

The answer provided by Karl Anderson is much more complex; the answer provided by Manish Sharma is wrong. Although I don't know C#, I've tried to write an example (based on how I would achieve this in Java):

PdfPTable table = new PdfPTable(2);
table.DefaultCell.Border = PdfPCell.NO_BORDER;
table.DefaultCell.VerticalAlignment = Element.ALIGN_RIGHT;
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("789456|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("456|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_RIGHT;
table.addCell(new Phrase("12345|", f5));
table.DefaultCell.HorizontalAlignment = Element.ALIGN_LEFT;
table.addCell(new Phrase("Test", f5));
doc.Add(table);

Note that the default width of a table is 80% of the available width (the horizontal space between the margins) and that the table is center aligned by default. You may want to change these defaults using WidthPercentage and HorizontalAlignment

Solution 2

Unfortunately, you can only apply the alignment to the Paragraph object and not the Chunks, so you will need to use a page layout using columns.

Read iTextSharp - Page Layout with Columns for details on getting layout closer to what you are asking for.

Share:
14,874
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I´m new to iTextSharp and I´m trying to create a PDF. Just a simple example. If I do something like this:

    Paragraph p = new Paragraph();
    
    p.Add(new Chunk("789456|Test", f5));
    newDocument.Add(p);
    p.Add(new Chunk("456|Test", f5));
    newDocument.Add(p);
    p.Add(new Chunk("12345|Test", f5));
    newDocument.Add(p);
    

    I get this result:

    789456|Test
    456|Test
    12345|Test
    

    What can I do to right align part of the text in my chunk. Like this:

    789456|Test
       456|Test
     12345|Test
    

    Thanks in advance.

  • Bruno Lowagie
    Bruno Lowagie almost 11 years
    I think your solution is overkill for what is actually asked. I'll provide another answer.
  • Bruno Lowagie
    Bruno Lowagie almost 11 years
    This won't work, not even is you change the HorizontalAlignment to ALIGN_CENTER. You realle need a table with two columns new PdfPTable(2) and you need to create two cells for each row: new PdfPCell(new Phrase("789456|")) and new PdfPCell(new Phrase("Test")); The code could also be simplified by using a default cell without creating the PdfPCell objects explicitly.
  • Karl Anderson
    Karl Anderson almost 11 years
    @BrunoLowagie - Fair enough.
  • Admin
    Admin almost 11 years
    Thanks for your answer @KarlAnderson. I agree with BrunoLowagie. Using tables is easier and accomplishes what I need.
  • Admin
    Admin almost 11 years
    Thanks for your answer @Manish Sharma, but using just one cell right aligned won´t work for what I need. Only if all the "Test" are equal.
  • Admin
    Admin almost 11 years
    I thought that there was an easy way to do that using chunks and paragraphes, but I guess there isn´t. This solution you proposed worked for me. Thanks a lot for your help.