【问题标题】:iTextSharp PdfPCell.VerticalAlignment does Not work in text modeiTextSharp PdfPCell.VerticalAlignment 在文本模式下不起作用
【发布时间】:2014-02-26 17:28:15
【问题描述】:

我是 iTextSharp 的新手。在下面的代码设置 PdfPCell 的 VerticalAlignment (我认为在文本模式下)没有做任何事情,文本仍然与单元格底部对齐。 根据多个在线示例,它应该可以工作,但不能。设置 Horizo​​ntalAlignment 有效。 请帮忙。谢谢

table = new PdfPTable(5);

Document document = new Document(PageSize.A4);
document.Open();

PdfPTable headerTable = new PdfPTable(3);
float[] headerWidths = { 2f, 5f, 2f };
headerTable.SetWidths(headerWidths);

myFont = FontFactory.GetFont("Arial", 8, Font.NORMAL);
PdfPCell myCell = setupHeaderCell("My Text", myFont);


// ADD headerTable to MainTable
// Creates a PdfPCell that accepts the headerTable as a parameter and then adds that cell to the main PdfPTable.
PdfPCell cellHeader = new PdfPCell(headerTable);
cellHeader.Border = PdfPCell.NO_BORDER;
// Sets the column span of the header cell to dataColumNb.
cellHeader.Colspan = 5;
// Adds the above header cell to the table.
table.AddCell(cellHeader);
................

}

private PdfPCell setupHeaderCell(string lineText, Font myFont)
{            
   var cl = new PdfPCell(new Phrase(lineText, myFont);
   cl.VerticalAlignment = Element.ALIGN_MIDDLE;  // does Not change vertical alignment

   return cl;
}

【问题讨论】:

  • 您使用的是哪个 iText 版本?

标签: itextsharp itext


【解决方案1】:

您有嵌套表,所以我不确定您要做什么。 VerticalAlignment 相对于单元格的内容,而不是单元格本身,但我不确定这是否是您的问题。在解决此问题时,我们需要通过消除复杂性并找到仍然产生问题的最简单的版本来简化事情。我已经针对 iTextSharp 5.4.4 测试了下面的代码,它正确地垂直对齐了最左边的列。从这个开始,慢慢添加你的逻辑,并可能包括一些你期望和得到的图纸和/或屏幕截图。

var t2 = new PdfPTable(3);
float[] headerWidths = { 2f, 5f, 2f };
t2.SetWidths(headerWidths);

var cl = new PdfPCell(new Phrase("Test"));
cl.VerticalAlignment = Element.ALIGN_MIDDLE;

t2.AddCell(cl);

t2.AddCell("This\nIs\nMore\nText");
t2.AddCell("This\nIs\nMore\nText");

【讨论】:

  • 请注意:OP 需要提供 iText 版本,最近嵌套表格出现了一些问题。
猜你喜欢
  • 2011-05-05
  • 2017-08-15
  • 2020-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-28
  • 1970-01-01
  • 2014-12-28
相关资源
最近更新 更多