2022-05-09 14:07:50 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
2022-10-08 09:07:03 +03:00
|
|
|
using System.Globalization;
|
2022-05-09 14:07:50 +02:00
|
|
|
using System.Linq;
|
|
|
|
|
using NUnit.Framework;
|
|
|
|
|
using QuestPDF.Elements;
|
|
|
|
|
using QuestPDF.Examples.Engine;
|
|
|
|
|
using QuestPDF.Fluent;
|
|
|
|
|
using QuestPDF.Helpers;
|
|
|
|
|
using QuestPDF.Infrastructure;
|
|
|
|
|
|
|
|
|
|
namespace QuestPDF.Examples
|
|
|
|
|
{
|
|
|
|
|
public class OrdersTable : IDynamicComponent<OrdersTableState>
|
|
|
|
|
{
|
|
|
|
|
private IList<OrderItem> Items { get; }
|
|
|
|
|
public OrdersTableState State { get; set; }
|
|
|
|
|
|
|
|
|
|
public OrdersTable(IList<OrderItem> items)
|
|
|
|
|
{
|
|
|
|
|
Items = items;
|
|
|
|
|
|
|
|
|
|
State = new OrdersTableState
|
|
|
|
|
{
|
|
|
|
|
ShownItemsCount = 0
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DynamicComponentComposeResult Compose(DynamicContext context)
|
|
|
|
|
{
|
|
|
|
|
var possibleItems = Enumerable
|
|
|
|
|
.Range(1, Items.Count - State.ShownItemsCount)
|
|
|
|
|
.Select(itemsToDisplay => ComposeContent(context, itemsToDisplay))
|
|
|
|
|
.TakeWhile(x => x.Size.Height <= context.AvailableSize.Height)
|
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
|
|
State = new OrdersTableState
|
|
|
|
|
{
|
|
|
|
|
ShownItemsCount = State.ShownItemsCount + possibleItems.Count
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return new DynamicComponentComposeResult
|
|
|
|
|
{
|
|
|
|
|
Content = possibleItems.Last(),
|
|
|
|
|
HasMoreContent = State.ShownItemsCount < Items.Count
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private IDynamicElement ComposeContent(DynamicContext context, int itemsToDisplay)
|
|
|
|
|
{
|
|
|
|
|
var total = Items.Skip(State.ShownItemsCount).Take(itemsToDisplay).Sum(x => x.Count * x.Price);
|
|
|
|
|
|
|
|
|
|
return context.CreateElement(container =>
|
|
|
|
|
{
|
|
|
|
|
container
|
|
|
|
|
.MinimalBox()
|
|
|
|
|
.Width(context.AvailableSize.Width)
|
|
|
|
|
.Table(table =>
|
|
|
|
|
{
|
|
|
|
|
table.ColumnsDefinition(columns =>
|
|
|
|
|
{
|
|
|
|
|
columns.ConstantColumn(30);
|
|
|
|
|
columns.RelativeColumn();
|
|
|
|
|
columns.ConstantColumn(50);
|
|
|
|
|
columns.ConstantColumn(50);
|
|
|
|
|
columns.ConstantColumn(50);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
table.Header(header =>
|
|
|
|
|
{
|
|
|
|
|
header.Cell().Element(Style).Text("#");
|
|
|
|
|
header.Cell().Element(Style).Text("Item name");
|
|
|
|
|
header.Cell().Element(Style).AlignRight().Text("Count");
|
|
|
|
|
header.Cell().Element(Style).AlignRight().Text("Price");
|
|
|
|
|
header.Cell().Element(Style).AlignRight().Text("Total");
|
|
|
|
|
|
|
|
|
|
IContainer Style(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
return container
|
|
|
|
|
.DefaultTextStyle(x => x.SemiBold())
|
|
|
|
|
.BorderBottom(1)
|
|
|
|
|
.BorderColor(Colors.Grey.Darken2)
|
|
|
|
|
.Padding(5);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
table.Footer(footer =>
|
|
|
|
|
{
|
|
|
|
|
footer
|
|
|
|
|
.Cell().ColumnSpan(5)
|
|
|
|
|
.AlignRight()
|
2022-05-09 16:32:59 +02:00
|
|
|
.PaddingTop(10)
|
|
|
|
|
.Text($"Subtotal: {total}$", TextStyle.Default.Bold());
|
2022-05-09 14:07:50 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
foreach (var index in Enumerable.Range(State.ShownItemsCount, itemsToDisplay))
|
|
|
|
|
{
|
|
|
|
|
var item = Items[index];
|
|
|
|
|
|
2022-10-08 09:07:03 +03:00
|
|
|
table.Cell().Element(Style).Text((index + 1).ToString(CultureInfo.InvariantCulture));
|
2022-05-09 14:07:50 +02:00
|
|
|
table.Cell().Element(Style).Text(item.ItemName);
|
2022-10-08 09:07:03 +03:00
|
|
|
table.Cell().Element(Style).AlignRight().Text(item.Count.ToString(CultureInfo.InvariantCulture));
|
2022-05-09 14:07:50 +02:00
|
|
|
table.Cell().Element(Style).AlignRight().Text($"{item.Price}$");
|
|
|
|
|
table.Cell().Element(Style).AlignRight().Text($"{item.Count*item.Price}$");
|
|
|
|
|
|
|
|
|
|
IContainer Style(IContainer container)
|
|
|
|
|
{
|
|
|
|
|
return container
|
|
|
|
|
.BorderBottom(1)
|
|
|
|
|
.BorderColor(Colors.Grey.Lighten2)
|
|
|
|
|
.Padding(5);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class DynamicSimpleTableExample
|
|
|
|
|
{
|
|
|
|
|
[Test]
|
|
|
|
|
public static void Dynamic()
|
|
|
|
|
{
|
|
|
|
|
RenderingTest
|
|
|
|
|
.Create()
|
|
|
|
|
.PageSize(PageSizes.A5)
|
|
|
|
|
.ShowResults()
|
2022-05-09 16:32:59 +02:00
|
|
|
.ProduceImages()
|
2022-05-09 14:07:50 +02:00
|
|
|
.Render(container =>
|
|
|
|
|
{
|
2022-05-09 16:32:59 +02:00
|
|
|
var items = Enumerable.Range(0, 15).Select(x => new OrderItem()).ToList();
|
2022-05-09 14:07:50 +02:00
|
|
|
|
|
|
|
|
container
|
|
|
|
|
.Background(Colors.White)
|
|
|
|
|
.Padding(25)
|
2022-05-09 16:32:59 +02:00
|
|
|
.DefaultTextStyle(x => x.FontSize(16))
|
2022-05-09 14:07:50 +02:00
|
|
|
.Decoration(decoration =>
|
|
|
|
|
{
|
|
|
|
|
decoration
|
|
|
|
|
.Header()
|
|
|
|
|
.PaddingBottom(5)
|
|
|
|
|
.Text(text =>
|
|
|
|
|
{
|
2022-05-09 16:32:59 +02:00
|
|
|
text.DefaultTextStyle(TextStyle.Default.SemiBold().FontColor(Colors.Blue.Darken2));
|
2022-05-09 14:07:50 +02:00
|
|
|
text.Span("Page ");
|
|
|
|
|
text.CurrentPageNumber();
|
|
|
|
|
text.Span(" / ");
|
|
|
|
|
text.TotalPages();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
decoration
|
|
|
|
|
.Content()
|
|
|
|
|
.Dynamic(new OrdersTable(items));
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|