Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Source/Demo/Common/HtmlRenderer.Demo.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down
10 changes: 5 additions & 5 deletions Source/Demo/Common/Samples/02.Text.htm
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
<head>
<title>Text</title>
<link rel="Stylesheet" href="StyleSheet" />
<style type="text/css">
::selection {
color: #bb5555;
background-color: #aaddaa;
}
<style type="text/css">
::selection {
color: #bb5555;
background-color: #aaddaa;
}
</style>
</head>
<body>
Expand Down
14 changes: 7 additions & 7 deletions Source/Demo/Common/TestSamples/12.Text.htm
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<head>
<title>Text</title>
<link rel="Stylesheet" href="StyleSheet" />
<style>
.right {
text-align: right;
border-width: 1px;
border-style: solid;
border-color: blue;
}
<style>
.right {
text-align: right;
border-width: 1px;
border-style: solid;
border-color: blue;
}
</style>
</head>
<body>
Expand Down
34 changes: 17 additions & 17 deletions Source/Demo/Common/TestSamples/22.RTL.htm
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<html>
<body>
<div dir="rtl">
<div>שלום עולם, יש <b>ברבורים </b>בעגם הזה</div>
<br />
<div>שלום עולם, יש ברבורים בעגם הזה</div>
<div>שלום עולם, יש ברבורים בעגם הזה</div>
</div>
<hr />
<div dir="rtl">
<div>שלום עולם,<span>hello world</span> יש ברבורים בעגם הזה</div>
<br />
<div>שלום עולם, יש ברבורים בעגם הזה</div>
<div>שלום עולם, יש ברבורים בעגם הזה</div>
</div>
<hr />
</body>
<html>
<body>
<div dir="rtl">
<div>שלום עולם, יש <b>ברבורים </b>בעגם הזה</div>
<br />
<div>שלום עולם, יש ברבורים בעגם הזה</div>
<div>שלום עולם, יש ברבורים בעגם הזה</div>
</div>
<hr />
<div dir="rtl">
<div>שלום עולם,<span>hello world</span> יש ברבורים בעגם הזה</div>
<br />
<div>שלום עולם, יש ברבורים בעגם הזה</div>
<div>שלום עולם, יש ברבורים בעגם הזה</div>
</div>
<hr />
</body>
</html>
18 changes: 18 additions & 0 deletions Source/Demo/WPF/HtmlRenderer.Demo.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@
<PropertyGroup>
<ApplicationIcon>html.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down
18 changes: 18 additions & 0 deletions Source/Demo/WinForms/HtmlRenderer.Demo.WinForms.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@
<PropertyGroup>
<ApplicationIcon>html.ico</ApplicationIcon>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="PdfSharp, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>..\..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.dll</HintPath>
Expand Down
52 changes: 45 additions & 7 deletions Source/HtmlRenderer.PdfSharp/HtmlRenderer.PdfSharp.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{CA249F5D-9285-40A6-B217-5889EF79FD7E}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>TheArtOfDev.HtmlRenderer.PdfSharp</RootNamespace>
<RootNamespace>HtmlRenderer.PdfSharp</RootNamespace>
<AssemblyName>HtmlRenderer.PdfSharp</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<TargetFrameworkProfile />
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
</PropertyGroup>
Expand All @@ -31,13 +32,44 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<OutputPath>bin\x86\Release\</OutputPath>
<PlatformTarget>x86</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="PdfSharp, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.dll</HintPath>
<HintPath>..\..\..\..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="PdfSharp.Charting, Version=1.50.4000.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<HintPath>..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.Charting.dll</HintPath>
<HintPath>..\..\..\..\packages\PDFsharp.1.50.4000-beta3b\lib\net20\PdfSharp.Charting.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand All @@ -55,20 +87,26 @@
<Compile Include="Adapters\GraphicsAdapter.cs" />
<Compile Include="Adapters\GraphicsPathAdapter.cs" />
<Compile Include="Adapters\ImageAdapter.cs" />
<Compile Include="Adapters\PdfSharpAdapter.cs" />
<Compile Include="Adapters\PenAdapter.cs" />
<Compile Include="Adapters\XTextureBrush.cs" />
<Compile Include="PdfGenerateConfig.cs" />
<Compile Include="HtmlContainer.cs" />
<Compile Include="PdfGenerateConfig.cs" />
<Compile Include="PdfGenerator.cs" />
<Compile Include="Adapters\PdfSharpAdapter.cs" />
<Compile Include="Utilities\Utils.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HtmlRenderer\HtmlRenderer.csproj">
<Project>{fe611685-391f-4e3e-b27e-d3150e51e49b}</Project>
<Project>{FE611685-391F-4E3E-B27E-D3150E51E49B}</Project>
<Name>HtmlRenderer</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Reference Include="System">
<HintPath>C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions Source/HtmlRenderer.PdfSharp/PdfGenerateConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ public int MarginRight
}
}

/// <summary>
/// Sets weather to Handle HTML links by create PDF
/// Documents link either to external URL or to another page in the document
/// </summary>
public bool HandleLinks { get; set; }

/// <summary>
/// Add a page count footer to the PDF
/// </summary>
public bool AddPageCountFoooter { get; set; }



/// <summary>
/// Set all 4 margins to the given value.
/// </summary>
Expand Down
56 changes: 47 additions & 9 deletions Source/HtmlRenderer.PdfSharp/PdfGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,21 @@ public static void AddPdfPages(PdfDocument document, string html, PdfGenerateCon
container.Location = new XPoint(config.MarginLeft, config.MarginTop);
container.MaxSize = new XSize(pageSize.Width, 0);
container.SetHtml(html, cssData);
container.PageSize = pageSize;
container.MarginBottom = config.MarginBottom;
container.PageSize = pageSize;
container.MarginBottom = config.MarginBottom;
container.MarginLeft = config.MarginLeft;
container.MarginRight = config.MarginRight;
container.MarginTop = config.MarginTop;

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

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

// add web links and anchors
HandleLinks(document, container, orgPageSize, pageSize);
if (config.AddPageCountFoooter && document.PageCount > 1) //Only add page numbers if more than one page
AddPageCountFoooter(document, pageSize);

// SL: Set config option to handle links or not as it crashes for
// some valid html links.
// TODO: Investigate reason for crashing.
if (config.HandleLinks)
HandleLinks(document, container, orgPageSize, pageSize);
}
}
}
Expand All @@ -195,6 +202,37 @@ public static void AddPdfPages(PdfDocument document, string html, PdfGenerateCon

#region Private/Protected methods

/// <summary>
/// Add a page count footer to the PDF
/// </summary>
/// <param name="document"></param>
/// <param name="container"></param>
/// <param name="orgPageSize"></param>
/// <param name="pageSize"></param>
private static void AddPageCountFoooter(PdfDocument document, XSize pageSize)
{

// Make a font and a brush to draw the page counter.
XFont font = new XFont("Helvetica", 8);
XBrush brush = XBrushes.Black;

// Add the page counter.
string noPages = document.Pages.Count.ToString();
for (int i = 0; i < document.Pages.Count; ++i)
{
PdfPage page = document.Pages[i];
// Make a layout rectangle.
XRect layoutRectangle = new XRect(0/*X*/, page.Height - font.Height - 10/*Y*/, pageSize.Width/*Width*/, font.Height/*Height*/);
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(
"Page " + (i + 1).ToString() + " of " + noPages,
font,
brush,
layoutRectangle,
XStringFormats.CenterRight);
}
}

/// <summary>
/// Handle HTML links by create PDF Documents link either to external URL or to another page in the document.
/// </summary>
Expand All @@ -219,7 +257,7 @@ private static void HandleLinks(PdfDocument document, HtmlContainer container, X
// document links to the same page as the link is not allowed
int anchorPageIdx = (int)(anchorRect.Value.Top / pageSize.Height);
if (i != anchorPageIdx)
document.Pages[i].AddDocumentLink(new PdfRectangle(xRect), anchorPageIdx);
document.Pages[i].AddDocumentLink(new PdfRectangle(xRect), anchorPageIdx);
}
}
else
Expand Down
18 changes: 18 additions & 0 deletions Source/HtmlRenderer.WPF/HtmlRenderer.WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
Expand Down
Loading