I can now see the new version of Aspose.Words which I have downloaded and implemented in my following code:
private string getSectionHtml(Section section, string title)
{
string returnHtml;
Document dummyDoc = new Document();
dummyDoc.RemoveAllChildren();
dummyDoc.AppendChild(dummyDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting));
dummyDoc.BuiltInDocumentProperties.Title = title;
dummyDoc.SaveOptions.ExportPrettyFormat = true;
// This is to allow headings to appear to the left of main text.
dummyDoc.SaveOptions.HtmlExportAllowNegativeLeftIndent = true;
dummyDoc.SaveOptions.HtmlExportHeadersFooters = false;
dummyDoc.SaveOptions.HtmlExportCssStyleSheetType = CssStyleSheetType.External;
// Code here for obtaining HTML in a string. Note that images will still be written into files!
MemoryStream stream = new MemoryStream();
// By default, images will be written into the system TEMP folder! control this by using the following option.
dummyDoc.SaveOptions.ExportImagesFolder = _imagesOriginDir;
dummyDoc.Save(stream, SaveFormat.Html);
// Rewind the stream to beginning.
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
returnHtml = reader.ReadToEnd();
// close the stream
stream.Close();
return returnHtml;
}
When I ran the code I get the following error:
“You are saving HTML to a stream and requesting a CSS style sheet to be written into a separate file. This is not supported. You need to either save HTML to a file or request CSS style sheet to be embedded.”
This is a problem because I need to be able to use the stream to get the string value of the html in order to add it to a placeholder via code.
I then noticed that I only need the html between the body tags anyway so added the following code, as well as applying the embedded style as opposed to the external option:
private string getSectionHtml(Section section, string title)
{
string returnHtml;
Document dummyDoc = new Document();
dummyDoc.RemoveAllChildren();
dummyDoc.AppendChild(dummyDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting));
dummyDoc.BuiltInDocumentProperties.Title = title;
dummyDoc.SaveOptions.ExportPrettyFormat = true;
// This is to allow headings to appear to the left of main text.
dummyDoc.SaveOptions.HtmlExportAllowNegativeLeftIndent = true;
dummyDoc.SaveOptions.HtmlExportHeadersFooters = false;
dummyDoc.SaveOptions.HtmlExportCssStyleSheetType = CssStyleSheetType.Embedded;
// Code here for obtaining HTML in a string. Note that images will still be written into files!
MemoryStream stream = new MemoryStream();
// By default, images will be written into the system TEMP folder! control this by using the following option.
dummyDoc.SaveOptions.ExportImagesFolder = _imagesOriginDir;
dummyDoc.Save(stream, SaveFormat.Html);
// Rewind the stream to beginning.
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
returnHtml = reader.ReadToEnd();
// close the stream
stream.Close();
// Strip everything in the html before and after the tags
// We won't require this html as it is already included when the sitecore item is created.
returnHtml = TrimHtml(returnHtml, "", "");
returnHtml = returnHtml.Replace(" class=\"Normal0\"", "");
returnHtml = returnHtml.Replace(" class=\"ListBullet\"", "");
return returnHtml;
}
The only thing then is there are still references in tags which I wanted to remove (this is the only way I can see to do this) as above.
Now this seemed to do the trick and after doing some initial testing all seemed well. But then I noticed that any tables in the html still had in-built styles! Is there a way which I can strip these table styles? In particular the font size and font family?
As always any advice or suggestions would be greatly appreciated.
This message was posted using Email2Forum by romank.