Created
March 10, 2023 22:50
-
-
Save flew2bits/5344d8691ac26d7881091cd18d0d5d4e to your computer and use it in GitHub Desktop.
QuestPDF sample Dynamic Component to use available space aboce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using QuestPDF.Elements; | |
using QuestPDF.Fluent; | |
using QuestPDF.Helpers; | |
using QuestPDF.Infrastructure; | |
using QuestPDF.Previewer; | |
using SkiaSharp; | |
Document.Create(doc => | |
{ | |
doc.Page(page => | |
{ | |
page.Size(PageSizes.Letter); | |
page.Margin(18); | |
page.Content().Column(column => | |
{ | |
column.Item().AlignCenter().Text(Placeholders.Label()).FontSize(18); | |
column.Item().Text(Placeholders.Paragraphs()); | |
column.Item().PaddingTop(10).Dynamic(new FillAboveComponent(below => below.Text(Placeholders.Paragraphs()))); | |
}); | |
}); | |
}).ShowInPreviewer(); | |
public class FillAboveComponent : IDynamicComponent<int> | |
{ | |
private readonly Action<IContainer> _below; | |
public FillAboveComponent(Action<IContainer> below) | |
{ | |
_below = below; | |
} | |
public DynamicComponentComposeResult Compose(DynamicContext context) | |
{ | |
var element = context.CreateElement(e => e.Width(context.AvailableSize.Width).Element(_below)); | |
var heightRemaining = context.AvailableSize.Height - element.Size.Height - 10f; | |
var content = context.CreateElement(container => | |
{ | |
container.Column(column => | |
{ | |
column.Spacing(10); | |
column.Item().Height(heightRemaining).Width(context.AvailableSize.Width) | |
//.Background(Colors.Green.Lighten3); | |
.Canvas((canvas, space) => canvas.DrawRect(0, 0, context.AvailableSize.Width, heightRemaining, | |
new SKPaint { StrokeWidth = 2f,Style = SKPaintStyle.Stroke, Color = SKColors.Black })); | |
column.Item().Element(element); | |
}); | |
}); | |
return new DynamicComponentComposeResult | |
{ | |
HasMoreContent = false, | |
Content = content | |
}; | |
} | |
public int State { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment