Horizontal text alignment in a PdfPCell

帅比萌擦擦* 提交于 2019-12-29 07:32:11

问题


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.


回答1:


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




回答2:


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);



回答3:


try this,

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



回答4:


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




回答5:


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

Here is a nice example




回答6:


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);



回答7:


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



来源:https://stackoverflow.com/questions/18199344/horizontal-text-alignment-in-a-pdfpcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!