简体   繁体   English

iTextSharp:在PdfPCell中使用哪些对齐属性?

[英]iTextSharp: which alignment properties are used in a PdfPCell?

When I use the alignment of the cell so it works: 当我使用单元格的对齐,所以它的工作原理:

PdfPCell cell1 = new PdfPCell(new Phrase("Text" , Font));
cell1.HorizontalAlignment = 2;

But once the alignment does not work: 但是一旦对齐不起作用:

PdfPCell cell1 = new PdfPCell();
cell1.AddElement(new Phrase("Text 1", Font));
cell1.AddElement(new Phrase("Text 2", Font));
cell1.HorizontalAlignment = 2;

The reason? 原因?

You are confusing text mode with composite mode . 您将文本模式复合模式混淆。

In the first code snippet, you work in text mode . 在第一个代码段中,您将以文本模式工作。 This means that the content of the cell is considered to be text only and the properties of the cell are respected, whereas the properties of the elements added to the cell are ignored. 这意味着单元格的内容被认为仅是文本,并且遵循单元格的属性,而忽略了添加到单元格的元素的属性。

In the second code snippet, you work in composite mode . 在第二个代码段中,您将以复合模式工作。 A cell switches to composite mode the moment you use the AddElement() method. 当您使用AddElement()方法时,单元格将切换到复合模式 In this case, the properties of the cell are ignored. 在这种情况下,将忽略单元格的属性。 Instead the properties of the elements is used. 而是使用元素的属性。

For instance: in text mode, the content of the cell can only have one type of alignment. 例如:在文本模式下,单元格的内容只能有一种对齐方式。 In composite mode, you can have a paragraph that is left aligned, a paragraph that is centered, and a paragraph that is right aligned, all in the same cell. 在复合模式下,您可以使一个左对齐的段落,一个居中的段落和一个右对齐的段落,所有这些都在同一个单元格中。

Now yes, it worked. 现在是的,它有效。

PdfPCell cell1 = new PdfPCell();
Paragraph p1 = new Paragraph("Text 1", Font);
p1.Alignment = Element.ALIGN_RIGHT;
Paragraph p2 = new Paragraph("Text 2", Font);
p2.Alignment = Element.ALIGN_RIGHT;

cell1.AddElement(p1);
cell1.AddElement(p2);

Thank you. 谢谢。

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

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