Hello - I’m trying to convert HTML string to Doc using Aspose.Words but it seems that it does not support the css style display:none… what are the possible fix for this or work around?
example html:
<html><body><div>display1</div><div style='display:none'>display2</div></body></html>
@kdepadua As I can see text with display:none
is read from HTML as hidden as expected. Here is the output produced on my side:
out.docx (7.5 KB)
As you can see display2
text is shown as hidden:
But on my end it is still visible on the output file… although the checkbox hidden is also checked…
@alexey.noskov By the way, I’m using this line of VB code
Dim doc As New Document(New MemoryStream(System.Text.Encoding.UTF8.GetBytes("<html><body><div>display1</div><div style='display:none'>display2</div></body></html>")), New HtmlLoadOptions())
doc.Save(pageStream, SaveFormat.Docx)
@kdepadua
The Aspose.Words library does not support the CSS style display: none;
when converting HTML to DOC format. This means that any content styled with display: none;
will not be included in the resulting document.
To work around this limitation, you can consider the following options:
-
Remove the display: none;
Style: Before converting the HTML, you can preprocess the HTML string to remove any elements with the display: none;
style. This way, all content will be included in the conversion.
-
Use Conditional Logic: If you need to keep the content hidden in the HTML but still want it to appear in the DOC, you can use a different approach, such as setting a class that you can check for in your application logic and then decide whether to include that content in the conversion.
-
Alternative CSS Properties: Instead of using display: none;
, consider using other CSS properties that might achieve a similar visual effect without being excluded from the conversion, such as visibility: hidden;
. However, be aware that this will still render the space for the element in the document.
Here’s a simple example of how you might preprocess the HTML to remove elements with display: none;
:
string html = "<html><body><div>display1</div><div style='display:none'>display2</div></body></html>";
html = Regex.Replace(html, "<div[^>]*style=['\"]?display:\\s*none;?['\"]?[^>]*>.*?</div>", string.Empty);
This code uses a regular expression to find and remove any <div>
elements that have the display: none;
style.
By applying one of these methods, you should be able to work around the limitation of Aspose.Words regarding the display: none;
CSS property.
do you have documentation on what CSS is not supported by Aspose.Words?
@kdepadua It looks like MS Word on your side is configured to show the hidden text. You can check properties of the text and see that the text is marked as hidden.
@alexey.noskov here’s the properties…
can you confirm if the css style display:none is not supported by Aspose.Words? I’m trying to convert HTML to DOC
@kdepadua
It is supported.
As I have mentioned the text is properly marked as hidden. This is seen on your screenshot too.
@alexey.noskov how about if the css is external? would it still work as expected? I tried it on my end and the property is not marked as hidden…
@kdepadua This should work the same with an external css. Please attach your problematic input and output documents here for our reference. We will check them and provide you more information.