Skip to content

Commit 56cb783

Browse files
committed
Update PDFsharp to 6.2.3
1 parent c4257c4 commit 56cb783

File tree

5 files changed

+71
-23
lines changed

5 files changed

+71
-23
lines changed

Source/HtmlRenderer.PdfSharp/Adapters/PdfSharpAdapter.cs

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212

1313
using PdfSharp.Drawing;
1414
using PdfSharp.Pdf;
15-
using System.Drawing;
16-
using System.Drawing.Text;
15+
using System;
1716
using System.IO;
17+
using FontResolver.PdfSharp;
1818
using TheArtOfDev.HtmlRenderer.Adapters;
1919
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
2020
using TheArtOfDev.HtmlRenderer.PdfSharp.Utilities;
@@ -41,14 +41,16 @@ internal sealed class PdfSharpAdapter : RAdapter
4141
/// </summary>
4242
private PdfSharpAdapter()
4343
{
44+
FontResolverPdfSharp.Register();
45+
4446
AddFontFamilyMapping("monospace", "Courier New");
4547
AddFontFamilyMapping("Helvetica", "Arial");
4648

47-
var families = new InstalledFontCollection();
48-
49-
foreach (var family in families.Families)
49+
var fontFamilies = FontResolver.FontResolver.DiscoverFontFamilies();
50+
51+
foreach (var fontFamily in fontFamilies)
5052
{
51-
AddFontFamily(new FontFamilyAdapter(new XFontFamily(family.Name)));
53+
AddFontFamily(new FontFamilyAdapter(new XFontFamily(fontFamily)));
5254
}
5355
}
5456

@@ -64,8 +66,23 @@ protected override RColor GetColorInt(string colorName)
6466
{
6567
try
6668
{
67-
var color = Color.FromKnownColor((KnownColor)System.Enum.Parse(typeof(KnownColor), colorName, true));
68-
return Utils.Convert(color);
69+
var colorResourceManager = new XColorResourceManager();
70+
71+
var knownColors = XColorResourceManager.GetKnownColors(true);
72+
73+
foreach (var knownColor in knownColors)
74+
{
75+
var name = colorResourceManager.ToColorName(knownColor);
76+
if (!string.Equals(name, colorName, StringComparison.OrdinalIgnoreCase))
77+
{
78+
continue;
79+
}
80+
81+
var xColor = XColor.FromKnownColor(knownColor);
82+
return xColor.IsEmpty ? RColor.Empty : Utils.Convert(xColor);
83+
}
84+
85+
return RColor.Empty;
6986
}
7087
catch
7188
{
@@ -119,14 +136,14 @@ protected override RImage ImageFromStreamInt(Stream memoryStream)
119136

120137
protected override RFont CreateFontInt(string family, double size, RFontStyle style)
121138
{
122-
var fontStyle = (XFontStyle)((int)style);
139+
var fontStyle = Utils.Convert(style);
123140
var xFont = new XFont(family, size, fontStyle, new XPdfFontOptions(PdfFontEncoding.Unicode));
124141
return new FontAdapter(xFont);
125142
}
126143

127144
protected override RFont CreateFontInt(RFontFamily family, double size, RFontStyle style)
128145
{
129-
var fontStyle = (XFontStyle)((int)style);
146+
var fontStyle = Utils.Convert(style);
130147
var xFont = new XFont(((FontFamilyAdapter)family).FontFamily.Name, size, fontStyle, new XPdfFontOptions(PdfFontEncoding.Unicode));
131148
return new FontAdapter(xFont);
132149
}

Source/HtmlRenderer.PdfSharp/HtmlRenderer.PdfSharp.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>net8.0-windows</TargetFramework>
3+
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>
44
<OutputType>Library</OutputType>
55
<RootNamespace>TheArtOfDev.HtmlRenderer.PdfSharp</RootNamespace>
66
<UseWinForms>true</UseWinForms>
@@ -10,13 +10,13 @@
1010
<!-- NuGet Package Properties -->
1111
<PropertyGroup>
1212
<PackageId>HtmlRenderer.PdfSharp</PackageId>
13-
<Title>HTML Renderer for PDF using PdfSharp</Title>
13+
<Title>HTML Renderer for PDF using PDFsharp</Title>
1414
<PackageTags>html render renderer draw pdfsharp</PackageTags>
15-
<Description>PDF document generator from HTML snippet, 100% managed (C#), High performance library using PdfSharp.
15+
<Description>PDF document generator from HTML snippet, 100% managed (C#), High performance library using PDFsharp.
1616

1717
Features and Benefits:
1818
---
19-
* 100% managed code depends only on PdfSharp library, no ActiveX, no MSHTML.
19+
* 100% managed code depends only on the libraries PDFsharp and FontResolver.PdfSharp, no ActiveX, no MSHTML.
2020
* Extensive HTML 4.01 and CSS level 2 specifications support.
2121
* Support separating CSS from HTML by loading stylesheet code separately.
2222
* Handles "real world" malformed HTML, it doesn't have to be XHTML.
@@ -32,7 +32,7 @@ Features and Benefits:
3232
<ProjectReference Include="..\HtmlRenderer\HtmlRenderer.csproj" />
3333
</ItemGroup>
3434
<ItemGroup>
35-
<PackageReference Include="PDFsharp" Version="1.50.5147" />
36-
<PackageReference Include="System.Drawing.Common" Version="10.0.1" />
35+
<PackageReference Include="FontResolver.PdfSharp" Version="1.1.0" />
36+
<PackageReference Include="PDFsharp" Version="6.2.3" />
3737
</ItemGroup>
3838
</Project>

Source/HtmlRenderer.PdfSharp/PdfGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,13 @@ public static void AddPdfPages(PdfDocument document, string html, PdfGenerateCon
171171
while (scrollOffset > -container.ActualSize.Height)
172172
{
173173
var page = document.AddPage();
174-
page.Height = orgPageSize.Height;
175-
page.Width = orgPageSize.Width;
174+
page.Height = XUnit.FromPoint(orgPageSize.Height);
175+
page.Width = XUnit.FromPoint(orgPageSize.Width);
176176

177177
using (var g = XGraphics.FromPdfPage(page))
178178
{
179179
//g.IntersectClip(new XRect(config.MarginLeft, config.MarginTop, pageSize.Width, pageSize.Height));
180-
g.IntersectClip(new XRect(0, 0, page.Width, page.Height));
180+
g.IntersectClip(new XRect(0, 0, page.Width.Point, page.Height.Point));
181181

182182
container.ScrollOffset = new XPoint(0, scrollOffset);
183183
container.PerformPaint(g);
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TheArtOfDev.HtmlRenderer.PdfSharp.Utilities
6+
{
7+
internal class FontFamilyCollection
8+
{
9+
}
10+
}

Source/HtmlRenderer.PdfSharp/Utilities/Utils.cs

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

1313
using PdfSharp.Drawing;
14-
using System.Drawing;
1514
using TheArtOfDev.HtmlRenderer.Adapters.Entities;
1615

1716
namespace TheArtOfDev.HtmlRenderer.PdfSharp.Utilities
@@ -89,11 +88,33 @@ public static XColor Convert(RColor c)
8988
}
9089

9190
/// <summary>
92-
/// Convert from color to WinForms color.
91+
/// Convert from PDFsharp color to WinForms color.
9392
/// </summary>
94-
public static RColor Convert(Color c)
93+
public static RColor Convert(XColor c)
9594
{
96-
return RColor.FromArgb(c.A, c.R, c.G, c.B);
95+
return RColor.FromArgb((byte)(c.A * 255.0), c.R, c.G, c.B);
96+
}
97+
98+
/// <summary>
99+
/// Convert from core font style to PDFsharp font style.
100+
/// </summary>
101+
public static XFontStyleEx Convert(RFontStyle style)
102+
{
103+
var fontStyle = XFontStyleEx.Regular;
104+
105+
if ((style & RFontStyle.Bold) == RFontStyle.Bold)
106+
fontStyle |= XFontStyleEx.Bold;
107+
108+
if ((style & RFontStyle.Italic) == RFontStyle.Italic)
109+
fontStyle |= XFontStyleEx.Italic;
110+
111+
if ((style & RFontStyle.Underline) == RFontStyle.Underline)
112+
fontStyle |= XFontStyleEx.Underline;
113+
114+
if ((style & RFontStyle.Strikeout) == RFontStyle.Strikeout)
115+
fontStyle |= XFontStyleEx.Strikeout;
116+
117+
return fontStyle;
97118
}
98119

99120
}

0 commit comments

Comments
 (0)