MariGold.OpenXHTML.RtL 3.0.0

dotnet add package MariGold.OpenXHTML.RtL --version 3.0.0                
NuGet\Install-Package MariGold.OpenXHTML.RtL -Version 3.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MariGold.OpenXHTML.RtL" Version="3.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add MariGold.OpenXHTML.RtL --version 3.0.0                
#r "nuget: MariGold.OpenXHTML.RtL, 3.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install MariGold.OpenXHTML.RtL as a Cake Addin
#addin nuget:?package=MariGold.OpenXHTML.RtL&version=3.0.0

// Install MariGold.OpenXHTML.RtL as a Cake Tool
#tool nuget:?package=MariGold.OpenXHTML.RtL&version=3.0.0                

MariGold.OpenXHTML.RtL

Based on MariaGold.OpenXHTML v3.0.0.

MariGold.OpenXHTML

OpenXHTML is a wrapper library for Open XML SDK to convert HTML documents into Open XML word documents. It has simply encapsulated the complexity of Open XML yet exposes the properties of Open XML for manipulation.

Installing via NuGet

In Package Manager Console, enter the following command:

Install-Package MariGold.OpenXHTML

Usage

To create an empty Open XML word document using the OpenXHTML, use the following code.

using MariGold.OpenXHTML;

WordDocument doc = new WordDocument("sample.docx");
doc.Save();

To create an Open XML document from an HTML document, use the following code.

using MariGold.OpenXHTML;

WordDocument doc = new WordDocument("sample.docx");
doc.Process(new HtmlParser("<div>sample text</div>"));
doc.Save();

Once the HTML is processed, you can access the Open XML document using the following properties in WordDocument.

public WordprocessingDocument WordprocessingDocument { get; }
public MainDocumentPart MainDocumentPart { get; }
public Document Document { get; }

Any modifications on Open XML document should be done before the Save method. This has to be done since the Save method will write all the changes and unload the document from the memory. So any further modifications may result in an exception. For example, if you want to append a paragraph at the document body, try the following code.

using MariGold.OpenXHTML;
using DocumentFormat.OpenXml.Wordprocessing;

WordDocument doc = new WordDocument("sample.docx");
doc.Process(new HtmlParser("<div>sample text</div>"));
doc.Document.Body.AppendChild<Paragraph>(new Paragraph(new Run(new Text("added text"))));
doc.Save();

You can also create an Open XML document in memory. Following example illustrates how to save the document in a MemoryStream.

using (MemoryStream mem = new MemoryStream())
{
	WordDocument doc = new WordDocument(mem);
	doc.Save();
}		
Relative Images

OpenXHTML cannot process the images with relative URL. This can be solved using the ImagePath property to set the base address for every relative image paths. The image path can be either a URL or a physical folder address.

using MariGold.OpenXHTML;

WordDocument doc = new WordDocument("sample.docx");
doc.ImagePath = "http:\\abc.com";
doc.Process(new HtmlParser("<img src=\"sample.png\" />"));
doc.Save();

You can also assign any file URI address on image path.

doc.ImagePath = @"file:///C:/Img";
Base URL

Like relative images, an HTML document may also contain links with relative path. This can be resolved using the BaseURL property.

using MariGold.OpenXHTML;

WordDocument doc = new WordDocument("sample.docx");
doc.BaseURL = "http:\\abc.com";
doc.Process(new HtmlParser("<a href=\"index.htm\">sample</a>"));
doc.Save();

Also, if there are any relative images in the given html document and ImagePath is not assigned, OpenXHTML will attempt to use BaseURL to resolve relative image paths. So using BaseURL, you can resolve both relative image paths and links. The reason to create a seperate property for image path is that sometimes image location is different from base URL address.

Uri Schema

The protocol relative URLs can be resolved using the UriSchema property.

doc.UriSchema = Uri.UriSchemeHttp;
HTML Parsing

OpenXHTML has a built-in HTML and CSS parser (MariGold.HtmlParser) which can be complectly replaced with any external HTML and CSS parser. The Process method in WordDocument class expects an IParser interface type implementation to process the HTML and CSS. You can create an implementation of this IParser interface to parse the HTML and CSS.

public void Process(IParser parser);
interface IParser
{
	string BaseURL { get; set; }
	string UriSchema { get; set; }

	decimal CalculateRelativeChildFontSize(string parentFontSize, string childFontSize);
	IHtmlNode FindBodyOrFirstElement();
}

Here is the structure of IParser. The BaseURL and UriSchema are just two simple properties to store the base url address and uri schema for processing the HTML images and links. Both properties are used to resolve the protocol free and relative path of external style sheet URLs. The CalculateRelativeChildFontSize method is used to calculate the relative child font size. For example, in the below html, the font size of the h1 tag is 20 pixel.

<div style="font-size:16px"><h1>sample</h1></div>

If you don't want to re-implement this functionality, you can simply use the CSSUtility class in your implementation.

using MariGold.HtmlParser;

return CSSUtility.CalculateRelativeChildFontSize(parentFontSize, childFontSize);

The FindBodyOrFirstElement method is expected to return an IHtmlNode representation of html body tag and the hierarchy of its child elements. If the document does not have body element, then it is expected to return the first root element. All the CSS styles and HTML attributes of IHtmlNode must be resolved and filled in the respective properties.

References

Convert HTML to Word Document using CKEditor and MariGold.OpenXHTML

Windows Forms Application - Convert HTML Files To DOCX Files With MariGold.OpenXHTML

Implement Custom HTML Parser using AngleSharp

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.1 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 471 10/19/2022

Bug fixes on image width and height, Fixed the border line issues on table cell with vertical span, page-break-before 'always' implemented for block elements, DocumentFormat.OpenXml upgraded from 2.11.3 to 2.17.1, Upgraded to .NET Core 3.1