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

Skip to content

Commit 59c6429

Browse files
author
Arthur
committed
Merge pull request ArthurHub#25 from jfillbrook/master
Added ability to set page orientation for PdfSharp
2 parents 180ca6e + 987587e commit 59c6429

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Source/HtmlRenderer.PdfSharp/PdfGenerateConfig.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ public sealed class PdfGenerateConfig
3232
/// </summary>
3333
private XSize _xsize;
3434

35+
/// <summary>
36+
/// the orientation of each page of the generated pdf
37+
/// </summary>
38+
private PageOrientation _pageOrientation;
39+
3540
/// <summary>
3641
/// the top margin between the page start and the text
3742
/// </summary>
@@ -72,6 +77,15 @@ public XSize ManualPageSize {
7277
set { _xsize = value; }
7378
}
7479

80+
/// <summary>
81+
/// the orientation of each page of the generated pdf
82+
/// </summary>
83+
public PageOrientation PageOrientation
84+
{
85+
get { return _pageOrientation; }
86+
set { _pageOrientation = value; }
87+
}
88+
7589
/// <summary>
7690
/// the top margin between the page start and the text
7791
/// </summary>

Source/HtmlRenderer.PdfSharp/PdfGenerator.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
9696
orgPageSize = PageSizeConverter.ToSize(config.PageSize);
9797
else
9898
orgPageSize = config.ManualPageSize;
99-
99+
100+
if (config.PageOrientation == PageOrientation.Landscape)
101+
{
102+
// invert pagesize for landscape
103+
orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
104+
}
105+
100106
var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);
101107

102108
if (!string.IsNullOrEmpty(html))
@@ -123,12 +129,8 @@ public static PdfDocument GeneratePdf(string html, PdfGenerateConfig config, Css
123129
while (scrollOffset > -container.ActualSize.Height)
124130
{
125131
var page = document.AddPage();
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+
page.Height = orgPageSize.Height;
133+
page.Width = orgPageSize.Width;
132134

133135
using (var g = XGraphics.FromPdfPage(page))
134136
{

0 commit comments

Comments
 (0)