We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Load Clipboard HTML & Convert to HTML Format such that CSS Styles are Written Inline as Value of Style Attributes by using C# .NET

I need to be able to insert from multiple clipboard formats, and insert into an MSHTML control.
If CF_HTML is in the clipboard, the MSHTML control’s in-built paste functionality does NOT interpret CSS within <STYLE></STYLE> blocks properly and injects old-style <FONT> tags instead, which is terrible (as they only use sizes 1 to 7 with SIZE=N). This changes the font size. I have discovered that if the style is within each element, like this:

<div style="font-family:Arial;font-size:23pt">hello world</div>

That the <FONT> tags are not inserted, and the paste works as expected.

I have a full licence of aspose total for .net, and have been trying to use both Aspose.Words and Aspose.Pdf to load up the HTML from the clipboard, and re-save it so that styling is placed inline in the elements (terrible, I know). But so far, the styling is all placed inside the <STYLE> tags.

Are there any mechanisms to help me achieve putting style information inside attributes of elements instead?


Thanks for contacting support.

Would you kindly share the sample code snippet(s) that you are using to achieve your requirements using Aspose.PDF or Aspose.Words. It would really be helpful if you can please share a sample project which is able to reproduce the issue you are facing. We will further test the scenario in our environment and address it accordingly.

I’m asking you if the API exists, so that makes it hard to provide a code sample, if I don’t know what I’m calling - or if I can call it.

What I’m trying to achieve is that when saving to HTML that instead of getting results like this:

<!-- lots of CSS -->
<!-- lots of divs with class="stl_etc"> -->

That I can produce inline styles instead:

     <!-- NO CSS -->
     <!-- lots of divs with style="CSS"> -->

Does the API support that?


You can use Aspose.Words for .NET API to convert to HTML format such that CSS styles are written inline (as a value of the style attribute on every element) by using the following code:

Document doc = new Document("E:\\Temp\\in.html");

HtmlSaveOptions opts = new HtmlSaveOptions(SaveFormat.Html);
opts.CssStyleSheetType = CssStyleSheetType.Inline;
opts.PrettyFormat = true;

doc.Save("E:\\Temp\\20.1.html", opts);

Hope, this helps.

Oh my! I can’t believe I didn’t spot that option. This is EXACTLY what I wanted. Thank you so much!