【问题标题】:Unable to vertically align text in table cell with itextsharp无法使用 itextsharp 垂直对齐表格单元格中的文本
【发布时间】:2013-11-25 20:57:14
【问题描述】:

我不知道如何垂直对齐表格单元格中的文本。水平对齐没问题。我使用 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;
}

【问题讨论】:

    标签: c# itextsharp itext cell vertical-alignment


    【解决方案1】:

    您有一个复杂的示例,似乎使用了嵌套表和扩展方法。正如亚历克西斯指出的那样,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:

    【讨论】:

    • 即使在您将上边距和下边距都设置为零后,上边距仍会显示一些值。这是单行短语的情况。但是您可以设置单元格的绝对高度并使下边距“接近”到上边距高度,因此可以完美调整中间对齐。如果您仔细查看 Chris Haas 示例,您会注意到第一列文本不是完全居中(顶部有一个额外的高度)。如果将对齐方式更改为顶部或底部,效果会更明显。
    【解决方案2】:

    使用

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

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

    kitosKalbosTable.DefaultCell.VerticalAlignment
    

    【讨论】:

    • 我也尝试了这两个选项,但似乎都没有做任何事情。
    • 是的,cellBla.VerticalAlignment = Element.ALIGN_MIDDLE 也不适合我; ALIGN_CENTER 也没有。
    • 感谢DefaultCell 的注意 - 使用它清理了我的代码。
    【解决方案3】:

    似乎 Element.ALIGN_MIDDLE 仅在单元格高度很大时才有效,而不是文本高度。 在 PDF 中,默认情况下单元格中文本的边距很大,请参阅iText developers

    您可以在字符串中附加一个 \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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多