This sample shows how to convert from HTML to RTF, RTF to HTML, XAML to HTML and HTML to XAML.
For a full detailed write up on how this works see Converting between RTF and HTML on the Code Gallery.
This sample shows how to convert from HTML to RTF, RTF to HTML, XAML to HTML and HTML to XAML.
License: Apache License 2.0
This sample shows how to convert from HTML to RTF, RTF to HTML, XAML to HTML and HTML to XAML.
For a full detailed write up on how this works see Converting between RTF and HTML on the Code Gallery.
The HtmlToXamlConveter miss handles the following html fragment
#-- Html Fragment ---
<P style="TEXT-ALIGN: left"><SPAN style="COLOR: black"><SPAN
style="BACKGROUND-COLOR: window"><SPAN
style="FONT-SIZE: 9pt; FONT-FAMILY: Segoe UI">The </SPAN><SPAN
style="COLOR: red">gospel </SPAN><SPAN style="COLOR: black">was </SPAN><SPAN
style="COLOR: blue">heard </SPAN><SPAN style="COLOR: black">by the </SPAN><SPAN
style="COLOR: seagreen">saints</SPAN></SPAN></SPAN></P>
--- end Html fragment ---
Note: the <SPAN style="BACKGROUND-COLOR: window">
This is miss handled to the following.
XamlText = HtmlToXamlConverter.ConvertHtmlToXaml(htmlFragment, false);
#--- XamlText result ---
<Section xml:space="preserve" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><Paragraph TextAlignment="left"><Span Foreground="black"><Span Background="black"><Run FontSize="9pt" FontFamily="segoe ui">The </Run><Run Foreground="red">gospel </Run><Run Foreground="black">was </Run><Run Foreground="blue">heard </Run><Run Foreground="black">by the </Run><Run Foreground="seagreen">saints</Run></Span></Span></Paragraph></Section>
Note the <Span Foreground="black">
style="BACKGROUND-COLOR: window"
This currently doesnt support images, any plan to add image support?
Hi,
Just wanted to check that the code here is Apache Licensed ok? The link to msdn says so, but don't want to presume.
Many thanks,
Chris
I have an WPF application in which i am using the classes (htmlLexicalAnalyzer.cs, HtmlToXAMLConverter, etc.) from the sample application HtmlToXAMLDemo (the main repo).
I have changed the namespace of that classes for mine only.
Then in my physical pc is working correctly (no errors nor exceptions).
Then I create an MSI and deploy my WPF application to a new clean and fresh pc (a virtual machine). After installation I execute it and I get the below exception:
System.OverflowException: Arithmetic operation resulted in an overflow.
at myApp.Converters.HtmlConverter.HtmlLexicalAnalyzer.ReadLookAheadCharacter()
at myApp.Converters.HtmlConverter.HtmlLexicalAnalyzer.GetNextCharacter()
at myApp.Converters.HtmlConverter.HtmlLexicalAnalyzer.GetNextTagToken()
at myApp.Converters.HtmlConverter.HtmlParser.ParseHtmlContent()
at myApp.Converters.HtmlConverter.HtmlToXamlConverter.ConvertHtmlToXaml(String htmlString, Boolean asFlowDocument)
at myApp.UI.HtmlRichTextBoxBehavior.OnValueChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
It looks like there is something wrong in ReadLookAheadCharacter that is causing this exception in some particular scenario like mine.
Please, could you have a look at it and fix it? Thanks in advance.
HtmlToRtfConverter.ConvertHtmlToRtf removes characters like:
Hello,
This project is very good & very useful to me for my WPF application.
Perhaps, if I am using a "
" tag with "style='color: rgb(255,90,40);' then it is not getting color and set black or gray color.
I have debugged into it and found the issue in "MarkupConverter/htmlcssparser.cs ". There is a function of ParseWhiteSpace in which color is converting but I can't understand why there is :
else if (styleValue.Substring(nextIndex, 3).ToLower() == "rbg")
{
// Implement real rgb() color parsing
while (nextIndex < styleValue.Length && styleValue[nextIndex] != ')')
{
nextIndex++;
}
if (nextIndex < styleValue.Length)
{
nextIndex++; // to skip ')'
}
color = "gray"; // return bogus color
}
In this "rbg" is by mistake or some other reason (it should be rgb on rbg) - I don't know.
Anyway, I have resolved it by adding following code:
//Added by Sachin for converting rgb color to hex and return
else if (styleValue.Substring(nextIndex, 3).ToLower() == "rgb")
{
// Implement real rgb() color parsing
startIndex = 4;
string temp_color = styleValue.Substring(startIndex, styleValue.Length - 1 - startIndex);
string[] rgb = temp_color.Split(',');
color = "#" + (byte.Parse(rgb[0])).ToString("X2") + (byte.Parse(rgb[1])).ToString("X2") + (byte.Parse(rgb[2])).ToString("X2");
}
Thanks,
Sachin
Whenever i enter a bulleted list in rtf it crashes.
Unrecognized structure in data format 'Rich Text Format'.
Parameter name: stream
Function: ConvertRtfToXaml
Code line: textRange.Load(rtfMemoryStream, DataFormats.Rtf)
The ParseXamlColor routine assumes the input string is in the format #AARRGGBB. In an operational scenario, this has been demonstrated to not always be true, and when the value is a 6-digit string (#000000) this converts the value to #0000 which is not valid.
private static string ParseXamlColor(string color) { if (color.StartsWith("#")) { // Remove transparancy value color = "#" + color.Substring(3); } return color; }
Hello,
I know this projct is not really up-to-date, but I've just started looking for a HTML to RTF converter, and found yours. Awesome job! Almost as good as Sautin's dll, but there's a significant problem. I can't set column widths in tables, or width of tables.
Example HTML:
<!DOCTYPE html>
<html>
<head>
<title>Table example</title>
<meta http-equiv=Content-Type content="text/html; charset=unicode">
<style>
body {
font-family: "Bookman Old Style", serif;
font-weight: 200;
font-size: 16px;
margin: 0px;
table {
width: 100%;
}
</style>
</head>
<body>
<table style="width:100%">
<!-- <colgroup>
<col width="50%" />
<col width="50%" />
</colgroup>-->
<tr>
<td style="width:50%">First cell</td>
<td style="width:50%">Second cell, a bit longer</td>
</tr>
</table>
</body>
</html>
At colgroup
it crashes, and style="width:50%"
is ignored.
This is the only thing I miss, otherwise it would be a perfect alternative of Sautin's one. I hope anyone is still caring about this cool project.
Any help would be appreciated.
Thanks,
dobragab
I am xaml beginner. So i dont know how to run these. Please guide me.
Hi Sir,
Can you help to how to convert RTF to XML ?
please i am stuck here ...
https://stackoverflow.com/questions/49005971/vb-net-rtf-to-required-xml-format
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.