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