1

I'm trying to get some rows of text on the left side and some on the right side. For some reason iText seems to ignore the alignment entirely.

Example:

// create 200x100 column
ct = new ColumnText(writer.DirectContent);
ct.SetSimpleColumn(0, 0, 200, 100);
ct.AddElement(new Paragraph("entry1"));
ct.AddElement(new Paragraph("entry2"));
ct.AddElement(new Paragraph("entry3"));
ret = ct.Go();

ct.SetSimpleColumn(0, 0, 200, 100);
ct.Alignment = Element.ALIGN_RIGHT;
ct.AddElement(new Paragraph("entry4"));
ct.AddElement(new Paragraph("entry5"));
ct.AddElement(new Paragraph("entry6"));
ret = ct.Go();

I've set the alignment of the 2nd column to Element.ALIGN_RIGHT but the text appears printed on top of column one, rendering unreadable text. Like the alignment was still set to left.

Any ideas?

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
Chuck
  • 1,110
  • 3
  • 15
  • 22

1 Answers1

7

Please google for the concepts "text mode" and "composite mode" or read chapter 3 of my book.

If you work in text mode, you can define the alignment at the level of the ColumnText object. In other words ct.Alignment = Element.ALIGN_RIGHT; will work in text mode.

If you work in composite mode, the alignment at the column level will be ignored in favor of the alignment of the elements added to the column. In your case, iText will ignore the ALIGN_RIGHT in favor of the alignment of the Paragraph objects added to the column. Looking at your code, I see that you didn't define an alignment for the paragraphs, so the default alignment ALIGN_LEFT is used.

How do you know if you're working in text mode or in composite mode? By default, ColumnText uses text mode but it switches to composite mode (removing all previously added text) the moment you invoke the AddElement() method.

As explained in chapter 4 of my book, text mode and composite mode also applies to PdfPCell.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • You're right. If I set the alignment in the Paragraph to the right, it works. Unfortunately, the Paragraph has no constructor that takes the alignment directly. The code does always look messy if you have to add further properties. Therefore I've created a derived class called RightParagraph :) – Chuck Aug 09 '13 at 13:00
  • That's a good solution. I usually create an element factory. That is: for every project, I create a class with static `getParagraph()`, `getAnchor()`, `get...()` methods. That way, when I need to change a font, an alignment,... for the whole project, I only have to apply changes to that custom `ElementFactory` class. – Bruno Lowagie Aug 09 '13 at 13:04