6

表のセルでテキストを垂直方向に揃える方法がわかりません。水平方向の配置は問題ありません。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;
}
4

3 に答える 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 を生成します。ここに画像の説明を入力

于 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 に答える