c# – 无法将表格单元格中的文本与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:

转载注明原文:c# – 无法将表格单元格中的文本与itextsharp垂直对齐 - 代码日志