0
votes

Dear Internet Community.

I am trying to build an itextsharp PdfPTable with a row of footer cells with italic text, and I wish to right-align stuff like amount values.

Either I am doing it wrong, or setting alignment AND style is mutually exclusive.

    private void AddCellFooterRightAlign(PdfPTable pdfPTable, string text)
    {
        var phrase = new Phrase(text)
        {
            Font = FontFactory.GetFont("Arial", Font.DEFAULTSIZE, Font.ITALIC),
        };

        var pdfPCell = new PdfPCell(phrase)
        {
            HorizontalAlignment = Element.ALIGN_RIGHT,
        };

        pdfPTable.AddCell(pdfPCell);
    }

This yields a cell with right-aligned, normal-style text.

    private void AddCellFooterRightAlign(PdfPTable pdfPTable, string text)
    {
        var phrase = new Phrase(text)
        {
            Font = FontFactory.GetFont("Arial", Font.DEFAULTSIZE, Font.ITALIC),
        };

        var pdfPCell = new PdfPCell()
        {
            HorizontalAlignment = Element.ALIGN_RIGHT,
        };

        pdfPCell.AddElement(phrase);

        pdfPTable.AddCell(pdfPCell);
    }

This yields the opposite: normal-aligned (left), italic-style text.

Notice the subtle difference: By sending the Phrase-object into the Cell constructor, I retain the alignment, but by using AddElement I retain the font style.

Note: I'm stuck with v.5.5.3.0 for the foreseeable future.

Thanks! -S

1

1 Answers

2
votes

The problem is that you are using a Phrase. Use a Paragraph where you can set the font and the alignment.