【发布时间】: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,这对解决我的问题有何帮助?