Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 97a0d5c

Browse files
author
Arthur
committed
Merge pull request ArthurHub#22 from sergio73/master
Changes to allow custom sizes if these are not defined in PageSize
2 parents 9a92a74 + f0349fd commit 97a0d5c

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

Source/HtmlRenderer.PdfSharp/PdfGenerateConfig.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// "The Art of War"
1212

1313
using PdfSharp;
14+
using PdfSharp.Drawing;
1415

1516
namespace TheArtOfDev.HtmlRenderer.PdfSharp
1617
{
@@ -26,6 +27,11 @@ public sealed class PdfGenerateConfig
2627
/// </summary>
2728
private PageSize _pageSize;
2829

30+
/// <summary>
31+
/// if the page size is undefined this allow you to set manually the page size
32+
/// </summary>
33+
private XSize _xsize;
34+
2935
/// <summary>
3036
/// the top margin between the page start and the text
3137
/// </summary>
@@ -58,6 +64,14 @@ public PageSize PageSize
5864
set { _pageSize = value; }
5965
}
6066

67+
/// <summary>
68+
/// if the page size is undefined this allow you to set manually the page size
69+
/// </summary>
70+
public XSize ManualPageSize {
71+
get { return _xsize; }
72+
set { _xsize = value; }
73+
}
74+
6175
/// <summary>
6276
/// the top margin between the page start and the text
6377
/// </summary>
@@ -119,5 +133,29 @@ public void SetMargins(int value)
119133
if (value > -1)
120134
_marginBottom = _marginLeft = _marginTop = _marginRight = value;
121135
}
136+
137+
// The international definitions are:
138+
// 1 inch == 25.4 mm
139+
// 1 inch == 72 point
140+
141+
/// <summary>
142+
/// Convert the units passed in milimiters to the units used in PdfSharp
143+
/// </summary>
144+
/// <param name="width"></param>
145+
/// <param name="height"></param>
146+
/// <returns></returns>
147+
public static XSize MilimitersToUnits(double width, double height) {
148+
return new XSize(width / 25.4 * 72, height / 25.4 * 72);
149+
}
150+
151+
/// <summary>
152+
/// Convert the units passed in inches to the units used in PdfSharp
153+
/// </summary>
154+
/// <param name="width"></param>
155+
/// <param name="height"></param>
156+
/// <returns></returns>
157+
public static XSize InchesToUnits(double width, double height) {
158+
return new XSize(width * 72, height * 72);
159+
}
122160
}
123161
}

Source/HtmlRenderer.PdfSharp/PdfGenerator.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,13 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
9090
// create PDF document to render the HTML into
9191
var document = new PdfDocument();
9292

93+
XSize orgPageSize;
9394
// get the size of each page to layout the HTML in
94-
var orgPageSize = PageSizeConverter.ToSize(config.PageSize);
95+
if (config.PageSize != PageSize.Undefined)
96+
orgPageSize = PageSizeConverter.ToSize(config.PageSize);
97+
else
98+
orgPageSize = config.ManualPageSize;
99+
95100
var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);
96101

97102
if (!string.IsNullOrEmpty(html))
@@ -118,7 +123,13 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
118123
while (scrollOffset > -container.ActualSize.Height)
119124
{
120125
var page = document.AddPage();
121-
page.Size = config.PageSize;
126+
if (config.PageSize != PageSize.Undefined) {
127+
page.Size = config.PageSize;
128+
}else {
129+
page.Height = config.ManualPageSize.Height;
130+
page.Width = config.ManualPageSize.Width;
131+
}
132+
122133
using (var g = XGraphics.FromPdfPage(page))
123134
{
124135
g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));

0 commit comments

Comments
 (0)