onizet / html2openxml

Html2OpenXml is a small .Net library that convert simple or advanced HTML to plain OpenXml components. This program has started in 2009, initially to convert user's comments from SharePoint to Word.
MIT License
297 stars 106 forks source link

Border width for tables is faulty #131

Open Martin56756 opened 1 year ago

Martin56756 commented 1 year ago

When parsing styled tables from HTML, the borders aren't displayed correctly, i.e. the borders are way too wide. This has 2 issues:

No. 1: In SideBorder.cs: If the rgb string contains spaces, it gets chopped up too and the colour isn't transferred into the docx.

Solution:

Just add this to the Parse method: // Remove the spaces that could appear in the color parameter: rgb(233, 233, 233) --> rgb(233,233,233) str = Regex.Replace(str, @",\s+?", ",");

No. 2: Border Width in OpenXML is expressed in 1/8 of a point instead of dxa. So 1px becomes 2.25pt somehow.

Solution:

Add this to the Unit struct: /// <summary> /// Gets the value expressed in 1/8 of a Point /// IMPORTANT: Use this for borders, as OpenXML expresses Border Width in 1/8 of points, /// with a minimum value of 2 (1/4 of a point) and a maximum value of 96 (12 points). /// </summary> public int ValueInEighthPoint { get { return ValueInPoint * 8; } }

And replace the border stylings in HtmlConverter.ProcessTag.cs as follows:

properties.TableCellBorders.LeftBorder = new LeftBorder { Val = border.Left.Style, Color = StringValue.FromString(border.Left.Color.ToHexString()), Size = (uint)border.Left.Width.ValueInDxa };

becomes

properties.TableCellBorders.LeftBorder = new LeftBorder { Val = border.Left.Style, Color = StringValue.FromString(border.Left.Color.ToHexString()), Size = (uint)border.Left.Width.ValueInEighthPoint };