Hi – I am using StyleIdentifier, but it does not work as expected with respect to the design.
Code:
table.StyleIdentifier = StyleIdentifier.LightListAccent1;
Enclosed:
- Pls see the enclosed fully working code.
- Also, see the screenshot of Aspose generated table and table designed using Word.
Thanks.
Hi,
Thanks for your inquiry and sorry for the delayed response. In your case, once you have inserted the HTML table into the Word document, you can use the following code snippet to be able to apply table styling via StyleIdentifier:
Table tbl = (Table) doc.GetChild(NodeType.Table, 0, true);
foreach(Row r in tbl.Rows)
{
r.RowFormat.ClearFormatting();
foreach(Cell c in r.Cells)
c.CellFormat.ClearFormatting();
}
tbl.StyleIdentifier = StyleIdentifier.LightListAccent1;
tbl.StyleOptions = TableStyleOptions.FirstColumn | TableStyleOptions.RowBands | TableStyleOptions.FirstRow;
tbl.AutoFit(AutoFitBehavior.AutoFitToContents);
I hope, this will help.
Best Regards,
Thanks Awais. That’s works!
- Once I load a html to doc, is that matter whether it is an HTML table or doc table. The reason I am asking this one is that, I have no idea how my code gets the source (existing doc or html table).
- Clearing format, is this is the efficient way OR is there a better way? There might be thousand’s of cells.
Thanks.
Hi,
Thanks for your inquiry. When a table is imported from HTML, it is represented by Table object in DOM. Also, please note that when you use InsertHtml method, all formatting/styling is taken from HTML snippet, so you should specify whole formation in HTML. However, in your case, I think you can safely use the previous work around before applying Table style.
Please let me know if I can be of any further assistance.
Best Regards,
Hi Awais…Thx for your response.
In that case, say if I have cell like this below:
<td class="decimal scores" title="56">56-3</td>
What happens to those two attributes (class, title)?? Is there way to access them once it is converted??
Thx a lot.
Hi,
Thanks for your inquiry.
First of all, please note that currently Aspose.Words imports ‘title’ attributes of HTML tag as comments in DOM. We will provide an option to enable/disable this behaviour upon loading HTML in the next version of Aspose.Words which will be released within a couple of days. Your request has been linked to the appropriate feature and you will be notified as soon as it is available.
Secondly, when you apply a css class to td tag, it is simply converted to direct formatting for CELL node in DOM on save. Moreover, please see the following article to learn about restrictions during HTML import/export:
https://docs.aspose.com/words/net/convert-a-document-to-html-mhtml-or-epub/
Best Regards,
Hi there,
Just to clarify a part of what Awais has said, in the next version of Aspose.Words the title attribute will no longer be imported as a comment at all. There will be no option to turn this behavior on or off. In the future comments will be imported/exported using HTML footnotes.
Currently title will be ignored during import. The Aspose.Words DOM is based off the representation of Microsoft Word documents. Cells do not have titles in a Word document therefore there is currently no property to import title into.
Thanks,
Thanks Awis/Adam.
That’s fine. Do you see any other way I can access the html data attributes (metadata) after converting to Excel.
Hi,
Thanks for your inquiry. It is not quite clear what you are trying to achieve? Could you please describe your problem further and provide us more details. We will then investigate your scenario and provide you more information.
Best Regards,
Hi Awais – Thx a lot for asking me this question.
Take this html snippet:
<td class="highlight score">64-9<.td>
<td class="studentid">0000124233<td>
Values in the class attribute conveys not just formatting (but in the above example, it is just formatting). If there is a way to access those html attributes after conversion, we could apply those formats.
Well, the work-around currently what we have is:
- Convert to DOC
- Parse and iterate html, for every td in html – locate the doc table cell
- Apply Word formatting/style/rule based on the td class attribute.
If Aspose somehow preserves those attributes or ideally convert those class values in to Word Quick Style…that will be great.
Please let me know. Thanks.
Hi there,
Thanks for your inquiry and sorry for the delayed response.
First of all, please note that, a CSS class defined in HTML is imported into the Aspose.Words Document Object Model as a Style. Any content inserted from HTML with a CSS class set has the style automatically applied to it. Unfortunately, currently Aspose.Words does not support importing HTML table styles. Your request has been linked to the appropriate feature and you will be notified as soon as it is available. Sorry for inconvenience.
Moreover, to access CSS class attributes in DOM, I can offer you the following work around:
Since, Aspose.Words does support importing SPAN css class attributes please wrap the content of td inside SPAN tag as below:
// The modified HTML string
string htmlText = "<html><head><style type=\"text/css\"> span.someClass{color: red;}</style></head><body><table><tr><td><span class=\"someClass\">Content inside first cell</span></td><td>second cell</td></tr></table></body></html>";
// Load HTML from string into a new document
MemoryStream content = new MemoryStream(Encoding.UTF8.GetBytes(htmlText));
Document doc = new Document(content);
or insert an HTML snippet into a specific point at the document builder’s current position in an existing document by using the InsertHtml method like below.
DocumentBuilder builder = new DocumentBuilder(doc);
// Insert HTML from string into an existing document.
builder.InsertHtml(htmlText);
The details and properties of a style are stored in the Style class and can be retrieved/modified like below.
// You can retrieve a style using the same name as in CSS. The properties of the style can also be modified through ere.
Style style = doc.Styles["someClass"];
You can then easily set a DocumentBuilder to use this style and insert the styled content:
// Set the current style to the one imported from HTML. Insert some content
builder.Font.StyleName = "someClass";
Please let me know if I can be of any further assistance.
Best Regards,
Thanks a lot Awais. This takes us where we really want to go. But there is one simple fix we need…can you please suggest what needs to be done.
Pls see the enclosed and here is what I am trying to do:
- Fully working code enclosed.
- enclosed .dotx have quick styles called “highlight” and “footer”
- Doc is generated using the .dotx and I see that style applied correctly where those classes are.
But the actual style is not applied. Pls see the enclosed snapshot. Thx again.
Hi,
Thanks for the additional
information. While using latest Aspose.Words 11.1.0. I managed to reproduce this problem on my side. I have logged your issue into our
bug tracking system. Your request has also been linked to the appropriate issue. Once
we sort it out, we will let you know. Sorry for inconvenience.
The issues you have found earlier (filed as WORDSNET-5146) have been fixed in this .NET update and this Java update.
This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(2)
Since 3 issues filed under this one, is there a way I know which one got fixed? Thx a lot.
Hi there,
Thanks for your inquiry. There are three issues linked to this thread. Only one of them was fixed. Unfortunately, other two issues are not resolved yet. Please check below the progress of these issues:
|
|
WORDSNET - 318: |
It is pending for analysis and is in the queue.No ETA is available. |
WORDSNET - 5976: |
It is pending for analysis and is in the queue.No ETA is available. |
WORDSNET - 5146: |
This issue has been resolved. |
We will keep you informed via this forum thread and let you know once these issues are resolved.
Best Regards,
Any update on the unresolved ones? Thx.
Hi,
Thanks for your inquiry. Unfortunately, there is no further news about WORDSNET-318 and WORDSNET-5976. We will inform you as soon as these are resolved.
Best Regards,
The issues you have found earlier (filed as WORDSNET-5976) have been fixed in this .NET update and this Java update.
This message was posted using Notification2Forum from Downloads module by aspose.notifier.
Thx a lot.
BTW, what issue is 5976 – since 3 issues are logged in and I don’t know which one is for which.
Also, It’ll be great if we know the timeline to fix the remaining one.
Thx again.