Horizontal text alignment in a PdfPCell

I tried all the above solutions and none worked. Then I tried this and that led me to the correct solution

PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("Text")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER };
table.AddCell(c2);

I know it's not required from the OP but you can also have several words centered if separated with \n. Additionally you can also vertically center with Element.ALIGN_MIDDLE

With all that the code is:

PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("AAAAAAAAA\nBBBBBB")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER, VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE };
table.AddCell(c2);

and the result:

Hope it helps


try this,

cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right

try this,

cell = New PdfPCell();
p = New Phrase("value");
cell.AddElement(p);
cell.HorizontalAlignment = Element.ALIGN_CENTER; //Tried with Element.Align_Center Also. Tried Adding this line before adding element also. 
table.AddCell(cell);

Per the comments, the correct answer (which I have just tested locally) is to create a paragraph element and add the alignment directive to the paragraph.

A working block of code which demonstrates this is:

        Font qFont = FontFactory.GetFont(FontFactory.HELVETICA_BOLDOBLIQUE, 10);
        List<float> widths = new List<float>();
        for(int i = 0; i < qs.Selected.Items.Count; i++) {
            widths.Add((pageRectangle.Width - 250)/ qs.Selected.Items.Count);
        }

        PdfPTable table = new PdfPTable(qs.Selected.Items.Count);
        table.HorizontalAlignment = Element.ALIGN_CENTER;
        table.SetTotalWidth(widths.ToArray());

        foreach(System.Web.UI.WebControls.ListItem answer in qs.Selected.Items) {
            cell = new PdfPCell();
            cell.Border = Rectangle.NO_BORDER;
/******************** RELEVANT CODE STARTS HERE ***************************/
            Paragraph p = new Paragraph(answer.Text, aFont);
            p.Alignment = Element.ALIGN_CENTER;
            cell.AddElement(p);
            table.AddCell(cell);
/******************** RELEVANT CODE  ENDS  HERE ***************************/
        }

Credit for this should go to the user Bruno Lowagie but there seems to be some odd drama going on, complaints of downvotes on the correct answer and subsequent deletion of the same.

I'll eat the downvotes if it gets a clear answer posted in the right place at the right time.

Some things are more important than internet-points. <3


I know this is old question, but the proper sollution is to use cell.HorizontalAlignment = 1;

Here is a nice example


Comments

  1. Jaden

    • 2018/7/19

    PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("Text")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER }; table.

  2. Caleb

    • 2017/1/11

    Additionally you can also vertically center with Element.ALIGN_MIDDLE. With all that the code is: PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("AAAAAAAAA BBBBBB")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER, VerticalAlignment = iTextSharp.text.Element.ALIGN_MIDDLE }; table.AddCell(c2); and the result: Hope it helps

  3. Robert

    • 2020/4/13

    Horizontal text alignment in a PdfPCell. I am using this code to align horizontally. cell = New PdfPCell(); p = New Phrase("value"); cell.

  4. Kuqi

    • 2015/9/30

    Horizontally Align a Phrase in your PdfPCell CellTwoHdr.HorizontalAlignment = Element.ALIGN_CENTER;. myTable.AddCell (CellTwoHdr);. CellTreeHdr.HorizontalAlignment = Element.ALIGN_RIGHT;. myTable.AddCell (CellTreeHdr);. Things get a little more interesting when you are rotating one of your table

  5. White

    • 2018/8/24

    What this means is that if you rotate your cell 90 degrees clockwise and top-align the text in your cell, the cell text will appear horizontally 

  6. Brodie

    • 2017/11/13

    iTextSharp PdfPCell Text Alignment Example – Justin Cooney, PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("Text")) It is possible to set alignment for all table cells as example: table.DefaultCell. PdfPCell c2 = new PdfPCell(new iTextSharp.text.Phrase("Text")) { HorizontalAlignment = iTextSharp.text.Element.ALIGN_CENTER }; table.AddCell(c2); I know it's not required from the OP but you can also have several words centered if separated with .

  7. Lee

    • 2021/1/5

    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 

  8. Lambert

    • 2016/4/13

    Active Oldest Votes. 8. You need to set the alignment on the image, not the cell: img10.Alignment = iTextSharp.text.Image.ALIGN_CENTER; Share. Improve this answer. answered Sep 30 '11 at 20:02. Chris Haas.

  9. Steven

    • 2017/12/28

    @Override public void setCellStyler(PdfPCell iCell, ExcelExportEntity entity, String text) { iCell.setHorizontalAlignment(Element.ALIGN_CENTER); iCell.

  10. Laci

    • 2018/8/3

    You say I'm trying text within a row looks with the same horizontal alignment but you also say that you're not talking about left, center or right alignment. I only know one other type of horizontal alignment, and that's justified alignment.

  11. Malakai

    • 2020/12/17

    We can set table cell's content alignment horizontally and vertically. Setting table's cells vertical alignment PdfPCell[] cells = new 

  12. Josiah

    • 2019/11/26

    Gets the horizontal alignment for the cell. Image: getImage() Getter for property image. float: getIndent() Gets the first paragraph line indent. float: getLeading() Gets the fixed leading. float: getMaxHeight() Returns the height of the cell. float: getMinimumHeight() Get the minimum height of the cell. float

  13. Zeke

    • 2016/2/5

    PdfPCell#setHorizontalAlignment() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't 

  14. Cullen

    • 2016/8/25

    PdfPCell cell = New PdfPCell(New Paragraph("Your Text")) cell.HorizontalAlignment = 2 //0=Left, 1=Centre, 2=Right Or cell.

  15. Finnegan

    • 2019/10/14

    com.itextpdf.text.Rectangle extended by com.itextpdf.text.pdf.PdfPCell. All Implemented Interfaces: Element, Gets the horizontal alignment for the cell.

  16. Shehu

    • 2020/1/1

    There are two key methods to specify alignment which are defined in com.itextpdf.text.pdf.PdfPCell. These methods are listed below; setHorizontalAlignment 

Comments are closed.

Recent Posts