简体   繁体   English

PdfPCell中的水平文本对齐

[英]Horizontal text alignment in a PdfPCell

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. 我正在创建一个包含5列的表,并在运行时使用上面的代码在for循环中动态添加单元格。 I want all cells content to be centered. 我希望所有单元格内容都居中。

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

试试这个,

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

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. 我知道OP不需要它,但如果用\\ n分隔,你也可以将几个单词居中。 Additionally you can also vertically center with Element.ALIGN_MIDDLE 此外,您还可以使用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 希望能帮助到你

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. 对此应该归功于用户Bruno Lowagie,但似乎有一些奇怪的戏剧正在发生,对正确答案的暗示投票和随后删除相同的投诉。

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 <3

I know this is old question, but the proper sollution is to use cell.HorizontalAlignment = 1; 我知道这是一个老问题,但适当的解决方法是使用cell.HorizontalAlignment = 1;

Here is a nice example 这是一个很好的例子

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

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 看起来相似但结果不同我不知道为什么

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM