【问题标题】:Stretching internal content inside the cell iText 7 PDF在单元格 iText 7 PDF 中拉伸内部内容
【发布时间】:2018-12-11 09:29:35
【问题描述】:

我还在和 iText 打架,而且看起来他正在碾压我。 我已经在这里问过这个问题,但很可能这个问题太抽象了,我无法得到答案。

所以,我有一个包含两个单元格的表格 - notesCell 和 priceCell:

public void GenerateTable(SimpleProductVM product)
{
    // Create the table
    var productTable = new Table(new float[] { 1, 1 })
        .SetWidth(UnitValue.CreatePercentValue(100))
        .SetKeepTogether(true);

    // Create a cell of the notes
    var notesCell = CreateNotesCell(product.AdditionalAttributes);

    // Create a cell of the price
    var priceCell = CreatePriceCell(product.Price);

    // Add all of the cells
    productTable.AddRange(notesCell, priceCell);

    var writer = new PdfWriter(PathToFile);
    var pdfDocument = new PdfDocument(writer);
    var document = new Document(pdfDocument);

    document.Add(productTable);

    document.Close();
}

在每个单元格(notesCell、priceCell)中,我都有一个内表。我希望这个内部表被拉伸到父单元格的大小。 这是我创建两个单元格的代码(notesCell,priceCell):

private Cell CreateNotesCell(IList<ProductAttribute> notes)
{
    // Create a notes cell
    var notesCell = new Cell()
        .SetPadding(10);

    // Create an inner table for the notes
    var tableNotes = new Table(1)
        .SetBackgroundColor(RegularBackgroundColor)
        .SetMargin(10)
        // if I set the width in percent, 
        //then the table is stretched to the width of the parent cell
        .SetWidth(UnitValue.CreatePercentValue(100))
        // But if I set the height in percent,
        // the table is not stretched to the height of the parent cell!!!!
        .SetHeight(UnitValue.CreatePercentValue(100));

    // Add a header of the inner table
    tableNotes.AddHeaderCell(new TextCell("Примечание",
        BoldFont, TitleForegroundColor, false));

    // Fill the inner table
    foreach(var note in notes)
        tableNotes.AddCell(new TextCell($"{note.Name}: {string.Join(", ", note.Options)}", 
            LightFont, RegularForegroundColor));

    // Add the inner table to the parent cell
    notesCell.Add(tableNotes);

    return notesCell;
}

private Cell CreatePriceCell(decimal price)
{
    // Create a price cell
    var priceCell = new Cell()
        .SetPadding(10);

    // Create an inner table for the price
    var tablePrice = new Table(1)
        .SetBackgroundColor(RegularBackgroundColor)
        .SetMargin(10)
        // if I set the width in percent, 
        //then the table is stretched to the width of the parent cell
        .SetWidth(UnitValue.CreatePercentValue(100))
        // But if I set the height in percent,
        // the table is not stretched to the height of the parent cell!!!!
        .SetHeight(UnitValue.CreatePercentValue(100));

    // Add a header of the inner table
    tablePrice.AddHeaderCell(new TextCell("Цена",
        BoldFont, TitleForegroundColor, false, TextAlignment.RIGHT));

    // Fill the inner table
    tablePrice.AddCell(new TextCell(price.ToString(), 
        LightFont, RegularForegroundColor, true, TextAlignment.RIGHT));

    // Add the inner table to the parent cell
    priceCell.Add(tablePrice);

    return priceCell;
}

正如我在 cmets 中指出的那样,如果您在内表上设置 SetWidth(UnitValue.CreatePercentValue (100)),一切都很好 - 它横跨父单元格的宽度。但如果我设置SetHeight(UnitValue.CreatePercentValue(100)),则内表不会 延伸到父单元格的高度。 结果如下: 在我之前的问题中,有人建议我使用FormXObject,但我不知道如何将其应用于我的问题,因为使用FormXObject 意味着设置我最初不知道的绝对大小: var tableTemplate = new PdfFormXObject(new Rectangle(??, ??));

【问题讨论】:

  • 您还可以为价格单元格服务合并属性
  • @Nilay,我没有看到这个属性?我在哪里可以找到它?我为什么要使用它?
  • 我的意思是为此使用 ColSpan/RowSpan。更多参考:developers.itextpdf.com/examples/tables/colspan-and-rowspan
  • @Nilay,这对解决我的问题有何帮助?

标签: c# .net itext itext7


【解决方案1】:

我自己设法解决了这个问题。 现在每个内表都没有颜色,我不想拉伸它。这很可能是最重要的。我非常关注细胞,并决定我需要与它们一起工作。为什么?因为每个单元格的高度等于行中某个单元格的最大高度。这就是我需要的

所以,我希望每个单元格都有颜色、边距、圆角,并以内部表格的形式包含内容。此外,每个单元格将自动具有相同的高度 - 这是我试图实现的。

首先,我们需要为单元格创建一个自定义渲染器,它允许您绘制带有边距(默认为无)和圆角的单元格。这是我的单元格渲染器:

public class RoundCornerСellRenderer : CellRenderer
{
    public RoundCornerСellRenderer(Cell modelElement) : base(modelElement)
    {
    }

    public override IRenderer GetNextRenderer() =>
        new RoundCornerСellRenderer((Cell)GetModelElement());

    protected override Rectangle ApplyMargins(Rectangle rect, UnitValue[] margins, bool reverse)
    {
        var values = margins.Select(x => x.GetValue()).ToList();
        return rect.ApplyMargins(values[0], values[1], values[2], values[3], reverse);
    }

    public override void DrawBackground(DrawContext drawContext)
    {
        // Apply the margins
        var area = ApplyMargins(GetOccupiedAreaBBox(), GetMargins(), false);

        // Get background color
        var background = GetProperty<Background>(Property.BACKGROUND);
        var color = background.GetColor();

        // Draw the rectangle with rounded corners
        var canvas = drawContext.GetCanvas();
        canvas.SaveState();
        canvas.RoundRectangle(area.GetX(), area.GetY(), area.GetWidth(), area.GetHeight(), 5);
        canvas.SetFillColor(color);
        canvas.Fill();
        canvas.RestoreState();  
    } 
}

就是这样!现在您所要做的就是创建单元格,为它们添加边距和这个自定义渲染器:

private Cell CreateNotesCell(IList<ProductAttribute> notes)
{
    // Create a notes cell
    var notesCell = new Cell()
        .SetBackgroundColor(RegularBackgroundColor)
        .SetPadding(10)
        .SetMargins(20, 10, 20, 20);

    // Set the our custom renderer for the cell
    notesCell.SetNextRenderer(new RoundCornerСellRenderer(notesCell));

    // Create an inner table for the notes
    var tableNotes = new Table(1)
        .SetWidth(UnitValue.CreatePercentValue(100));

    // Add a header of the inner table
    tableNotes.AddHeaderCell(new TextCell("Примечание",
        BoldFont, TitleForegroundColor, false));

    // Fill the inner table
    foreach(var note in notes)
        tableNotes.AddCell(new TextCell($"{note.Name}: {string.Join(", ", note.Options)}", 
            LightFont, RegularForegroundColor));

    // Add the inner table to the parent cell
    notesCell.Add(tableNotes);

    return notesCell;
}

private Cell CreatePriceCell(decimal price)
{
    // Create a price cell
    var priceCell = new Cell()
        .SetBackgroundColor(RegularBackgroundColor)
        .SetPadding(10)
        .SetMargins(20, 20, 20, 10);

    // Set the our custom renderer for the cell
    priceCell.SetNextRenderer(new RoundCornerСellRenderer(priceCell));

    // Create an inner table for the price
    var tablePrice = new Table(1)
        .SetWidth(UnitValue.CreatePercentValue(100));

    // Add a header of the inner table
    tablePrice.AddHeaderCell(new TextCell("Цена",
        BoldFont, TitleForegroundColor, false, TextAlignment.RIGHT));

    // Fill the inner table
    tablePrice.AddCell(new TextCell(price.ToString(), 
        LightFont, RegularForegroundColor, true, TextAlignment.RIGHT));

    // Add the inner table to the parent cell
    priceCell.Add(tablePrice);

    return priceCell;
}

瞧,我们得到了带有边距、圆角的单元格,最重要的是——它们的高度被拉伸了:

【讨论】:

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