无法垂直对齐表格单元格中的文本与itextsharp

我无法弄清楚如何垂直对齐表格单元格中的文本。 水平对齐没问题。 我使用itextsharp生成pdf。 对齐应该应用于表kitosKalbosTable中的单元格。 任何帮助,将不胜感激。 这是我的代码:

var table = new PdfPTable(new float[] { 36, 1, 63 }); table.WidthPercentage = 100.0f; table.HorizontalAlignment = Element.ALIGN_LEFT; table.DefaultCell.Border = Rectangle.NO_BORDER; table.SplitRows = false; ......... PdfPTable kitosKalbosTable = new PdfPTable(new float[] {10, 30}); kitosKalbosTable.TotalWidth = 40f; kitosKalbosTable.SplitRows = false; kitosKalbosTable.AddCell("Kalba", FontType.SmallTimes, vAligment: Element.ALIGN_MIDDLE, hAligment: Element.ALIGN_CENTER); .......... table.AddCell(kitosKalbosTable); //method in other file public static PdfPCell CreateCell( string text, FontType? fontType = FontType.RegularTimes, int? rotation = null, int? colspan = null, int? rowspan = null, int? hAligment = null, int? vAligment = null, int? height = null, int? border = null, int[] disableBorders = null, int? paddinLeft = null, int? paddingRight = null, bool? splitLate = null) { var cell = new PdfPCell(); ............ if (vAligment.HasValue) { cell.VerticalAlignment = vAligment.Value; } return cell; } 

您有一个似乎使用嵌套表和扩展方法的复杂示例。 正如Alexis所指出的, VerticalAlignment是正确的属性。 下面是一个完整的例子。 我建议暂时摆脱你的扩展方法,并从这个例子开始。

 //Our test file to output var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test.pdf"); //Standard PDF setup, nothing special here using (var fs = new FileStream(testFile, FileMode.Create, FileAccess.Write, FileShare.None)) { using (var doc = new Document()) { using (var writer = PdfWriter.GetInstance(doc, fs)) { doc.Open(); //Create our outer table with two columns var outerTable = new PdfPTable(2); //Create our inner table with just a single column var innerTable = new PdfPTable(1); //Add a middle-align cell to the new table var innerTableCell = new PdfPCell(new Phrase("Inner")); innerTableCell.VerticalAlignment = Element.ALIGN_MIDDLE; innerTable.AddCell(innerTableCell); //Add the inner table to the outer table outerTable.AddCell(innerTable); //Create and add a vertically longer second cell to the outer table var outerTableCell = new PdfPCell(new Phrase("Hello\nWorld\nHello\nWorld")); outerTable.AddCell(outerTableCell); //Add the table to the document doc.Add(outerTable); doc.Close(); } } } 

此代码生成此PDF: 在此处输入图像描述

使用

 cell.VerticalAlignment = Element.ALIGN_MIDDLE; // or ALIGN_TOP or ALIGN_BOTTOM 

此外,您可以通过设置为所有单元格设置默认垂直对齐方式

 kitosKalbosTable.DefaultCell.VerticalAlignment 

似乎Element.ALIGN_MIDDLE只有在单元格高度大于文本高度时才有效。 在PDF中,默认情况下单元格中文本的边距很大,请参阅iText开发人员

您可以在字符串中附加\ n和空格来解决此问题,但单元格高度会变得更大。

对于普通文本,一种提升几个像素块中文本的方法是:

  myChunk.SetTextRise(value); 

唯一的缺点:当文本加下划线(如链接)时,它只会引发文本! 不是下划线..

以下似乎也适用于带下划线的文本,

  myCell.PaddingBottom = value; 

想法是在表格单元格中放置一个链接。蓝色字体,带下划线,垂直居中。 我的代码现在:

  iTextSharp.text.Font linksFont = FontFactory.GetFont(FontFactory.HELVETICA, 10, Font.UNDERLINE, BaseColor.BLUE); PdfPTable myTable = new PdfPTable(1); Chunk pt = new Chunk("Go Google Dutch", linksFont); pt.SetAnchor(new Uri("https://www.google.nl")); Phrase ph1 = new Phrase(pt); PdfPCell cellP = new PdfPCell(); cellP.PaddingBottom = linksFont.CalculatedSize/2; cellP.AddElement(ph1); myTable.AddCell(cellP);