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 into templated Word.
MIT License
321 stars 109 forks source link

Bugfix for Cell Width and Row heigth #60

Closed atmike closed 3 months ago

atmike commented 5 years ago

Hi Olivier here is the new request only for the bugfix for the height and width problem. Both Problems can be tested with the following snippet =>

<td colspan="2" style="width:260.55pt;padding:0in 5.4pt 0in 5.4pt;height:16.0pt;" width="49.64234620886982%">
                <p style='margin:0in;margin-bottom:.0001pt;font-size:13px;font-family:"Arial",sans-serif;text-align:center;'>&nbsp;XYZA  Paper Packing Production</p>
</td>

As you can see, the cell has a height and a width specified in points. The first problem was in my eyes a copy and past error. In Line 1055 (your code) in the case UnitMetric.Point you calculate the Height in Pixel and not in Points

old
properties.TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Dxa, Width = (unit.ValueInDxa * 20).ToString(CultureInfo.InvariantCulture) };
new
properties.TableCellWidth = new TableCellWidth() { Type = TableWidthUnitValues.Auto, Width = (unit.ValueInPoint * 20).ToString(CultureInfo.InvariantCulture)

The secound Problem was, that the current implementation is ignoring the cell height. So you can't set the height of the row. What we do in Line 1050 (our code) is to get the Height of the cell and in Line 1149 to 1164 we set this height as Row height to the openxml element. This is because in word the row and not the cell has the heigth.

I hope this change is all right with you. Concerning the entry points I will contact you again in a separate request.

Best Regards from Trier

Michael

p.s. I thought you could make several pull requests - but apparently it doesn't work. So now the two new checkins have been added to this request. Both checkins extend the HtmlConverter by two events to edit the creation of a tag before and after. This time I implemented the implementation without the enum and only trigger the events.

Can you please check if the changes are OK for you. If you need further changes please let me know.

onizet commented 5 years ago

Hello Michael,

Thank you very much for the explanation, this is more clear now :-) And thank you very much for the bug fix and your time!

I did a cherry-pick of your commits related to the cell width & height. Regarding the events, I'm less inclined to merge it because all your modification could have been done prior to the parsing, by using a more powerful Html library such as AngleSharp or HtmlAgilityPack. Moreover, you are exposing internal stuff (like the Paragraph which is a bit hacky) which will slow me down to refactor without breaking changes. And I'm working on v3 which is a big rewrite of the parser. I hope you will understand my position but I still open to the discussion

onizet commented 3 months ago

The bug fix has been cherry-picked but I disagree with the "eventing" API