Example T1280738
Visible to All Users

Word Processing - Export Only <BODY> Tag Content and Specify the Location of CSS Styles

This example uses the HtmlExporter class to export the document in HTML format.

The Export method enables you to emit only content encompassed by the specified root tag. The HtmlDocumentExporterOptions object passed as the method's parameter specifies the location of CSS styles - they can be exported to an external file, located within the style tag, or embedded as inline styles in the corresponding HTML tags.

The code uses a custom UriProvider to accomplish this task. The IUriProviderService.RegisterProvider method instantiates and registers the UriProvider service.

Files to Review

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Program.cs(vb)
C#
using DevExpress.XtraRichEdit; using DevExpress.XtraRichEdit.API.Native; using System; using DevExpress.XtraRichEdit.Export.Html; using System.Windows.Forms; using System.IO; using DevExpress.XtraRichEdit.Export; using System.Diagnostics; namespace ExportOnlyBodyContent { static class Program { /// <summary> /// The main entry point for the application. /// </summary> static CssPropertiesExportType cssExportType; static ExportRootTag htmlExportType; static void Main() { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); using (RichEditDocumentServer wordProcessor = new RichEditDocumentServer()) { wordProcessor.LoadDocument("Document.docx"); Document document = wordProcessor.Document; Console.WriteLine("Do you want to export HTML body? y/n"); string answer1 = Console.ReadLine()?.ToLower(); if (answer1 == "y") { htmlExportType = ExportRootTag.Body; } else { htmlExportType = ExportRootTag.Html; } Console.WriteLine("Choose one of the CSS options:\r\nInclude CSS in a <STYLE> tag. - 1\r\nSave style sheets in a separate CSS file - 2\r\nPlace CSS as an attribute to an HTML tag - 3"); string answer2 = Console.ReadLine()?.ToLower(); switch (answer2) { case "1": cssExportType = CssPropertiesExportType.Style; break; case "2": cssExportType = CssPropertiesExportType.Link; break; case "3": cssExportType = CssPropertiesExportType.Inline; break; } string fileName = "Exported.html"; string stringHtml = String.Empty; ExportHtml(out stringHtml, fileName, wordProcessor); SaveFile(fileName, stringHtml); var p = new Process(); p.StartInfo = new ProcessStartInfo("Exported.html") { UseShellExecute = true }; p.Start(); } } private static void SaveFile(string fileName, string value) { using (FileStream stream = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(value); } } } #region #exporting private static void ExportHtml(out string stringHtml, string fileName, RichEditDocumentServer wordProcessor) { stringHtml = String.Empty; HtmlDocumentExporterOptions options = new HtmlDocumentExporterOptions(); options.ExportRootTag = htmlExportType; options.CssPropertiesExportType = cssExportType; options.TargetUri = Path.GetFileNameWithoutExtension(fileName); Document document = wordProcessor.Document; var uriProvider = new MyUriProvider(Path.GetDirectoryName(Application.StartupPath)); stringHtml = document.GetHtmlText(document.Range, uriProvider, options); } #endregion #exporting } }
MyUriProvider.cs(vb)
C#
using System; using System.Collections.Generic; using System.Text; using System.Drawing; using System.Drawing.Imaging; using System.IO; using DevExpress.Office.Services; using DevExpress.XtraRichEdit.Utils; using DevExpress.Office.Utils; using DevExpress.Drawing; namespace ExportOnlyBodyContent { public class MyUriProvider : IUriProvider { string rootDirecory; public MyUriProvider(string rootDirectory) { if(String.IsNullOrEmpty(rootDirectory)) Exceptions.ThrowArgumentException("rootDirectory", rootDirectory); this.rootDirecory = rootDirectory; } public string CreateCssUri(string rootUri, string styleText, string relativeUri) { string cssDir = String.Format("{0}\\{1}", this.rootDirecory, rootUri.Trim('/')); if(!Directory.Exists(cssDir)) Directory.CreateDirectory(cssDir); string cssFileName = String.Format("{0}\\style.css", cssDir); File.AppendAllText(cssFileName, styleText); return GetRelativePath(cssFileName); } public string CreateImageUri(string rootUri, OfficeImage image, string relativeUri) { string imagesDir = String.Format("{0}\\{1}", this.rootDirecory, rootUri.Trim('/')); if(!Directory.Exists(imagesDir)) Directory.CreateDirectory(imagesDir); string imageName = String.Format("{0}\\{1}.png", imagesDir, Guid.NewGuid()); image.DXImage.Save(imageName, DXImageFormat.Png); return GetRelativePath(imageName); } string GetRelativePath(string path) { string substring = path.Substring(this.rootDirecory.Length); return substring.Replace("\\", "/").Trim('/'); } } }

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.