使用itextsharp无法在表格单元格中垂直对齐文本

6

我无法弄清如何在表格单元格中垂直对齐文本。水平对齐是可以的。我使用itextsharp生成PDF。对齐应该应用于table 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;
}
3个回答

6

您有一个看起来使用了嵌套表格和扩展方法的复杂示例。正如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文档:enter image description here


1
即使您将顶部和底部边距都设置为零,顶部边距仍将显示一些值。虽然这适用于单行短语。但是,您可以设置单元格的绝对高度,并使底部边距“接近”顶部边距高度,以便完美地调整中间对齐。如果仔细查看Chris Haas的示例,您会注意到第一列文本并不完全居中(顶部有额外的高度)。如果将对齐方式更改为顶部或底部,则会更加明显。 - Sam Saarian

2
使用
cell.VerticalAlignment = Element.ALIGN_MIDDLE; // or ALIGN_TOP or ALIGN_BOTTOM

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

2
我也尝试了这两个选项,但似乎都没有任何作用。 - Adam Jones
2
是的,cellBla.VerticalAlignment = Element.ALIGN_MIDDLE 对我也不起作用;ALIGN_CENTER 也一样。 - B. Clay Shannon-B. Crow Raven
谢谢你提供DefaultCell的提示 - 使用它使我的代码更加简洁。 - Alex C

0

看起来只有当单元格高度相对于文本高度较大时,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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接