Right Align part of a Chunk with iTextSharp
c#, itext
Solution
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`
Problem
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.