plutext / docx4j-ImportXHTML

Converts XHTML to OpenXML WordML (docx) using docx4j
135 stars 124 forks source link

HTML Table's background-color attribute is not generating a table with background colour in word #94

Open Abhinandan578 opened 1 year ago

Abhinandan578 commented 1 year ago

I am trying to import html-based table, that includes background color style attribute, to word. I am using the latest version docx4j-ImportXHTML - 11.4.8. When the table is rendered in word background color is not coming. I tried adding it to style attribute in <tr> and <td> . Only with <td> i was able to generate background color in word, even <tr> is not working.

<table style="background-color:gray" border=4>
   <tr>
      <th>Month</th>
      <th>Savings</th>
   </tr>
   <tr style="background-color: #ff0000;">
      <td style="background-color: #00ff00;">January</td>
      <td>$100</td>
   </tr>
   <tr>
      <td>February</td>
      <td>$80</td>
   </tr>
</table>

Output Screenshot 2023-01-19 at 12 24 40 PM

The following xml is from a XHTMLImport converted document that was slightly simplified to improve readability. The highlight and shades are all in bold. As we can see shade property only got added to the <tc> and not to <table> and <tr> which is somehow unexpected.

<w:tbl>
    <w:tblPr>
        <w:tblpPr w:horzAnchor="page" w:leftFromText="180" w:rightFromText="180" w:tblpX="2894" w:tblpY="1333" w:vertAnchor="text"/>
        <w:tblW w:type="auto" w:w="0"/>
        <w:tblCellSpacing w:type="dxa" w:w="20"/>
        <w:tblBorders>
            ...
        </w:tblBorders>
        <w:tblLook w:firstColumn="1" w:firstRow="1" w:lastColumn="0" w:lastRow="0" w:noHBand="0" w:noVBand="1" w:val="04A0"/>
    </w:tblPr>
    <w:tblGrid>
        <w:gridCol w:w="1336"/>
        <w:gridCol w:w="1230"/>
    </w:tblGrid>
    <w:tr w14:paraId="3000B923" w14:textId="77777777" w:rsidR="00F76D05" w:rsidTr="008511EB">
        <w:trPr>
            <w:trHeight w:val="330"/>
            <w:tblCellSpacing w:type="dxa" w:w="20"/>
        </w:trPr>
        <w:tc>
            ...
        </w:tc>
    </w:tr>
    <w:tr w14:paraId="73652483" w14:textId="77777777" w:rsidR="00F76D05" w:rsidTr="008511EB">
        <w:trPr>
            <w:trHeight w:val="315"/>
            <w:tblCellSpacing w:type="dxa" w:w="20"/>
        </w:trPr>
        <w:tc>
            <w:tcPr>
                <w:tcW w:type="dxa" w:w="1276"/>
                <w:tcBorders>
                    ...
                </w:tcBorders>
                **<w:shd w:color="auto" w:fill="00FF00" w:val="clear"/>**
                <w:tcMar>
                    ...
                </w:tcMar>
                <w:vAlign w:val="center"/>
            </w:tcPr>
            <w:p w14:paraId="26CB3C52" w14:textId="77777777" w:rsidP="008511EB" w:rsidR="00F76D05" w:rsidRDefault="00F76D05">
                <w:r>
                    <w:rPr>
                        <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
                        <w:color w:val="000000"/>
                    </w:rPr>
                    <w:t>January</w:t>
                </w:r>
            </w:p>
        </w:tc>
        <w:tc>
            ...
        </w:tc>
    </w:tr>
    <w:tr w14:paraId="4D30ACBA" w14:textId="77777777" w:rsidR="00F76D05" w:rsidTr="008511EB">
        ...
    </w:tr>
</w:tbl>

Can anyone point me out whether i am doing something wrong or is it the limitation of library?

pkimouris commented 2 months ago

From my experience colors in hex format are working