Skip to content

Commit 81342de

Browse files
authored
Merge pull request #1 from slagou/master
Merge in @slaguo changes
2 parents e0a2d17 + 7a81677 commit 81342de

13 files changed

Lines changed: 343 additions & 110 deletions

File tree

Source/Demo/Common/HtmlRenderer.Demo.Common.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,24 @@
3030
<ErrorReport>prompt</ErrorReport>
3131
<WarningLevel>4</WarningLevel>
3232
</PropertyGroup>
33+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
34+
<DebugSymbols>true</DebugSymbols>
35+
<OutputPath>bin\x64\Debug\</OutputPath>
36+
<DefineConstants>DEBUG;TRACE</DefineConstants>
37+
<DebugType>full</DebugType>
38+
<PlatformTarget>x64</PlatformTarget>
39+
<ErrorReport>prompt</ErrorReport>
40+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
41+
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
43+
<OutputPath>bin\x64\Release\</OutputPath>
44+
<DefineConstants>TRACE</DefineConstants>
45+
<Optimize>true</Optimize>
46+
<DebugType>pdbonly</DebugType>
47+
<PlatformTarget>x64</PlatformTarget>
48+
<ErrorReport>prompt</ErrorReport>
49+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
50+
</PropertyGroup>
3351
<ItemGroup>
3452
<Reference Include="System" />
3553
<Reference Include="System.Data" />

Source/Demo/Common/Samples/02.Text.htm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
<head>
33
<title>Text</title>
44
<link rel="Stylesheet" href="StyleSheet" />
5-
<style type="text/css">
6-
::selection {
7-
color: #bb5555;
8-
background-color: #aaddaa;
9-
}
5+
<style type="text/css">
6+
::selection {
7+
color: #bb5555;
8+
background-color: #aaddaa;
9+
}
1010
</style>
1111
</head>
1212
<body>

Source/Demo/Common/TestSamples/12.Text.htm

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
<head>
33
<title>Text</title>
44
<link rel="Stylesheet" href="StyleSheet" />
5-
<style>
6-
.right {
7-
text-align: right;
8-
border-width: 1px;
9-
border-style: solid;
10-
border-color: blue;
11-
}
5+
<style>
6+
.right {
7+
text-align: right;
8+
border-width: 1px;
9+
border-style: solid;
10+
border-color: blue;
11+
}
1212
</style>
1313
</head>
1414
<body>
Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
<html>
2-
<body>
3-
<div dir="rtl">
4-
<div>שלום עולם, יש <b>ברבורים </b>בעגם הזה</div>
5-
<br />
6-
<div>שלום עולם, יש ברבורים בעגם הזה</div>
7-
<div>שלום עולם, יש ברבורים בעגם הזה</div>
8-
</div>
9-
<hr />
10-
<div dir="rtl">
11-
<div>שלום עולם,<span>hello world</span> יש ברבורים בעגם הזה</div>
12-
<br />
13-
<div>שלום עולם, יש ברבורים בעגם הזה</div>
14-
<div>שלום עולם, יש ברבורים בעגם הזה</div>
15-
</div>
16-
<hr />
17-
</body>
1+
<html>
2+
<body>
3+
<div dir="rtl">
4+
<div>שלום עולם, יש <b>ברבורים </b>בעגם הזה</div>
5+
<br />
6+
<div>שלום עולם, יש ברבורים בעגם הזה</div>
7+
<div>שלום עולם, יש ברבורים בעגם הזה</div>
8+
</div>
9+
<hr />
10+
<div dir="rtl">
11+
<div>שלום עולם,<span>hello world</span> יש ברבורים בעגם הזה</div>
12+
<br />
13+
<div>שלום עולם, יש ברבורים בעגם הזה</div>
14+
<div>שלום עולם, יש ברבורים בעגם הזה</div>
15+
</div>
16+
<hr />
17+
</body>
1818
</html>

Source/Demo/WPF/HtmlRenderer.Demo.WPF.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@
3939
<PropertyGroup>
4040
<ApplicationIcon>html.ico</ApplicationIcon>
4141
</PropertyGroup>
42+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
43+
<DebugSymbols>true</DebugSymbols>
44+
<OutputPath>bin\x64\Debug\</OutputPath>
45+
<DefineConstants>DEBUG;TRACE</DefineConstants>
46+
<DebugType>full</DebugType>
47+
<PlatformTarget>x64</PlatformTarget>
48+
<ErrorReport>prompt</ErrorReport>
49+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
50+
</PropertyGroup>
51+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
52+
<OutputPath>bin\x64\Release\</OutputPath>
53+
<DefineConstants>TRACE</DefineConstants>
54+
<Optimize>true</Optimize>
55+
<DebugType>pdbonly</DebugType>
56+
<PlatformTarget>x64</PlatformTarget>
57+
<ErrorReport>prompt</ErrorReport>
58+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
59+
</PropertyGroup>
4260
<ItemGroup>
4361
<Reference Include="System" />
4462
<Reference Include="System.Data" />

Source/Demo/WinForms/HtmlRenderer.Demo.WinForms.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@
6262
<PropertyGroup>
6363
<ApplicationIcon>html.ico</ApplicationIcon>
6464
</PropertyGroup>
65+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
66+
<DebugSymbols>true</DebugSymbols>
67+
<OutputPath>bin\x64\Debug\</OutputPath>
68+
<DefineConstants>DEBUG;TRACE</DefineConstants>
69+
<DebugType>full</DebugType>
70+
<PlatformTarget>x64</PlatformTarget>
71+
<ErrorReport>prompt</ErrorReport>
72+
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
73+
</PropertyGroup>
74+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
75+
<OutputPath>bin\x64\Release\</OutputPath>
76+
<DefineConstants>TRACE</DefineConstants>
77+
<Optimize>true</Optimize>
78+
<DebugType>pdbonly</DebugType>
79+
<PlatformTarget>x64</PlatformTarget>
80+
<ErrorReport>prompt</ErrorReport>
81+
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
82+
</PropertyGroup>
6583
<ItemGroup>
6684
<Reference Include="PdfSharp, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
6785
<HintPath>..\..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.dll</HintPath>

Source/HtmlRenderer.PdfSharp/HtmlRenderer.PdfSharp.csproj

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
33
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
44
<PropertyGroup>
55
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
66
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
77
<ProjectGuid>{CA249F5D-9285-40A6-B217-5889EF79FD7E}</ProjectGuid>
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
10-
<RootNamespace>TheArtOfDev.HtmlRenderer.PdfSharp</RootNamespace>
10+
<RootNamespace>HtmlRenderer.PdfSharp</RootNamespace>
1111
<AssemblyName>HtmlRenderer.PdfSharp</AssemblyName>
1212
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
14+
<TargetFrameworkProfile />
1415
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
1516
<RestorePackages>true</RestorePackages>
1617
</PropertyGroup>
@@ -31,13 +32,44 @@
3132
<ErrorReport>prompt</ErrorReport>
3233
<WarningLevel>4</WarningLevel>
3334
</PropertyGroup>
35+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
36+
<DebugSymbols>true</DebugSymbols>
37+
<OutputPath>bin\x86\Debug\</OutputPath>
38+
<DefineConstants>DEBUG;TRACE</DefineConstants>
39+
<DebugType>full</DebugType>
40+
<PlatformTarget>x86</PlatformTarget>
41+
<ErrorReport>prompt</ErrorReport>
42+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
43+
</PropertyGroup>
44+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
45+
<OutputPath>bin\x86\Release\</OutputPath>
46+
<PlatformTarget>x86</PlatformTarget>
47+
</PropertyGroup>
48+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
49+
<DebugSymbols>true</DebugSymbols>
50+
<OutputPath>bin\x64\Debug\</OutputPath>
51+
<DefineConstants>DEBUG;TRACE</DefineConstants>
52+
<DebugType>full</DebugType>
53+
<PlatformTarget>x64</PlatformTarget>
54+
<ErrorReport>prompt</ErrorReport>
55+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
56+
</PropertyGroup>
57+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
58+
<OutputPath>bin\x64\Release\</OutputPath>
59+
<DefineConstants>TRACE</DefineConstants>
60+
<Optimize>true</Optimize>
61+
<DebugType>pdbonly</DebugType>
62+
<PlatformTarget>x64</PlatformTarget>
63+
<ErrorReport>prompt</ErrorReport>
64+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
65+
</PropertyGroup>
3466
<ItemGroup>
3567
<Reference Include="PdfSharp, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
36-
<HintPath>..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.dll</HintPath>
68+
<HintPath>..\..\..\..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.dll</HintPath>
3769
<Private>True</Private>
3870
</Reference>
3971
<Reference Include="PdfSharp.Charting, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
40-
<HintPath>..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.Charting.dll</HintPath>
72+
<HintPath>..\..\..\..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.Charting.dll</HintPath>
4173
<Private>True</Private>
4274
</Reference>
4375
<Reference Include="System" />
@@ -55,20 +87,26 @@
5587
<Compile Include="Adapters\GraphicsAdapter.cs" />
5688
<Compile Include="Adapters\GraphicsPathAdapter.cs" />
5789
<Compile Include="Adapters\ImageAdapter.cs" />
90+
<Compile Include="Adapters\PdfSharpAdapter.cs" />
5891
<Compile Include="Adapters\PenAdapter.cs" />
5992
<Compile Include="Adapters\XTextureBrush.cs" />
60-
<Compile Include="PdfGenerateConfig.cs" />
6193
<Compile Include="HtmlContainer.cs" />
94+
<Compile Include="PdfGenerateConfig.cs" />
6295
<Compile Include="PdfGenerator.cs" />
63-
<Compile Include="Adapters\PdfSharpAdapter.cs" />
6496
<Compile Include="Utilities\Utils.cs" />
6597
</ItemGroup>
6698
<ItemGroup>
6799
<ProjectReference Include="..\HtmlRenderer\HtmlRenderer.csproj">
68-
<Project>{fe611685-391f-4e3e-b27e-d3150e51e49b}</Project>
100+
<Project>{FE611685-391F-4E3E-B27E-D3150E51E49B}</Project>
69101
<Name>HtmlRenderer</Name>
70102
</ProjectReference>
71103
</ItemGroup>
104+
<ItemGroup>
105+
<Reference Include="System">
106+
<HintPath>C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll</HintPath>
107+
</Reference>
108+
<Reference Include="System.Drawing" />
109+
</ItemGroup>
72110
<ItemGroup>
73111
<None Include="packages.config" />
74112
</ItemGroup>

Source/HtmlRenderer.PdfSharp/PdfGenerateConfig.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ public int MarginRight
138138
}
139139
}
140140

141+
/// <summary>
142+
/// Sets weather to Handle HTML links by create PDF
143+
/// Documents link either to external URL or to another page in the document
144+
/// </summary>
145+
public bool HandleLinks { get; set; }
146+
147+
/// <summary>
148+
/// Add a page count footer to the PDF
149+
/// </summary>
150+
public bool AddPageCountFoooter { get; set; }
151+
152+
153+
141154
/// <summary>
142155
/// Set all 4 margins to the given value.
143156
/// </summary>

Source/HtmlRenderer.PdfSharp/PdfGenerator.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,20 +154,21 @@ public static void AddPdfPages(PdfDocument document, string html, PdfGenerateCon
154154
container.Location = new XPoint(config.MarginLeft, config.MarginTop);
155155
container.MaxSize = new XSize(pageSize.Width, 0);
156156
container.SetHtml(html, cssData);
157-
container.PageSize = pageSize;
158-
container.MarginBottom = config.MarginBottom;
157+
container.PageSize = pageSize;
158+
container.MarginBottom = config.MarginBottom;
159159
container.MarginLeft = config.MarginLeft;
160160
container.MarginRight = config.MarginRight;
161161
container.MarginTop = config.MarginTop;
162162

163163
// layout the HTML with the page width restriction to know how many pages are required
164-
using (var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards))
165-
{
166-
container.PerformLayout(measure);
167-
}
164+
var measure = XGraphics.CreateMeasureContext(pageSize, XGraphicsUnit.Point, XPageDirection.Downwards);
165+
container.PerformLayout(measure);
168166

169167
// while there is un-rendered HTML, create another PDF page and render with proper offset for the next page
170168
double scrollOffset = 0;
169+
//SL: if there is more than one page, increase the bottom margin to allow space for the page number
170+
container.MarginBottom += scrollOffset > -container.ActualSize.Height ? 20 : 0;
171+
container.PerformLayout(measure); //SL: This still does not increase the margin for the first page of a multi page.. welp
171172
while (scrollOffset > -container.ActualSize.Height)
172173
{
173174
var page = document.AddPage();
@@ -185,8 +186,14 @@ public static void AddPdfPages(PdfDocument document, string html, PdfGenerateCon
185186
scrollOffset -= pageSize.Height;
186187
}
187188

188-
// add web links and anchors
189-
HandleLinks(document, container, orgPageSize, pageSize);
189+
if (config.AddPageCountFoooter && document.PageCount > 1) //Only add page numbers if more than one page
190+
AddPageCountFoooter(document, pageSize);
191+
192+
// SL: Set config option to handle links or not as it crashes for
193+
// some valid html links.
194+
// TODO: Investigate reason for crashing.
195+
if (config.HandleLinks)
196+
HandleLinks(document, container, orgPageSize, pageSize);
190197
}
191198
}
192199
}
@@ -195,6 +202,37 @@ public static void AddPdfPages(PdfDocument document, string html, PdfGenerateCon
195202

196203
#region Private/Protected methods
197204

205+
/// <summary>
206+
/// Add a page count footer to the PDF
207+
/// </summary>
208+
/// <param name="document"></param>
209+
/// <param name="container"></param>
210+
/// <param name="orgPageSize"></param>
211+
/// <param name="pageSize"></param>
212+
private static void AddPageCountFoooter(PdfDocument document, XSize pageSize)
213+
{
214+
215+
// Make a font and a brush to draw the page counter.
216+
XFont font = new XFont("Helvetica", 8);
217+
XBrush brush = XBrushes.Black;
218+
219+
// Add the page counter.
220+
string noPages = document.Pages.Count.ToString();
221+
for (int i = 0; i < document.Pages.Count; ++i)
222+
{
223+
PdfPage page = document.Pages[i];
224+
// Make a layout rectangle.
225+
XRect layoutRectangle = new XRect(0/*X*/, page.Height - font.Height - 10/*Y*/, pageSize.Width/*Width*/, font.Height/*Height*/);
226+
XGraphics gfx = XGraphics.FromPdfPage(page);
227+
gfx.DrawString(
228+
"Page " + (i + 1).ToString() + " of " + noPages,
229+
font,
230+
brush,
231+
layoutRectangle,
232+
XStringFormats.CenterRight);
233+
}
234+
}
235+
198236
/// <summary>
199237
/// Handle HTML links by create PDF Documents link either to external URL or to another page in the document.
200238
/// </summary>
@@ -219,7 +257,7 @@ private static void HandleLinks(PdfDocument document, HtmlContainer container, X
219257
// document links to the same page as the link is not allowed
220258
int anchorPageIdx = (int)(anchorRect.Value.Top / pageSize.Height);
221259
if (i != anchorPageIdx)
222-
document.Pages[i].AddDocumentLink(new PdfRectangle(xRect), anchorPageIdx);
260+
document.Pages[i].AddDocumentLink(new PdfRectangle(xRect), anchorPageIdx);
223261
}
224262
}
225263
else

Source/HtmlRenderer.WPF/HtmlRenderer.WPF.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@
3333
<ErrorReport>prompt</ErrorReport>
3434
<WarningLevel>4</WarningLevel>
3535
</PropertyGroup>
36+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
37+
<DebugSymbols>true</DebugSymbols>
38+
<OutputPath>bin\x64\Debug\</OutputPath>
39+
<DefineConstants>DEBUG;TRACE</DefineConstants>
40+
<DebugType>full</DebugType>
41+
<PlatformTarget>x64</PlatformTarget>
42+
<ErrorReport>prompt</ErrorReport>
43+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
44+
</PropertyGroup>
45+
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
46+
<OutputPath>bin\x64\Release\</OutputPath>
47+
<DefineConstants>TRACE</DefineConstants>
48+
<Optimize>true</Optimize>
49+
<DebugType>pdbonly</DebugType>
50+
<PlatformTarget>x64</PlatformTarget>
51+
<ErrorReport>prompt</ErrorReport>
52+
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
53+
</PropertyGroup>
3654
<ItemGroup>
3755
<Reference Include="System" />
3856
<Reference Include="System.Data" />

0 commit comments

Comments
 (0)