public XElement ConvertToHtml(HtmlConverterSettings htmlConverterSettings) { return(HtmlConverter.ConvertToHtml(this, htmlConverterSettings)); }
public static void ConvertToHtml(string file, string outputDirectory) { var fi = new FileInfo(file); byte[] byteArray = File.ReadAllBytes(fi.FullName); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { var destFileName = new FileInfo(fi.Name.Replace(".docx", ".html")); if (outputDirectory != null && outputDirectory != string.Empty) { DirectoryInfo di = new DirectoryInfo(outputDirectory); if (!di.Exists) { throw new OpenXmlPowerToolsException("Output directory does not exist"); } destFileName = new FileInfo(Path.Combine(di.FullName, destFileName.Name)); } var imageDirectoryName = destFileName.FullName.Substring(0, destFileName.FullName.Length - 5) + "_files"; int imageCounter = 0; var pageTitle = (string)wDoc.CoreFilePropertiesPart.GetXDocument().Descendants(DC.title).FirstOrDefault(); if (pageTitle == null) { pageTitle = fi.FullName; } HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = pageTitle, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { DirectoryInfo localDirInfo = new DirectoryInfo(imageDirectoryName); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { // Convert png to jpeg. extension = "gif"; imageFormat = ImageFormat.Gif; } else if (extension == "gif") { imageFormat = ImageFormat.Gif; } else if (extension == "bmp") { imageFormat = ImageFormat.Bmp; } else if (extension == "jpeg") { imageFormat = ImageFormat.Jpeg; } else if (extension == "tiff") { // Convert tiff to gif. extension = "gif"; imageFormat = ImageFormat.Gif; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = ImageFormat.Wmf; } // If the image format isn't one that we expect, ignore it, // and don't return markup for the link. if (imageFormat == null) { return(null); } string imageFileName = imageDirectoryName + "/image" + imageCounter.ToString() + "." + extension; try { imageInfo.Bitmap.Save(imageFileName, imageFormat); } catch (System.Runtime.InteropServices.ExternalException) { return(null); } XElement img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageFileName), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; XElement html = HtmlConverter.ConvertToHtml(wDoc, settings); // Note: the xhtml returned by ConvertToHtmlTransform contains objects of type // XEntity. PtOpenXmlUtil.cs define the XEntity class. See // http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx // for detailed explanation. // // If you further transform the XML tree returned by ConvertToHtmlTransform, you // must do it correctly, or entities will not be serialized properly. var htmlString = html.ToString(SaveOptions.DisableFormatting); File.WriteAllText(destFileName.FullName, htmlString, Encoding.UTF8); } } }
public XElement ConvertToHtml(HtmlConverterSettings htmlConverterSettings, Func <ImageInfo, XElement> imageHandler) { return(HtmlConverter.ConvertToHtml(this, htmlConverterSettings, imageHandler)); }