Hi guys
We encountered a new problem by using Aspose.word which version is 22.11 at dot-net platform.
When I inserted html with the following code, the generated Word document could not use the background color button of the paragraph to clear its background color.
Later, we found that its background color effect was set by the styles in Word.
Document doc = new Document("d:\\1.docx", new Aspose.Words.Loading.LoadOptions() { LoadFormat = LoadFormat.Auto });
DocumentBuilder db = new DocumentBuilder(doc);
db.MoveToDocumentEnd();
db.InsertHtml("<p style=\"margin-top:6pt; margin-left:1.35pt; margin-bottom:6pt; font-size:10pt\"><span><span style=\"font-family:Arial; background-color:#ffff00\">(</span></span><span id=\"c3e7e385_af87_4366_bb2d_1a8fcae11054\" sys=\"1\"><pre style=\"font-family:Arial; background-color:#ffff00\">Selected specimen(s) was/were tested as specified by the applicant, please refer to test result page(s) for details) </pre></span><span id=\"b1362029_99fa_4401_9881_0c78cd522d5a\" sys=\"1\"><span style=\"font-family:Arial; background-color:#c0c0c0\">(Selected specimens</span><span style=\"font-family:宋体; background-color:#c0c0c0\">客户指定</span></span><span><span style=\"font-family:宋体; background-color:#c0c0c0\">测试</span></span><span id=\"a2baf6cd_6f8a_4c65_bc4a_ff03043e3c10\" sys=\"1\"><span style=\"font-family:宋体; background-color:#c0c0c0\">物料</span><span style=\"font-family:Arial; background-color:#c0c0c0\">)</span></span></p>", true);
doc.Save("d:\\style.docx", SaveFormat.Docx);
Is there any way? The background-color CSS Style in the html I inserted applies the background color in the paragraph instead of using the heavyweight setting of style.
bellow is the document generated base on upon code .
style.docx (21.0 KB)
@wengyeung In your HTML background color is applied to <span>
that corresponds a Run node in Aspose.Words DOM. So the color is applied to like this:
<w:r>
<w:rPr>
<w:rFonts w:ascii="Arial" w:eastAsia="Arial" w:hAnsi="Arial" w:cs="Arial" />
<w:shd w:val="clear" w:color="auto" w:fill="C0C0C0" />
</w:rPr>
<w:t>(Selected specimens</w:t>
</w:r>
If you need the background color to be applied to paragraph element only, you should use HTML like this:
<p style='background-color:#c0c0c0'>This is a paragraph with background color</p>
In this case background will be applied on the paragraph level:
<w:p w:rsidR="00A77B3E">
<w:pPr>
<w:shd w:val="clear" w:color="auto" w:fill="C0C0C0" />
</w:pPr>
<w:r>
<w:t>This is a paragraph with background color</w:t>
</w:r>
</w:p>
Thank you for your reply
However, if I use Aspose to convert Word into html, it also uses “<p>"
contains "<span>"
element to describe the background color at the paragraph level, which is as follows, that is, I can’t use the Html generated by Aspose to insert it into Word and keep it consistent with the original format.
<p style="margin-top:6pt; margin-bottom:0pt">
<span style="background-color:#ff0000">Weng red</span>
<span> </span>
<span style="background-color:#ffff00">testing</span>
</p>
@wengyeung Actually, it is expected that in the provided example background is applied to span
elements. In the provided example there are two runs in the paragraph that have different background color.
In document XML the content is represented like this:
<w:p w14:paraId="3FD990E5" w14:textId="19904084" w:rsidR="002B2CA3" w:rsidRDefault="00F610D4">
<w:r w:rsidRPr="00F610D4">
<w:rPr>
<w:highlight w:val="red"/>
</w:rPr>
<w:t>Red</w:t>
</w:r>
<w:r w:rsidRPr="00F610D4">
<w:rPr>
<w:highlight w:val="yellow"/>
</w:rPr>
<w:t>Yellow</w:t>
</w:r>
</w:p>
so it is expected that such text is exported like this:
<p style="margin-top:0pt; margin-bottom:8pt">
<span style="background-color:#ff0000">Red</span><span style="background-color:#ffff00">Yellow</span>
</p>