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

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

本文介绍了无法垂直对齐与iTextSharp的表格单元格中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法弄清楚如何垂直对齐表格单元格的文本。水平对齐方式是确定的。我使用iTextSharp的生成PDF。应对准表kitosKalbosTable应用于细胞。任何帮助,将不胜感激。这里是我的代码:

 无功表=新PdfPTable(新浮法[] 
{
36,1 ,63
});
table.WidthPercentage = 100.0f;
table.Horizo​​ntalAlignment = Element.ALIGN_LEFT;
table.DefaultCell.Border = Rectangle.NO_BORDER;
table.SplitRows = FALSE;
.........
PdfPTable kitosKalbosTable =新PdfPTable(新浮法[] {10,30});
kitosKalbosTable.TotalWidth = 40F;
kitosKalbosTable.SplitRows = FALSE;

kitosKalbosTable.AddCell(Kalba,FontType.SmallTimes,vAligment:Element.ALIGN_MIDDLE,hAligment:Element.ALIGN_CENTER);
..........
table.AddCell(kitosKalbosTable);

//其他文件
方法public static PdfPCell CreateCell(
字符串文本,
FontType?fontType = FontType.RegularTimes,
INT?旋转=空,
INT?合并单元格= NULL,
INT?行跨度= NULL,
INT?hAligment = NULL,
INT?vAligment = NULL,
INT?高度=空,
INT?边界= NULL,
INT [] disableBorders = NULL,
INT?paddinLeft = NULL,
INT?paddingRight = NULL,
布尔?splitLate = NULL)
{
细胞VAR =新PdfPCell();
............

如果(vAligment.HasValue)
{
cell.VerticalAlignment = vAligment.Value;
}

返回细胞;
}


解决方案

您有一个复杂的例子,似乎使用嵌套表和扩展方法。正如亚历克西斯所指出的, VerticalAlignment 是正确的属性来使用。下面就是一个完整的工作的例子。我建议摆脱你的扩展方法,现在,只是用这个例子开始。

  //我们的测试文件输出
VAR TESTFILE = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop)的test.pdf);使用

//标准PDF设置,这里没有什么特别使用
(VAR FS =新的FileStream(TESTFILE,FileMode.Create,FileAccess.Write,FileShare.None)){
(VAR DOC =新的文件()){
使用(VAR作家= PdfWriter.GetInstance(DOC,FS)){
doc.Open();

//创建我们有两列
变种外部表outerTable =新PdfPTable(2);

//只有一列
变种innerTable创建我们的内心表=新PdfPTable(1);

//添加中间对齐电池到新表
变种innerTableCell =新PdfPCell(新词(内在));
innerTableCell.VerticalAlignment = Element.ALIGN_MIDDLE;
innerTable.AddCell(innerTableCell);

//内表添加到外部表
outerTable.AddCell(innerTable);

//创建和垂直较长的第二单元添加到外部表
变种outerTableCell =新PdfPCell(新词(Hello\\\
World\\\
Hello\\\
World));
outerTable.AddCell(outerTableCell);

//将表添加到文件
doc.Add(outerTable);

doc.Close();
}
}
}

这代码产生此PDF:


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;
}
解决方案

You have a complex example that appears to be using nested tables and extension methods. As Alexis pointed out, the VerticalAlignment is the correct property to use. Below is a full working example of this. I recommend getting rid of your extension method for now and just starting with this example.

//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();
        }
    }
}

This code produces this PDF:

这篇关于无法垂直对齐与iTextSharp的表格单元格中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 00:47