C# 拉伸单元格内的内部内容iText 7 PDF
我还在和iText打架,看起来他在追我。 我已经在这里问了这个问题,但很可能这个问题太抽象了,我无法得到答案 因此,我有一个包含两个单元格的表-notesCell和priceCell:C# 拉伸单元格内的内部内容iText 7 PDF,c#,.net,itext,itext7,C#,.net,Itext,Itext7,我还在和iText打架,看起来他在追我。 我已经在这里问了这个问题,但很可能这个问题太抽象了,我无法得到答案 因此,我有一个包含两个单元格的表-notesCell和priceCell: public void GenerateTable(SimpleProductVM product) { // Create the table var productTable = new Table(new float[] { 1, 1 }) .SetWidth(UnitVal
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();
}
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;
}
在每个单元格(notesCell、priceCell)中,我都有一个内部表。我希望此内部表在父单元格的大小上拉伸。
这是我创建两个单元格(notesCell、priceCell)的代码:
private Cell CreateNotesCell(IList notes)
{
//创建notes单元格
var notesCell=新单元格()
.设置填充(10);
//为注释创建一个内部表
var tableNotes=新表(1)
.SetBackgroundColor(常规背景色)
.SetMargin(10)
//如果我用百分比设置宽度,
//然后将表格拉伸到父单元格的宽度
.SetWidth(UnitValue.CreatePercentValue(100))
//但是如果我把高度设成百分比,
//表格未拉伸到父单元格的高度!!!!
.SetHeight(UnitValue.CreatePercentValue(100));
//添加内部表的标题
tableNotes.AddHeaderCell(新文本单元格,
粗体字,标题前底色,假);
//填写内部表格
foreach(附注中的var附注)
tableNotes.AddCell(新的TextCell($“{note.Name}:{string.Join(“,”,note.Options)}),
LightFont,RegularForegroundColor));
//将内部表格添加到父单元格
添加(表格注释);
退货通知单;
}
专用单元格CreatePriceCell(十进制价格)
{
//创建一个价格单元
var priceCell=新单元格()
.设置填充(10);
//为价格创建一个内部表
var tablePrice=新表(1)
.SetBackgroundColor(常规背景色)
.SetMargin(10)
//如果我用百分比设置宽度,
//然后将表格拉伸到父单元格的宽度
.SetWidth(UnitValue.CreatePercentValue(100))
//但是如果我把高度设成百分比,
//表格未拉伸到父单元格的高度!!!!
.SetHeight(UnitValue.CreatePercentValue(100));
//添加内部表的标题
tablePrice.AddHeaderCell(新文本单元格(“ааааа”),
粗体字体、标题前底色、false、TextAlignment.RIGHT);
//填写内部表格
tablePrice.AddCell(新的TextCell(price.ToString()),
LightFont、RegularForegroundColor、true、TextAlignment.RIGHT);
//将内部表格添加到父单元格
priceCell.Add(tablePrice);
返回单元;
}
正如我在注释中所指出的,如果在内部表格上设置SetWidth(UnitValue.CreatePercentValue(100))
,一切都很好-它会延伸到父单元格的宽度。但是,如果我设置SetHeight(UnitValue.CreatePercentValue(100))
,则内部表格不会跨越父单元格的高度。
结果如下:
在我之前的问题中,有人建议我使用FormXObject
,但我不知道如何将其应用于我的问题,因为使用FormXObject
意味着设置我最初不知道的绝对大小:
var tableTemplate=新的PdfFormXObject(新矩形(?,?)代码>我自己设法解决了这个问题。
现在每个内桌都没有颜色了,我不想把它拉长。这很可能是最重要的。我非常关注细胞,并决定我需要与它们合作。为什么?因为每个单元格的高度等于行中某个单元格的最大高度。这就是我需要的
因此,我希望每个单元格都有颜色、边距、圆角,并以内部表格的形式包含内容。此外,每个单元格将自动具有相同的高度-这正是我试图实现的
首先,我们需要为单元格创建一个自定义渲染器,该渲染器允许您使用边距(默认情况下没有)和圆角绘制单元格。这是我的单元格渲染器:
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();
}
}
公共类圆角СellRenderer:CellRenderer
{
公共圆角СellRenderer(单元模型元素):基础(模型元素)
{
}
公共覆盖iRender GetNextRenderer()=>
新的圆角СellRenderer((单元格)GetModelElement());
受保护的覆盖矩形ApplyMargins(矩形矩形、单位值[]边距、布尔反向)
{
var values=margins.Select(x=>x.GetValue()).ToList();
返回矩形ApplyMargins(值[0]、值[1]、值[2]、值[3]、反向);
}
公共覆盖无效退税地(DrawContext DrawContext)
{
//应用边距
var area=ApplyMargins(getoccupiederabbox(),GetMargins(),false);
//获取背景色
var background=GetProperty(Property.background);
var color=background.GetColor();
//用圆角绘制矩形
var canvas=drawContext.GetCanvas();
canvas.SaveState();
canvas.RoundRectangle(area.GetX()、area.GetY()、area.GetWidth()、area.GetHeight()、5);
canvas.SetFillColor(颜色);
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;
}
private Cell CreateNotesCell(IList notes)
{
//创建notes单元格
var notesCell=新单元格()
.SetBackgroundColor(常规背景色)
.SetPadding(10)
.SetMargins(20,10,20,20);
//设置单元格的自定义渲染器
notesCell.SetNextRenderer(新圆角СellRenderer(notesCell));
//为注释创建一个内部表
var tableNotes=新表(1)
.SetWidth(UnitValue.CreatePercentValue(100));
//添加内部表的标题
tableNotes.AddHeaderCell(新文本单元格,
粗体字,标题前底色,假);
//填充内部选项卡