Skip to content

Commit

Permalink
adding document margins to options
Browse files Browse the repository at this point in the history
[minor][add]
Adding missing document margins for configuration.
  • Loading branch information
X39 committed Nov 25, 2023
1 parent b16801e commit e075e96
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 14 deletions.
12 changes: 12 additions & 0 deletions source/X39.Solutions.PdfTemplate/DocumentOptions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using X39.Solutions.PdfTemplate.Data;

namespace X39.Solutions.PdfTemplate;

/// <summary>
Expand Down Expand Up @@ -53,4 +55,14 @@ public float DotsPerMillimeter
/// The product that is converting this document to PDF.
/// </summary>
public string Producer { get; set; } = "";

/// <summary>
/// The margin of the document.
/// </summary>
/// <remarks>
/// Margin is removed from the page size, not added to it.
/// This implies that a margin of 100pt... or 10pt... will result in the same page size,
/// but the content will be moved by 100pt... or 10pt... respectively.
/// </remarks>
public Thickness Margin { get; init; } = new(0, 0, 0, 0);
}
44 changes: 30 additions & 14 deletions source/X39.Solutions.PdfTemplate/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,20 @@ public async Task<IReadOnlyCollection<SKBitmap>> GenerateBitmapsAsync(
{
SKCanvas? canvas = null;
await GenerateAsync(
() =>
{
var bitmap = new SKBitmap((int) Math.Ceiling(pageSize.Width), (int) Math.Ceiling(pageSize.Height));
bitmaps.Add(bitmap);
canvas = new SKCanvas(bitmap);
canvas.Clear(SKColors.White);
return canvas;
},
reader,
cultureInfo,
options,
cancellationToken)
() =>
{
var bitmap = new SKBitmap(
(int) Math.Ceiling(pageSize.Width),
(int) Math.Ceiling(pageSize.Height));
bitmaps.Add(bitmap);
canvas = new SKCanvas(bitmap);
canvas.Clear(SKColors.White);
return canvas;
},
reader,
cultureInfo,
options,
cancellationToken)
.ConfigureAwait(false);
canvas?.Dispose();
return bitmaps.AsReadOnly();
Expand Down Expand Up @@ -206,6 +208,16 @@ private async Task GenerateAsync(
var pageSize = new Size(
options.DotsPerMillimeter * options.PageWidthInMillimeters,
options.DotsPerMillimeter * options.PageHeightInMillimeters);
var marginLeft = options.Margin.Left.ToPixels(pageSize.Width, options.DotsPerInch);
var marginTop = options.Margin.Top.ToPixels(pageSize.Height, options.DotsPerInch);
pageSize = new Size(
pageSize.Width
- marginLeft
- options.Margin.Right.ToPixels(pageSize.Width, options.DotsPerInch),
pageSize.Height
- marginTop
- options.Margin.Bottom.ToPixels(pageSize.Height, options.DotsPerInch)
);

#region Measure

Expand Down Expand Up @@ -234,7 +246,7 @@ private async Task GenerateAsync(
var footerPageSize = pageSize with {Height = pageSize.Height * 0.25F};
foreach (var control in template.FooterControls)
{
var size = control.Arrange(options.DotsPerInch,pageSize, footerPageSize, footerPageSize, cultureInfo);
var size = control.Arrange(options.DotsPerInch, pageSize, footerPageSize, footerPageSize, cultureInfo);
footerSizes.Add(size);
}

Expand All @@ -247,7 +259,7 @@ private async Task GenerateAsync(
var bodyPageSize = pageSize with {Height = pageSize.Height - headerPageSize.Height - footerPageSize.Height};
foreach (var control in template.BodyControls)
{
var size = control.Arrange(options.DotsPerInch,pageSize, bodyPageSize, bodyPageSize, cultureInfo);
var size = control.Arrange(options.DotsPerInch, pageSize, bodyPageSize, bodyPageSize, cultureInfo);
bodySizes.Add(size);
}

Expand Down Expand Up @@ -294,6 +306,8 @@ private async Task GenerateAsync(
for (var i = 0; i < pageCount; i++)
{
using var canvas = nextCanvas();
canvas.Save();
canvas.Translate(marginLeft, marginTop);

canvas.Save();
canvas.ClipRect(
Expand All @@ -316,6 +330,8 @@ private async Task GenerateAsync(
footerCanvasAbstraction.Render(canvas);
canvas.Restore();
currentHeight += bodyPageSize.Height;

canvas.Restore();
}

#endregion
Expand Down

0 comments on commit e075e96

Please sign in to comment.