6

I can not figure out how to vertically align text in table cell. horizontal alignment is ok. I use itextsharp to generate pdf. Alignment should be applied to cells in table kitosKalbosTable. Any help would be appreciated. here's my code:

    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;
}
4

3 回答 3

6

您有一个复杂的示例,似乎正在使用嵌套表和扩展方法。正如亚历克西斯指出的那样,这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:在此处输入图像描述

于 2013-11-13T14:42:07.713 回答
2

利用

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

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

kitosKalbosTable.DefaultCell.VerticalAlignment
于 2013-11-13T11:00:53.430 回答
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);
于 2018-08-08T22:04:32.010 回答