Skip to content
Open
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
28 changes: 28 additions & 0 deletions GlobalAssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MigraDoc.Extensions")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.1.0")]
[assembly: AssemblyFileVersion("1.0.1.0")]

//will set the "ProductVersion" value displayed by Windows Explorer
[assembly: AssemblyInformationalVersion("1.0.1.0")]
5 changes: 5 additions & 0 deletions MigraDoc.Extensions.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MigraDoc.Extensions.Markdow
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MigraDoc.Extensions", "src\MigraDoc.Extensions\MigraDoc.Extensions.csproj", "{1863068F-975C-4F5B-ABA7-FBA344709A48}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solutions Items", "Solutions Items", "{199219F5-41E8-45D6-A267-E38242F50253}"
ProjectSection(SolutionItems) = preProject
GlobalAssemblyInfo.cs = GlobalAssemblyInfo.cs
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

Extensions for [MigraDoc/PDFSharp](http://www.pdfsharp.net/Overview.ashx).

## Remark
The tests are not updated to include the new support for html nested lists!
Use on your own risk!

The included example project is updated to have different example of nested lists.

## Quick Start

The biggest feature provided by this library is the ability to convert from HTML and Markdown to PDF, via MigraDoc's Document Object Model.
Expand Down
98 changes: 76 additions & 22 deletions src/MigraDoc.Extensions.Html/HtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,18 @@ namespace MigraDoc.Extensions.Html
{
public class HtmlConverter : IConverter
{
private IDictionary<string, Func<HtmlNode, DocumentObject, DocumentObject>> nodeHandlers
= new Dictionary<string, Func<HtmlNode, DocumentObject, DocumentObject>>();
private IDictionary<string, Func<HtmlNode, DocumentObject, DocumentObject, DocumentObject>> nodeHandlers
= new Dictionary<string, Func<HtmlNode, DocumentObject, DocumentObject, DocumentObject>>();

public HtmlConverter()
private readonly double _nestedListStartingLeftIndent = 1.0;

public HtmlConverter(double nestedListStartingLeftIndent)
{
_nestedListStartingLeftIndent = nestedListStartingLeftIndent;
AddDefaultNodeHandlers();
}

public IDictionary<string, Func<HtmlNode, DocumentObject, DocumentObject>> NodeHandlers
public IDictionary<string, Func<HtmlNode, DocumentObject, DocumentObject, DocumentObject>> NodeHandlers
{
get
{
Expand Down Expand Up @@ -51,11 +54,11 @@ private void ConvertHtmlNodes(HtmlNodeCollection nodes, DocumentObject section,
{
foreach (var node in nodes)
{
Func<HtmlNode, DocumentObject, DocumentObject> nodeHandler;
Func<HtmlNode, DocumentObject, DocumentObject, DocumentObject> nodeHandler;
if (nodeHandlers.TryGetValue(node.Name, out nodeHandler))
{
// pass the current container or section
var result = nodeHandler(node, current ?? section);
var result = nodeHandler(node, current ?? section, section);

if (node.HasChildNodes)
{
Expand Down Expand Up @@ -84,23 +87,24 @@ private void AddDefaultNodeHandlers()
nodeHandlers.Add("h5", AddHeading);
nodeHandlers.Add("h6", AddHeading);

nodeHandlers.Add("p", (node, parent) =>
nodeHandlers.Add("p", (node, parent, parentSection) =>
{
return ((Section)parent).AddParagraph();
});

// Inline Elements

nodeHandlers.Add("strong", (node, parent) => AddFormattedText(node, parent, TextFormat.Bold));
nodeHandlers.Add("i", (node, parent) => AddFormattedText(node, parent, TextFormat.Italic));
nodeHandlers.Add("em", (node, parent) => AddFormattedText(node, parent, TextFormat.Italic));
nodeHandlers.Add("u", (node, parent) => AddFormattedText(node, parent, TextFormat.Underline));
nodeHandlers.Add("a", (node, parent) =>
nodeHandlers.Add("strong", (node, parent, parentSection) => AddFormattedText(node, parent, TextFormat.Bold));
nodeHandlers.Add("i", (node, parent, parentSection) => AddFormattedText(node, parent, TextFormat.Italic));
nodeHandlers.Add("em", (node, parent, parentSection) => AddFormattedText(node, parent, TextFormat.Italic));
nodeHandlers.Add("u", (node, parent, parentSection) => AddFormattedText(node, parent, TextFormat.Underline));
nodeHandlers.Add("a", (node, parent, parentSection) =>
{
return GetParagraph(parent).AddHyperlink(node.GetAttributeValue("href", ""), HyperlinkType.Web);
});
nodeHandlers.Add("hr", (node, parent) => GetParagraph(parent).SetStyle("HorizontalRule"));
nodeHandlers.Add("br", (node, parent) => {
nodeHandlers.Add("hr", (node, parent, parentSection) => GetParagraph(parent).SetStyle("HorizontalRule"));
nodeHandlers.Add("br", (node, parent, parentSection) =>
{
if (parent is FormattedText)
{
// inline elements can contain line breaks
Expand All @@ -113,13 +117,28 @@ private void AddDefaultNodeHandlers()
return paragraph;
});

nodeHandlers.Add("li", (node, parent) =>
nodeHandlers.Add("li", (node, parent, parentSection) =>
{
var listStyle = node.ParentNode.Name == "ul"
? "UnorderedList"
: "OrderedList";
? "UnorderedList{0}"
: "OrderedList{0}";

var section = parent as Section;
int nestedLevelSameType = 1;
int nestedLevel = 1;
// if section == null then nested lists
if (section == null)
{
section = (Section) parentSection;

// get the nested level for the parent type
nestedLevelSameType = (node.Ancestors().Count(a => a.Name.Equals(node.ParentNode.Name))) + 1;
// get the nested level for all the types (ol or ul)
nestedLevel = (node.Ancestors().Count(a => a.Name.Equals("ol") || a.Name.Equals("ul"))) + 1;
// limit the levels to 3 because no more possible in MigraDoc
if (nestedLevelSameType > 3) nestedLevelSameType = 3;
}

var section = (Section)parent;
var isFirst = node.ParentNode.Elements("li").First() == node;
var isLast = node.ParentNode.Elements("li").Last() == node;

Expand All @@ -129,7 +148,13 @@ private void AddDefaultNodeHandlers()
section.AddParagraph().SetStyle("ListStart");
}

var listItem = section.AddParagraph().SetStyle(listStyle);
Paragraph listItem = section.AddParagraph().SetStyle(string.Format(listStyle, nestedLevelSameType));
// adapt indent if nested lists
if (nestedLevel > 0)
{
double leftIndent = _nestedListStartingLeftIndent + System.Convert.ToDouble(nestedLevel) / 2;
listItem.Format.LeftIndent = new Unit(leftIndent, UnitType.Centimeter);
}

// disable continuation if this is the first list item
listItem.Format.ListInfo.ContinuePreviousList = !isFirst;
Expand All @@ -139,11 +164,40 @@ private void AddDefaultNodeHandlers()
{
section.AddParagraph().SetStyle("ListEnd");
}

return listItem;
});

nodeHandlers.Add("#text", (node, parent) =>
//nodeHandlers.Add("li", (node, parent) =>
//{
// var listStyle = node.ParentNode.Name == "ul"
// ? "UnorderedList"
// : "OrderedList";

// var section = (Section)parent;
// var isFirst = node.ParentNode.Elements("li").First() == node;
// var isLast = node.ParentNode.Elements("li").Last() == node;

// // if this is the first item add the ListStart paragraph
// if (isFirst)
// {
// section.AddParagraph().SetStyle("ListStart");
// }

// var listItem = section.AddParagraph().SetStyle(listStyle);

// // disable continuation if this is the first list item
// listItem.Format.ListInfo.ContinuePreviousList = !isFirst;

// // if the this is the last item add the ListEnd paragraph
// if (isLast)
// {
// section.AddParagraph().SetStyle("ListEnd");
// }

// return listItem;
//});

nodeHandlers.Add("#text", (node, parent, parentSection) =>
{
// remove line breaks
var innerText = node.InnerText.Replace("\r", "").Replace("\n", "");
Expand Down Expand Up @@ -184,7 +238,7 @@ private static DocumentObject AddFormattedText(HtmlNode node, DocumentObject par
return GetParagraph(parent).AddFormattedText(format);
}

private static DocumentObject AddHeading(HtmlNode node, DocumentObject parent)
private static DocumentObject AddHeading(HtmlNode node, DocumentObject parent, DocumentObject parentSection = null)
{
return ((Section)parent).AddParagraph().SetStyle("Heading" + node.Name[1]);
}
Expand Down
28 changes: 18 additions & 10 deletions src/MigraDoc.Extensions.Html/MigraDoc.Extensions.Html.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,25 @@
<Reference Include="HtmlAgilityPack">
<HintPath>..\..\packages\HtmlAgilityPack.1.4.6\lib\Net40\HtmlAgilityPack.dll</HintPath>
</Reference>
<Reference Include="MigraDoc.DocumentObjectModel">
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.3879.0\lib\net20\MigraDoc.DocumentObjectModel.dll</HintPath>
<Reference Include="MigraDoc.DocumentObjectModel, Version=1.32.4334.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\MigraDoc.DocumentObjectModel.dll</HintPath>
</Reference>
<Reference Include="MigraDoc.Rendering">
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.3879.0\lib\net20\MigraDoc.Rendering.dll</HintPath>
<Reference Include="MigraDoc.Rendering, Version=1.32.4334.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\MigraDoc.Rendering.dll</HintPath>
</Reference>
<Reference Include="MigraDoc.RtfRendering">
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.3879.0\lib\net20\MigraDoc.RtfRendering.dll</HintPath>
<Reference Include="MigraDoc.RtfRendering, Version=1.32.4334.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\MigraDoc.RtfRendering.dll</HintPath>
</Reference>
<Reference Include="PdfSharp">
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.3879.0\lib\net20\PdfSharp.dll</HintPath>
<Reference Include="PdfSharp, Version=1.32.3057.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\PdfSharp.dll</HintPath>
</Reference>
<Reference Include="PdfSharp.Charting">
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.3879.0\lib\net20\PdfSharp.Charting.dll</HintPath>
<Reference Include="PdfSharp.Charting, Version=1.32.3057.0, Culture=neutral, PublicKeyToken=f94615aa0424f9eb, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\PdfSharp.Charting.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
Expand All @@ -61,6 +66,9 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\GlobalAssemblyInfo.cs">
<Link>GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="HtmlConverter.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SectionExtensions.cs" />
Expand Down
19 changes: 0 additions & 19 deletions src/MigraDoc.Extensions.Html/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
Expand All @@ -8,11 +7,6 @@
[assembly: AssemblyTitle("MigraDoc.Extensions.Html")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MigraDoc.Extensions.Html")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
Expand All @@ -21,16 +15,3 @@

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("666a8f9c-11cc-41b6-a709-57a5beb2df69")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
6 changes: 2 additions & 4 deletions src/MigraDoc.Extensions.Html/SectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using MigraDoc.DocumentObjectModel;
using MigraDoc.Extensions.Html;
using System;

namespace MigraDoc.Extensions.Html
{
public static class SectionExtensions
{
public static Section AddHtml(this Section section, string html)
public static Section AddHtml(this Section section, string html, double nestedListStartingLeftIndent = 1.0)
{
return section.Add(html, new HtmlConverter());
return section.Add(html, new HtmlConverter(nestedListStartingLeftIndent));
}
}
}
2 changes: 1 addition & 1 deletion src/MigraDoc.Extensions.Html/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="HtmlAgilityPack" version="1.4.6" targetFramework="net40" />
<package id="PDFsharp-MigraDoc-GDI" version="1.32.3879.0" targetFramework="net45" />
<package id="PDFsharp-MigraDoc-GDI" version="1.32.4334.0" targetFramework="net40" />
</packages>
17 changes: 10 additions & 7 deletions src/MigraDoc.Extensions.Markdown/MarkdownConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,35 @@ namespace MigraDoc.Extensions.Markdown
{
public class MarkdownConverter : IConverter
{
private readonly MarkdownOptions options;
private readonly MarkdownOptions _options;
private readonly double _nestedListStartingLeftIndent = 1.0;

public MarkdownConverter()
public MarkdownConverter(double nestedListStartingLeftIndent)
{
options = new MarkdownOptions
_options = new MarkdownOptions
{
LinkEmails = true
};
_nestedListStartingLeftIndent = nestedListStartingLeftIndent;
}

public MarkdownConverter(MarkdownOptions options)
public MarkdownConverter(MarkdownOptions options, double nestedListStartingLeftIndent)
{
if (options == null)
{
throw new ArgumentNullException("options");
}

this.options = options;
this._options = options;
_nestedListStartingLeftIndent = nestedListStartingLeftIndent;
}

public Action<Section> Convert(string contents)
{
var converter = new MarkdownSharp.Markdown(options);
var converter = new MarkdownSharp.Markdown(_options);
var html = converter.Transform(contents);

var htmlConverter = new HtmlConverter();
var htmlConverter = new HtmlConverter(_nestedListStartingLeftIndent);
return htmlConverter.Convert(html);
}
}
Expand Down
Loading