![]() |
VOOZH | about |
dotnet add package MigraDocCore.Extensions.Html --version 1.0.2
NuGet\Install-Package MigraDocCore.Extensions.Html -Version 1.0.2
<PackageReference Include="MigraDocCore.Extensions.Html" Version="1.0.2" />
<PackageVersion Include="MigraDocCore.Extensions.Html" Version="1.0.2" />Directory.Packages.props
<PackageReference Include="MigraDocCore.Extensions.Html" />Project file
paket add MigraDocCore.Extensions.Html --version 1.0.2
#r "nuget: MigraDocCore.Extensions.Html, 1.0.2"
#:package MigraDocCore.Extensions.Html@1.0.2
#addin nuget:?package=MigraDocCore.Extensions.Html&version=1.0.2Install as a Cake Addin
#tool nuget:?package=MigraDocCore.Extensions.Html&version=1.0.2Install as a Cake Tool
Extensions for MigraDocCore/PDFSharpCore. This project was ported from the hivvetech/migradoc-extensions project. It also adds support for HTML and Markdown in header or footer sections.
The biggest feature provided by this library is the ability to convert from HTML and Markdown to PDF, via MigraDocCore's Document Object Model.
MigraDocCore.Extensions makes use of Markdig (replacing MarkdownSharp in the original library) to convert from Markdown to HTML and the Html Agility Pack to convert from HTML to PDF.
Since the MigraDoc DOM is pretty basic, much of the conversion involves setting the Style of generated MigraDoc Paragraph instances. You can then configure these styles however you like. See the example project for more details.
Import the MigraDocCore.Extensions.Markdown namespace and call AddMarkdown on a MigraDoc Section, HeaderFooter, Cell, or Paragraph instance:
var markdown = @"
# This is a heading
This is some **bold** ass text with a [link](https://www.example.com).
- List Item 1
- List Item 2
- List Item 3
Pretty cool huh?
";
section.AddMarkdown(markdown);
Import the MigraDocCore.Extensions.Html namespace and call AddHtml on a MigraDoc Section, HeaderFooter, Cell, or Paragraph instance:
var html = @"
<h1>This is a heading</h1>
<p>This is some **bold** ass text with a <a href='https://www.example.com'>link</a>.<p>
<ul>
<li>List Item 1</li>
<li>List Item 2</li>
<li>List Item 3</li>
</ul>
<p>Pretty cool huh?</p>
";
section.AddHtml(html);
The HTML converter currently supports the following:
<strong>, <em>, <i>, <u>For more details, check out the specs.
To add a custom handler, create a new instance of HtmlConverter and add to its NodeHandlers dictionary. The key is the HTML element you wish to handle and the value is a Func<HtmlNode, DocumentObject, DocumentObject>.
The DocumentObject instance passed to the handler is the parent object in the MigraDoc DOM, usually a Section or Paragraph (you may need to cater for both). The return value should be the DocumentObject that was created. This will be passed as the parent for any child elements.
Here is the handler for processing a <strong> element:
nodeHandlers.Add("strong", (node, parent) => {
var format = TextFormat.Bold;
var formattedText = parent as FormattedText;
if (formattedText != null)
{
return formattedText.Format(format);
}
// otherwise parent is paragraph or section
return GetParagraph(parent).AddFormattedText(format);
});
In the above handler, we need to cater for nested format tags (e.g. <strong><em>some text</em></strong>) so we first attempt to cast the parent as FormattedText, otherwise fall back to adding formatted text to a Paragraph. Unfortunately such type checks are fairly frequent due to the limited relationships between objects in the MigraDoc DOM.
To use a custom converter instance use the Section.Add(string content, IConverter converter) extension in the MigraDocCore.Extensions namespace.
Note that an element handler should not process any inner HTML. For example the handler for a <h1> tag only adds a paragraph with a the style "Heading1", it does not add the text (there is a separate handler for processing text nodes).
As of PdfSharpCore v1.3.62 there appears to be a bug where multiple paragraphs in footer sections are written in the same position, overlapping each other. This extension works around the bug by using a single paragraph in footer sections, adding newlines as needed. In practice, this means some paragraph-level formatting may not be fully supported in footers (e.g. headings, lists).
I have opened an issue in the PdfSharpCore repo to track the progress of this bug.
Licensed under the MIT License.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 net5.0 was computed. net5.0-windows net5.0-windows was computed. net6.0 net6.0 was computed. net6.0-android net6.0-android was computed. net6.0-ios net6.0-ios was computed. net6.0-maccatalyst net6.0-maccatalyst was computed. net6.0-macos net6.0-macos was computed. net6.0-tvos net6.0-tvos was computed. net6.0-windows net6.0-windows was computed. net7.0 net7.0 was computed. net7.0-android net7.0-android was computed. net7.0-ios net7.0-ios was computed. net7.0-maccatalyst net7.0-maccatalyst was computed. net7.0-macos net7.0-macos was computed. net7.0-tvos net7.0-tvos was computed. net7.0-windows net7.0-windows was computed. net8.0 net8.0 was computed. net8.0-android net8.0-android was computed. net8.0-browser net8.0-browser was computed. net8.0-ios net8.0-ios was computed. net8.0-maccatalyst net8.0-maccatalyst was computed. net8.0-macos net8.0-macos was computed. net8.0-tvos net8.0-tvos was computed. net8.0-windows net8.0-windows was computed. net9.0 net9.0 was computed. net9.0-android net9.0-android was computed. net9.0-browser net9.0-browser was computed. net9.0-ios net9.0-ios was computed. net9.0-maccatalyst net9.0-maccatalyst was computed. net9.0-macos net9.0-macos was computed. net9.0-tvos net9.0-tvos was computed. net9.0-windows net9.0-windows was computed. net10.0 net10.0 was computed. net10.0-android net10.0-android was computed. net10.0-browser net10.0-browser was computed. net10.0-ios net10.0-ios was computed. net10.0-maccatalyst net10.0-maccatalyst was computed. net10.0-macos net10.0-macos was computed. net10.0-tvos net10.0-tvos was computed. net10.0-windows net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 netcoreapp2.0 was computed. netcoreapp2.1 netcoreapp2.1 was computed. netcoreapp2.2 netcoreapp2.2 was computed. netcoreapp3.0 netcoreapp3.0 was computed. netcoreapp3.1 netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 netstandard2.0 is compatible. netstandard2.1 netstandard2.1 was computed. |
| .NET Framework | net461 net461 was computed. net462 net462 was computed. net463 net463 was computed. net47 net47 was computed. net471 net471 was computed. net472 net472 was computed. net48 net48 was computed. net481 net481 was computed. |
| MonoAndroid | monoandroid monoandroid was computed. |
| MonoMac | monomac monomac was computed. |
| MonoTouch | monotouch monotouch was computed. |
| Tizen | tizen40 tizen40 was computed. tizen60 tizen60 was computed. |
| Xamarin.iOS | xamarinios xamarinios was computed. |
| Xamarin.Mac | xamarinmac xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos xamarinwatchos was computed. |
Showing the top 1 NuGet packages that depend on MigraDocCore.Extensions.Html:
| Package | Downloads |
|---|---|
|
MigraDocCore.Extensions.Markdown
Adds support for Markdown in MigraDocCore documents using Markdig, ported from the MigraDoc.Extensions library by Vertigo Ventures. |
This package is not used by any popular GitHub repositories.