Right aligning text in PdfPCell

Here is my derivation of user2660112's answer - one method to return a cell for insertion into a bordered and background-colored table, and a similar, but borderless/colorless variety:

private static PdfPCell GetCellForBorderedTable(Phrase phrase, int align, BaseColor color)
{
    PdfPCell cell = new PdfPCell(phrase);
    cell.HorizontalAlignment = align;
    cell.PaddingBottom = 2f;
    cell.PaddingTop = 0f;
    cell.BackgroundColor = color;
    cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
    return cell;
}

private static PdfPCell GetCellForBorderlessTable(Phrase phrase, int align)
{
    PdfPCell cell = new PdfPCell(phrase);
    cell.HorizontalAlignment = align;            
    cell.PaddingBottom = 2f;
    cell.PaddingTop = 0f;
    cell.BorderWidth = PdfPCell.NO_BORDER;
    cell.VerticalAlignment = PdfPCell.ALIGN_CENTER;
    return cell;
}

These can then be called like so:

Font timesRoman9Font = FontFactory.GetFont(FontFactory.TIMES_ROMAN, 9, BaseColor.BLACK);
Font timesRoman9BoldFont = FontFactory.GetFont(FontFactory.TIMES_BOLD, 9, BaseColor.BLACK);

Phrase phrasesec1Heading = new Phrase("Duckbills Unlimited", timesRoman9BoldFont);
PdfPCell cellSec1Heading = GetCellForBorderedTable(phrasesec1Heading, Element.ALIGN_LEFT, BaseColor.YELLOW);
tblHeadings.AddCell(cellSec1Heading);

Phrase phrasePoisonToe = new Phrase("Poison Toe Toxicity Level (Metric Richter Scale, adjusted for follicle hue)", timesRoman9Font);
PdfPCell cellPoisonToe = GetCellForBorderlessTable(phrasePoisonToe, Element.ALIGN_LEFT);
tblFirstRow.AddCell(cellPoisonToe);

private static PdfPCell PhraseCell(Phrase phrase, int align)
{
    PdfPCell cell = new PdfPCell(phrase);
    cell.BorderColor = BaseColor.WHITE;
    // cell.VerticalAlignment = PdfCell.ALIGN_TOP;
    //cell.VerticalAlignment = align;
    cell.HorizontalAlignment = align;
    cell.PaddingBottom = 2f;
    cell.PaddingTop = 0f;
    return cell;
}

I'm the original developer of iText, and the problem you're experiencing is explained in my book.

You're mixing text mode and composite mode.

In text mode, you create the PdfPCell with a Phrase as the parameter of the constructor, and you define the alignment at the level of the cell. However, you're working in composite mode. This mode is triggered as soon as you use the addElement() method. In composite mode, the alignment defined at the level of the cell is ignored (which explains your problem). Instead, the alignment of the separate elements is used.

How to solve your problem?

Either work in text mode by adding your Phrase to the cell in a different way. Or work in composite mode and use a Paragraph for which you define the alignment.

The advantage of composite mode over text mode is that different paragraphs in the same cell can have different alignments, whereas you can only have one alignment in text mode. Another advantage is that you can add more than just text: you can also add images, lists, tables,... An advantage of text mode is speed: it takes less processing time to deal with the content of a cell.

Tags:

C#

Itextsharp