Right Align part of a Chunk with iTextSharp
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 Chunk
s 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 Chunk
s, 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.
Admin
Updated on June 04, 2022Comments
-
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 almost 11 yearsI think your solution is overkill for what is actually asked. I'll provide another answer.
-
Bruno Lowagie almost 11 yearsThis won't work, not even is you change the
HorizontalAlignment
toALIGN_CENTER
. You realle need a table with two columnsnew PdfPTable(2)
and you need to create two cells for each row:new PdfPCell(new Phrase("789456|"))
andnew PdfPCell(new Phrase("Test"));
The code could also be simplified by using a default cell without creating thePdfPCell
objects explicitly. -
Karl Anderson almost 11 years@BrunoLowagie - Fair enough.
-
Admin almost 11 yearsThanks for your answer @KarlAnderson. I agree with BrunoLowagie. Using tables is easier and accomplishes what I need.
-
Admin almost 11 yearsThanks 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 almost 11 yearsI 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.