Skip to content

Commit 63fb53b

Browse files
committed
Add Zoom Level
1 parent b792a0a commit 63fb53b

4 files changed

Lines changed: 49 additions & 8 deletions

File tree

Source/Demos/HtmlRenderer.Demo.Console/PdfSharpCoreConverter.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public PdfSharpCoreConverter(string sampleRunIdentifier, string basePath) : base
1515
{
1616
}
1717

18-
public async Task GenerateSampleAsync(HtmlSample sample)
18+
public async Task GenerateSampleAsync(HtmlSample sample, double zoomLevel = 1)
1919
{
2020
var config = new PdfGenerateConfig();
2121

@@ -25,6 +25,8 @@ public async Task GenerateSampleAsync(HtmlSample sample)
2525
config.MarginTop = 0;
2626
config.MarginBottom = 0;
2727

28+
config.ZoomLevel = zoomLevel;
29+
2830
var pdf = await PdfGenerator.GeneratePdfAsync(sample.Html, config, imageLoad: OnImageLoaded);
2931
pdf.Save(GetSamplePath(sample));
3032
}

Source/Demos/HtmlRenderer.Demo.Console/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@
2424
foreach (var htmlSample in samples)
2525
{
2626
////Just doing one test here. Comment this for all of them.
27-
if (!htmlSample.FullName.Contains("40")) continue;
27+
if (!htmlSample.FullName.Contains("10")) continue;
2828

2929
//await skia.GenerateSampleAsync(htmlSample);
3030
//await svgSkia.GenerateSampleAsync(htmlSample);
3131
await pdfSharp.GenerateSampleAsync(htmlSample);
32+
//await pdfSharp.GenerateSampleAsync(htmlSample, .5);
3233
}
3334

3435

Source/HtmlRenderer.PdfSharp/PdfGenerateConfig.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public sealed class PdfGenerateConfig
3737
/// </summary>
3838
private PageOrientation _pageOrientation;
3939

40+
/// <summary>
41+
/// the zoom level for the page.
42+
/// </summary>
43+
private double _zoomLevel;
44+
4045
/// <summary>
4146
/// the top margin between the page start and the text
4247
/// </summary>
@@ -86,6 +91,19 @@ public PageOrientation PageOrientation
8691
set { _pageOrientation = value; }
8792
}
8893

94+
/// <summary>
95+
/// The zoom level for the page as a percent, defaults to 1 (100%).
96+
/// </summary>
97+
public double ZoomLevel
98+
{
99+
get { return _zoomLevel; }
100+
set
101+
{
102+
if (value > 0)
103+
_zoomLevel = value;
104+
}
105+
}
106+
89107
/// <summary>
90108
/// the top margin between the page start and the text
91109
/// </summary>

Source/HtmlRenderer.PdfSharp/PdfGenerator.cs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ public static async Task AddPdfPagesAsync(PdfDocument document, string html, Pdf
140140
orgPageSize = new XSize(orgPageSize.Height, orgPageSize.Width);
141141
}
142142

143+
var pdfPageSize = orgPageSize;
144+
145+
if (config.ZoomLevel != 1)
146+
{
147+
// let the layout engine render onto a smaller page, as we'll scale it up when we
148+
// render it to the PDF.
149+
orgPageSize = new XSize(orgPageSize.Width / config.ZoomLevel, orgPageSize.Height / config.ZoomLevel);
150+
}
151+
143152
var pageSize = new XSize(orgPageSize.Width - config.MarginLeft - config.MarginRight, orgPageSize.Height - config.MarginTop - config.MarginBottom);
144153

145154
if (!string.IsNullOrEmpty(html))
@@ -171,22 +180,25 @@ public static async Task AddPdfPagesAsync(PdfDocument document, string html, Pdf
171180
while (scrollOffset > -container.ActualSize.Height)
172181
{
173182
var page = document.AddPage();
174-
page.Height = orgPageSize.Height;
175-
page.Width = orgPageSize.Width;
183+
page.Height = pdfPageSize.Height;
184+
page.Width = pdfPageSize.Width;
176185

177186
using (var g = XGraphics.FromPdfPage(page))
178187
{
179188
//g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
180189
g.IntersectClip(new XRect(0, 0, page.Width, page.Height));
181190

191+
if (config.ZoomLevel != 1)
192+
g.ScaleTransform(config.ZoomLevel);
193+
182194
container.ScrollOffset = new XPoint(0, scrollOffset);
183195
await container.PerformPaintAsync(g);
184196
}
185197
scrollOffset -= pageSize.Height;
186198
}
187199

188200
// add web links and anchors
189-
HandleLinks(document, container, orgPageSize, pageSize);
201+
HandleLinks(document, container, pdfPageSize, pageSize, config.ZoomLevel);
190202
}
191203
}
192204
}
@@ -198,17 +210,25 @@ public static async Task AddPdfPagesAsync(PdfDocument document, string html, Pdf
198210
/// <summary>
199211
/// Handle HTML links by create PDF Documents link either to external URL or to another page in the document.
200212
/// </summary>
201-
private static void HandleLinks(PdfDocument document, HtmlContainer container, XSize orgPageSize, XSize pageSize)
213+
private static void HandleLinks(PdfDocument document, HtmlContainer container, XSize pdfPageSize, XSize pageSize, double zoomLevel)
202214
{
203215
foreach (var link in container.GetLinks())
204216
{
205217
int i = (int)(link.Rectangle.Top / pageSize.Height);
206218
for (; i < document.Pages.Count && pageSize.Height * i < link.Rectangle.Bottom; i++)
207219
{
208-
var offset = pageSize.Height * i;
220+
//Zoom as required.
221+
var offset = pageSize.Height * i * zoomLevel;
222+
var linkRect = new XRect
223+
{
224+
X = link.Rectangle.Left * zoomLevel,
225+
Y = link.Rectangle.Top * zoomLevel,
226+
Width = link.Rectangle.Width * zoomLevel,
227+
Height = link.Rectangle.Height * zoomLevel
228+
};
209229

210230
// fucking position is from the bottom of the page
211-
var xRect = new XRect(link.Rectangle.Left, orgPageSize.Height - (link.Rectangle.Height + link.Rectangle.Top - offset), link.Rectangle.Width, link.Rectangle.Height);
231+
var xRect = new XRect(linkRect.Left, pdfPageSize.Height - (linkRect.Height + linkRect.Top - offset), linkRect.Width, linkRect.Height);
212232

213233
if (link.IsAnchor)
214234
{

0 commit comments

Comments
 (0)