在单元格中扩展内部内容iText 7 PDF [英] Stretching internal content inside the cell iText 7 PDF

查看:75
本文介绍了在单元格中扩展内部内容iText 7 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然在与iText战斗,看起来他在碾压我. 我已经在这里问过这个问题,但是很可能这个问题太抽象了,以至于我找不到答案.

I still fight with iText, and it looks like he's running over me. I already asked the question here, but most likely the question was so abstract that I could not get answers.

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

So, I have a table with two cells - notesCell and 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)的代码:

In each cell(notesCell, priceCell), I have an inner table. I want this internal table to be stretched across the size of the parent cell. This is my code for creating two cells(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;
}

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

As I indicated in the comments, if you set SetWidth(UnitValue.CreatePercentValue (100)) on the inner table, everything is fine - it stretches across the width of the parent cell. But if I set SetHeight(UnitValue.CreatePercentValue(100)), the inner table does not stretches across the height of the parent cell. Here is the result: In my previous question, I was advised to use FormXObject, but I don't know how can I apply it to my problem, because using FormXObject implies setting absolute sizes that I do not know initially: var tableTemplate = new PdfFormXObject(new Rectangle(??, ??));

推荐答案

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

I managed to solve the problem myself. Now every inner table has no color, and I'm not trying to stretch it. This is most likely the most important. I paid much attention to cells and decided that I need to work with them. Why? Because each cell has a height equal to the maximum height of some cell in the row. And this is what I need

因此,我希望每个单元格都有颜色,有边距,有圆角并以内部表格的形式包含内容.此外,每个单元格都会自动具有相同的高度-我试图达到的高度.

So, I want each cell to have a color, have margins, have rounded corners, and contain the content in the form of an internal table. In addition, each cell will have the same height automatically - what I was trying to achieve.

首先,我们需要为单元格创建一个自定义渲染器,使您可以绘制带有边距(默认为否)和圆角的单元格.这是我的单元格渲染器:

First, we need to create a custom Renderer for cells, that allows you to draw cells with margins(no by default) and with rounded corners. This is my Renderer for cells:

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

仅此而已!现在,您要做的就是创建单元格,为其添加边距和此自定义渲染器:

That's all! Now all you have to do is create cells, add them margins and this custom renderer:

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

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

And voila, we get cells with margins, rounded corners, and most importantly - they are stretched in height:

这篇关于在单元格中扩展内部内容iText 7 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆