Skip to content

A couple of fixes for edge cases of converting docx to html #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: vNext
Choose a base branch
from
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
4 changes: 2 additions & 2 deletions OpenXmlPowerTools/FormattingAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2014,8 +2014,8 @@ private static IEnumerable<XElement> ParaStyleParaPropsStack(XDocument stylesXDo
while (localParaStyleName != null)
{
XElement paraStyle = stylesXDoc.Root.Elements(W.style).FirstOrDefault(s =>
s.Attribute(W.type).Value == "paragraph" &&
s.Attribute(W.styleId).Value == localParaStyleName);
s.Attribute(W.type)?.Value == "paragraph" &&
s.Attribute(W.styleId)?.Value == localParaStyleName);
if (paraStyle == null)
{
yield break;
Expand Down
18 changes: 15 additions & 3 deletions OpenXmlPowerTools/WmlToHtmlConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;

Expand Down Expand Up @@ -1520,9 +1521,20 @@ private static Dictionary<string, string> DefineRunStyle(XElement run)
private static decimal? GetFontSize(string languageType, XElement rPr)
{
if (rPr == null) return null;
return languageType == "bidi"
? (decimal?) rPr.Elements(W.szCs).Attributes(W.val).FirstOrDefault()
: (decimal?) rPr.Elements(W.sz).Attributes(W.val).FirstOrDefault();
var strVal = languageType == "bidi"
? rPr.Elements(W.szCs).Attributes(W.val).FirstOrDefault()?.Value
: rPr.Elements(W.sz).Attributes(W.val).FirstOrDefault()?.Value;

if (string.IsNullOrEmpty(strVal))
{
return null;
}

//if size is set as pt we should multiply it to compensate future division by 2
var multiplicator = strVal.ToLower().Contains("pt") ? 2m : 1m;

strVal = Regex.Replace(strVal, @"[^\d\.,]", "").Replace(",", ".");
return decimal.TryParse(strVal, NumberStyles.Any, CultureInfo.InvariantCulture, out var parsed) ? (decimal?) multiplicator * parsed : null;
}

private static void DetermineRunMarks(XElement run, XElement rPr, Dictionary<string, string> style, out XEntity runStartMark, out XEntity runEndMark)
Expand Down