C# PdfPCell 中的水平文本对齐方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18199344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Horizontal text alignment in a PdfPCell
提问by yogesh420
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.
我正在创建一个包含 5 列的表格,并在运行时在 for 循环中使用上述代码动态添加单元格。我希望所有单元格内容都居中。
回答by Manish Sharma
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);
回答by david
回答by Muneeb Siddiqui
try this,
尝试这个,
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
回答by Alex C
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
回答by Patrick
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
我知道 OP 不需要它,但是如果用 \n 分隔,您也可以有几个居中的单词。此外,您还可以使用 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
希望能帮助到你
回答by Sharunas Bielskis
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);
回答by rizkidzulkarnain
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
看起来相似但结果不同我不知道为什么