Horizontal text alignment in a PdfPCell

2020-02-12 03:42发布

I am using this code to align horizontally.

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

It's not working.

I am creating a table with 5 columns in it and adding cells dynamically in runtime in a for loop with above code. I want all cells content to be centered.

7条回答
Viruses.
2楼-- · 2020-02-12 04:02

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

查看更多
▲ chillily
3楼-- · 2020-02-12 04:12

It is possible to set alignment for all table cells as example:

table.DefaultCell.HorizontalAlignment = Element.ALIGN_JUSTIFIED;

if you need other alignment for some of the table cells you can specify it separately -

Phrase phraseConstant = new Phrase("Name :", font);
PdfPCell cell = new PdfPCell(phraseConstant);
cell.HorizontalAlignment = 0;
table.AddCell(phraseConstant);
查看更多
放我归山
4楼-- · 2020-02-12 04:13

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);
查看更多
我只想做你的唯一
5楼-- · 2020-02-12 04:14

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

Here is a nice example

查看更多
一纸荒年 Trace。
6楼-- · 2020-02-12 04:14

Finally

Change this :

 cell = New PdfPCell();
 p = New Phrase("value");
 cell.AddElement(p);
 cell.HorizontalAlignment = PdfPCell.ALIGN_CENTER; 
 table.AddCell(cell);

with this :

PdfPCell cell = new PdfPCell(new Phrase("value"));
cell.HorizontalAlignment = Element.ALIGN_CENTER;

Looks similar but different in result I don't know why

查看更多
虎瘦雄心在
7楼-- · 2020-02-12 04:18

try this,

cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
查看更多
登录 后发表回答