public IActionResult WordToHtml() { var file = Request.Form.Files.First(); try { using (MemoryStream memoryStream = new MemoryStream()) { file.CopyTo(memoryStream); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = file.Name }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); return(Json(new { success = true, data = html.ToStringNewLineOnAttributes() })); } } } catch (Exception ex) { return(Json(new { success = false, data = ex.Message })); } }
public bool WordToHTML() { bool res = false; try { byte[] byteArray = File.ReadAllBytes("C:/Users/lsamaniego/Documents/Sprint Backlog.docx"); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "My Page Title" }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); File.WriteAllText("C:/Users/lsamaniego/Documents/Sprint Backlog.html", html.ToStringNewLineOnAttributes()); } } res = true; } catch (Exception ex) { throw; } return(res); }
private string ConvertToHtml(IFormFile file) { using var ms = file.OpenReadStream(); using var fs = new FileStream(Path.Combine(Environment.GetEnvironmentVariable("temp") ?? "upload", file.FileName), FileMode.OpenOrCreate, FileAccess.ReadWrite); ms.CopyTo(fs); using var doc = WordprocessingDocument.Open(fs, true); var pageTitle = file.FileName; var part = doc.CoreFilePropertiesPart; if (part != null) { pageTitle ??= (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault(); } var settings = new HtmlConverterSettings() { PageTitle = pageTitle, FabricateCssClasses = false, RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { var stream = new MemoryStream(); imageInfo.Bitmap.Save(stream, imageInfo.Bitmap.RawFormat); var base64String = Convert.ToBase64String(stream.ToArray()); return(new XElement(Xhtml.img, new XAttribute(NoNamespace.src, $"data:{imageInfo.ContentType};base64," + base64String), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null)); } }; var htmlElement = HtmlConverter.ConvertToHtml(doc, settings); var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement); var htmlString = html.ToString(SaveOptions.DisableFormatting); return(htmlString); }
public byte[] HtmlToPdf(byte[] bytes) { MemoryStream origin = new MemoryStream(bytes, true); MemoryStream destination = new MemoryStream(); origin.CopyToAsync(destination); origin.Close(); using (MemoryStream memoryStream = new MemoryStream()) { using (WordprocessingDocument doc = WordprocessingDocument.Open(destination, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { FabricateCssClasses = true, }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); MemoryStream result = new MemoryStream(); html.Save(result); result.Position = 0; return(result.ToArray()); } } }
public string WordToHtml(MemoryStream ms) { using (WordprocessingDocument doc = WordprocessingDocument.Open(ms, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); return(html.ToStringNewLineOnAttributes()); } }
public string ConvertToHTML() { var wmldoc = new WmlDocument("tempDoc", MediaFile.GetMediaStream); var settings = new HtmlConverterSettings() { PageTitle = "HTML Converted Page", }; var html = HtmlConverter.ConvertToHtml(wmldoc, settings); }
public async ValueTask <ManagedIdDto> RenderWordAsync(Stream stream, string title, TUserBrief user, Guid id) { var anytask = AnyAsync(a => a.ArticleId == id); var article = Activator.CreateInstance <TArticle>(); using WordprocessingDocument doc = WordprocessingDocument.Open(stream, true); HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "My Page Title", ImageHandler = WordImageHandler }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); article.AuthorBrief = user; var santilizetask = Santilize(html.ToStringNewLineOnAttributes()); article.Title = title; article.IsDraft = true; article.Visibility = ArticleVisibility.everyone; article.Saved = true; article.DraftOrPublishId = Guid.Empty; Guid aid = Guid.Empty; article.Content = await santilizetask; article.Content = string.Concat(article.Content, @" <hr> <p>本文章由word文档转换而成</p> "); var ud = Builders <TArticle> .Update .Set(a => a.Content, article.Content) .Set(a => a.Saved, true); if (id != Guid.Empty && await anytask) { await UpDateAsync(a => a.ArticleId == id && a.IsDraft == true, ud); aid = id; } else { article.ArticleId = Guid.NewGuid(); article.DraftOrPublishId = Guid.Empty; await AddArticleAsync(article); aid = article.ArticleId; } return(new ManagedIdDto(await GetManagedIdAsync(aid), aid)); }
public static XElement ConvertToHtml(WordprocessingDocument wordDocument) { HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "My Page Title", CssClassPrefix = "", Css = "", ConvertFormatting = false }; XElement html = HtmlConverter.ConvertToHtml(wordDocument, settings, null); return(html); }
private static async Task <string> ConvertToHtml(IFormFile file) { var docfile = Path.Combine(Environment.GetEnvironmentVariable("temp") ?? "upload", file.FileName); try { await using var ms = file.OpenReadStream(); await using var fs = System.IO.File.Create(docfile, 1024, FileOptions.DeleteOnClose); await ms.CopyToAsync(fs); using var doc = WordprocessingDocument.Open(fs, true); var pageTitle = file.FileName; var part = doc.CoreFilePropertiesPart; if (part != null) { pageTitle ??= (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault(); } var settings = new HtmlConverterSettings() { PageTitle = pageTitle, FabricateCssClasses = false, RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { var stream = new MemoryStream(); imageInfo.Bitmap.Save(stream, imageInfo.Bitmap.RawFormat); var base64String = Convert.ToBase64String(stream.ToArray()); return(new XElement(Xhtml.img, new XAttribute(NoNamespace.src, $"data:{imageInfo.ContentType};base64," + base64String), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null)); } }; var htmlElement = HtmlConverter.ConvertToHtml(doc, settings); var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement); var htmlString = html.ToString(SaveOptions.DisableFormatting); return(htmlString); } finally { if (System.IO.File.Exists(docfile)) { Policy.Handle <IOException>().WaitAndRetry(5, i => TimeSpan.FromSeconds(1)).Execute(() => System.IO.File.Delete(docfile)); } } }
public StringBuilder getWordDoc(string docName, string version) { var str = GetUploadedDoc(docName, version); Stream stream = str.Result; StringBuilder word = new StringBuilder(); using (WordprocessingDocument doc = WordprocessingDocument.Open(stream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = docName }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); word.Append(html); } return(word); }
public IActionResult DocToPdf() { //Это если нужно конвертировать меньше 3 страниц // Document document = new Document(Path.Combine(Environment.CurrentDirectory, "SignData_and_VerifyData.docx"), FileFormat.Docx); // document.SaveToFile(Path.Combine(Environment.CurrentDirectory, "docToPdfTest3.pdf"), FileFormat.PDF); // var source = Package.Open(Path.Combine(Environment.CurrentDirectory, "SignData_and_VerifyData.docx")); var source = Package.Open(@"Table.docx"); var document = WordprocessingDocument.Open(source); HtmlConverterSettings settings = new HtmlConverterSettings(); XElement html = HtmlConverter.ConvertToHtml(document, settings); string htmlStr = html.ToString(); var doc = new HtmlToPdfDocument() { GlobalSettings = { ColorMode = ColorMode.Color, Orientation = Orientation.Landscape, PaperSize = PaperKind.A4Plus, Out = Path.Combine(Environment.CurrentDirectory, "test23.pdf"), }, Objects = { new ObjectSettings() { PagesCount = true, HtmlContent = htmlStr, WebSettings = { DefaultEncoding = "utf-8" }, HeaderSettings ={ FontSize =9, Right = "Страница [page] из [toPage]", Line = true, Spacing = 2.812 }, FooterSettings ={ FontSize =9, Right = "Страница [page] из [toPage]" } } } }; _converter.Convert(doc); return(View()); }
private static string ConverWordToHtml(string wordFilePath) { byte[] byteArray = File.ReadAllBytes(wordFilePath); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "Daily Status" }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); return(html.ToStringNewLineOnAttributes()); } } }
/// <summary> /// Convert word to html /// </summary> /// <param name="input"></param> /// <param name="mediaPath"></param> /// <returns></returns> public HtmlResult ToHtml(byte[] input, string mediaPath) { var result = new HtmlResult(); var imageHandler = mediaPath == null ? new Base64ImageHandler() : new FileImageHandler(mediaPath) as IImageHandler; using (var stream = new MemoryStream(input)) { using (var doc = WordprocessingDocument.Open(stream, true)) { var settings = new HtmlConverterSettings { PageTitle = "" }; var htmlConverterService = HostContainer.GetInstance <IHtmlConverterService>(); var html = htmlConverterService.ConvertToHtml(doc, settings, imageHandler.Handle); result.Html = html.ToStringNewLineOnAttributes(); } } result.Files = imageHandler.Files; return(result); }
public FileModel WordToPdf(byte[] data, string fileName = null) { byte[] byteArray = data; using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { //PageTitle = "My Page Title" AdditionalCss = "td { border : 1px solid #000 !important; }" }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); SelectPdf.HtmlToPdf htmlToPdf = new SelectPdf.HtmlToPdf(); return(_pdfSvc.GetPdfFromHtmlString(html.ToStringNewLineOnAttributes(), Path.GetFileNameWithoutExtension(fileName) ?? "Document" + ".pdf")); //File.writeal(@"Test.html", html.ToStringNewLineOnAttributes()); } } }
public static string Convert(string inputPath) { var fi = new FileInfo(inputPath); byte[] byteArray = File.ReadAllBytes(fi.FullName); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { FabricateCssClasses = true, RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false }; XElement html = HtmlConverter.ConvertToHtml(wDoc, settings); return(html.ToString(SaveOptions.DisableFormatting)); } } }
private async Task <IActionResult> ConvertDocxToHtmlWithoutImageAsync(string fileName, byte[] fileBytes) { try { var docFilePath = Path.Combine(_tempPath, fileName); System.IO.File.WriteAllBytes(docFilePath, fileBytes); var source = Package.Open(docFilePath); var document = WordprocessingDocument.Open(source); HtmlConverterSettings settings = new HtmlConverterSettings(); XElement html = HtmlConverter.ConvertToHtml(document, settings); var outfile = Path.Combine(_tempPath, ("htmldata" + ".html")); var writer = System.IO.File.CreateText(outfile); writer.WriteLine(html.ToString()); writer.Dispose(); source.Close(); System.IO.File.Delete(docFilePath); return(File(await System.IO.File.ReadAllBytesAsync(outfile), "application/octet-stream", (Path.GetFileNameWithoutExtension(fileName) + ".html"))); } catch (Exception ex) { return(StatusCode(StatusCodes.Status500InternalServerError)); } }
public static void ConvertToHtml(string file, string outputDirectory) { var fi = new FileInfo(file); Console.WriteLine(fi.Name); 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 = fi.FullName; var part = wDoc.CoreFilePropertiesPart; if (part != null) { pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fi.FullName; } // TODO: Determine max-width from size of content area. HtmlConverterSettings settings = new HtmlConverterSettings() { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", 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") { imageFormat = ImageFormat.Png; } 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); } string imageSource = localDirInfo.Name + "/image" + imageCounter.ToString() + "." + extension; XElement img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; XElement htmlElement = HtmlConverter.ConvertToHtml(wDoc, settings); // Produce HTML document with <!DOCTYPE html > declaration to tell the browser // we are using HTML5. var html = new XDocument( new XDocumentType("html", null, null, null), htmlElement); // 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 static void ConvertToHtmlNoCssClasses(FileInfo sourceDocx, FileInfo destFileName) { byte[] byteArray = File.ReadAllBytes(sourceDocx.FullName); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { var outputDirectory = destFileName.Directory; destFileName = new FileInfo(Path.Combine(outputDirectory.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 = sourceDocx.FullName; } HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = pageTitle, FabricateCssClasses = false, 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); } } }
private void button1_Click(object sender, EventArgs e) { Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pl-PL"); using (DocX document = DocX.Load(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój1.docx")) { // Check if all the replace patterns are used in the loaded document. document.ReplaceText("@PrzykładowyTekstDoZmiany0", "PierwszaWstawkaBezSPacji_Znakmi!#$%^)&()", false, System.Text.RegularExpressions.RegexOptions.None, new Formatting() { Bold = true }); document.ReplaceText("@PrzykładowyTekstDoZmiany1", "Zwykły tekst ", false, System.Text.RegularExpressions.RegexOptions.None, new Formatting() { Bold = true }); document.ReplaceText("@PrzykładowyTekstDoZmiany2", "Zwykły tekst, dodany przez aplikację ( ) ", false, System.Text.RegularExpressions.RegexOptions.None, new Formatting() { Bold = true }); var p = document.InsertParagraph(); p.Append("Przykładowy czerwony tekst dodany na końcu,") .Font(new Font("Arial")) .FontSize(25) .Color(System.Drawing.Color.Red) .Bold() .Append(" zawierający dodatkowe inne niebieskie formatowanie").Font(new Font("Times New Roman")).Color(System.Drawing.Color.Blue).Italic() .SpacingAfter(40); // Save this document to disk. document.SaveAs(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.docx"); using (DocX document1 = DocX.Load(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój1.docx")) { using (DocX document3 = DocX.Load(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.docx")) { document1.InsertDocument(document3, true); document1.SaveAs(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój3.docx"); } } var n = DateTime.Now; var tempDi = new DirectoryInfo(string.Format("ExampleOutput-{0:00}-{1:00}-{2:00}-{3:00}{4:00}{5:00}", n.Year - 2000, n.Month, n.Day, n.Hour, n.Minute, n.Second)); tempDi.Create(); /* * This example loads each document into a byte array, then into a memory stream, so that the document can be opened for writing without * modifying the source document. */ //foreach (var file in Directory.GetFiles("../../", "*.docx")) //{ // ConvertToHtml(file, tempDi.FullName); //} byte[] byteArray = File.ReadAllBytes(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.docx"); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "My Page Title" }; XElement html = HtmlConverter.ConvertToHtml(doc, settings); File.WriteAllText(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.html", html.ToStringNewLineOnAttributes()); } } string test = File.ReadAllText(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.hmtl"); //Byte[] res = null; //using (MemoryStream ms = new MemoryStream()) //{ // var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(test, PdfSharp.PageSize.A4); // pdf.Save(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.pdf"); // res = ms.ToArray(); //} //Load Document Spire.Doc.Document document2 = new Spire.Doc.Document(); document2.LoadFromFile(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\Mój2.docx"); //Convert Word to PDF document2.SaveToFile("toPDF.PDF", FileFormat.PDF); //Launch Document System.Diagnostics.Process.Start("toPDF.PDF"); string[] pdfFiles = { @"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF.PDF", @"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF.PDF", @"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF.PDF", @"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF.PDF", @"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF.PDF", @"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF.PDF" }; PdfDocument outputPDFDocument = new PdfDocument(); foreach (string pdfFile in pdfFiles) { PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import); outputPDFDocument.Version = inputPDFDocument.Version; foreach (PdfPage page in inputPDFDocument.Pages) { outputPDFDocument.AddPage(page); } } outputPDFDocument.Save(@"C:\Users\Ernest\source\repos\Word_Pdf\Word_Pdf\bin\Debug\toPDF2.PDF"); } }
public static HtmlConverterSettings Settings(string htmlPath, string pageTitle) { string imageDir = Path.Combine(Path.GetDirectoryName(htmlPath), "_files"); var dirInfo = new DirectoryInfo(imageDir); if (dirInfo.Exists) { foreach (var f in dirInfo.GetFiles()) { f.Delete(); } dirInfo.Delete(); } var imageCounter = 0; HtmlConverterSettings settings = new HtmlConverterSettings() { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = pageTitle, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { DirectoryInfo localDirInfo = new DirectoryInfo(imageDir); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { imageFormat = ImageFormat.Png; } 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 (imageFormat == null) { return(null); } string imageFileName = imageDir + "/image" + imageCounter.ToString() + "." + extension; try { imageInfo.Bitmap.Save(imageFileName, imageFormat); } catch (System.Runtime.InteropServices.ExternalException) { return(null); } string imageSource = localDirInfo.Name + "/image" + imageCounter.ToString() + "." + extension; XElement img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; return(settings); }
public static (string title, string content) ConvertToHtml(string docxFilePath, ConverterSettings settings) { string title; string content; var fi = new FileInfo(docxFilePath); var byteArray = File.ReadAllBytes(fi.FullName); using (var memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (var wDoc = WordprocessingDocument.Open(memoryStream, true)) { title = fi.FullName; var part = wDoc.CoreFilePropertiesPart; if (part != null) { title = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fi.FullName; } title = PathUtils.GetFileNameWithoutExtension(title); // TODO: Determine max-width from size of content area. var htmlSettings = new HtmlConverterSettings { // AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = title, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { if (settings.IsClearImages || string.IsNullOrEmpty(settings.ImageDirectoryPath)) { return(null); } DirectoryUtils.CreateDirectoryIfNotExists(settings.ImageDirectoryPath); var extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { imageFormat = ImageFormat.Png; } 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); } var imageFileName = StringUtils.GetShortGuid(false) + "." + extension; var imageFilePath = PathUtils.Combine(settings.ImageDirectoryPath, imageFileName); try { imageInfo.Bitmap.Save(imageFilePath, imageFormat); } catch (System.Runtime.InteropServices.ExternalException) { return(null); } var imageSource = PageUtils.Combine(settings.ImageDirectoryUrl, imageFileName); var img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; var htmlElement = HtmlConverter.ConvertToHtml(wDoc, htmlSettings); // Produce HTML document with <!DOCTYPE html > declaration to tell the browser // we are using HTML5. var html = new XDocument( new XDocumentType("html", null, null, null), htmlElement); // 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); var htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(htmlString); var style = htmlDoc.DocumentNode.SelectSingleNode("//style").OuterHtml; var body = htmlDoc.DocumentNode.SelectSingleNode("//body").InnerHtml; // var style = HtmlToWmlConverter.CleanUpCss((string)htmlElement.Descendants().FirstOrDefault(d => d.Name.LocalName.ToLower() == "style")); content = $"{style}{Environment.NewLine}{body}"; if (settings.IsSaveHtml && !string.IsNullOrEmpty(settings.HtmlDirectoryPath) && DirectoryUtils.IsDirectoryExists(settings.HtmlDirectoryPath)) { var htmlFilePath = PathUtils.Combine(settings.HtmlDirectoryPath, PathUtils.GetFileNameWithoutExtension(docxFilePath) + ".html"); File.WriteAllText(htmlFilePath, htmlString, Encoding.UTF8); } } } if (settings.IsFirstLineTitle) { var contentTitle = RegexUtils.GetInnerContent("p", content); contentTitle = StringUtils.StripTags(contentTitle); if (!string.IsNullOrEmpty(contentTitle) && settings.IsFirstLineRemove) { content = StringUtils.ReplaceFirst(contentTitle, content, string.Empty); } if (!string.IsNullOrEmpty(contentTitle)) { contentTitle = contentTitle.Trim(); contentTitle = contentTitle.Trim(' ', ' '); contentTitle = StringUtils.StripEntities(contentTitle); } if (!string.IsNullOrEmpty(contentTitle)) { title = contentTitle; } } if (settings.IsClearFormat) { content = HtmlClearUtils.ClearFormat(content); } if (settings.IsFirstLineIndent) { content = HtmlClearUtils.FirstLineIndent(content); } if (settings.IsClearFontSize) { content = HtmlClearUtils.ClearFontSize(content); } if (settings.IsClearFontFamily) { content = HtmlClearUtils.ClearFontFamily(content); } if (settings.IsFirstLineRemove) { content = StringUtils.ReplaceFirst(title, content, string.Empty); } if (string.IsNullOrEmpty(title)) { title = PathUtils.GetFileNameWithoutExtension(docxFilePath); } return(title, content); }
public static string Convert(FileInfo fileInfo, string outputDir) { var byteArray = File.ReadAllBytes(fileInfo.FullName); using var memoryStream = new MemoryStream(); memoryStream.Write(byteArray, offset: 0, byteArray.Length); using var wordDoc = WordprocessingDocument.Open(memoryStream, isEditable: true); var htmlFileName = new FileInfo(fileInfo.Name.Replace(".docx", ".html")); if (!string.IsNullOrEmpty(outputDir)) { var dirInfo = new DirectoryInfo(outputDir); if (!dirInfo.Exists) { throw new Exception("Output directory does not exist"); } htmlFileName = new FileInfo(Path.Combine(dirInfo.FullName, htmlFileName.Name)); } var imageDir = htmlFileName.FullName.Substring(startIndex: 0, htmlFileName.FullName.Length - 5) + "_images"; var imageCounter = 0; var pageTitle = fileInfo.FullName; var part = wordDoc.CoreFilePropertiesPart; if (part != null) { pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? fileInfo.FullName; } var settings = new HtmlConverterSettings { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = pageTitle, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { var localDirInfo = new DirectoryInfo(imageDir); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; var extension = imageInfo.ContentType.Split(separator: '/')[1].ToLower(); ImageFormat imageFormat = null; switch (extension) { case "png": imageFormat = ImageFormat.Png; break; case "gif": imageFormat = ImageFormat.Gif; break; case "bmp": imageFormat = ImageFormat.Bmp; break; case "jpeg": imageFormat = ImageFormat.Jpeg; break; case "tiff": // Convert tiff to gif. extension = "gif"; imageFormat = ImageFormat.Gif; break; case "x-wmf": extension = "wmf"; imageFormat = ImageFormat.Wmf; break; } // 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); } var imageFileName = imageDir + "/image" + imageCounter + "." + extension; try { imageInfo.Bitmap.Save(imageFileName, imageFormat); } catch (ExternalException) { return(null); } var imageSource = localDirInfo.Name + "/image" + imageCounter + "." + extension; var img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; var htmlElement = HtmlConverter.ConvertToHtml(wordDoc, settings); // Produce HTML document with <!DOCTYPE html > declaration to tell the browser // we are using HTML5. var html = new XDocument( new XDocumentType("html", publicId: null, systemId: null, internalSubset: null), htmlElement); var htmlString = html.ToString(SaveOptions.DisableFormatting); File.WriteAllText(htmlFileName.FullName, htmlString, Encoding.UTF8); return(htmlFileName.FullName); }
public static FileInfo ToHtml(string docxPath, string htmlPath) { var docxFileInfo = new FileInfo(docxPath); byte[] docxBytes = File.ReadAllBytes(docxFileInfo.FullName); var htmlFileInfo = new FileInfo(htmlPath); htmlFileInfo.Directory.Create(); if (docxBytes.Length <= 0) { // Create file with empty content if (!File.Exists(htmlFileInfo.FullName)) { File.Create(htmlFileInfo.FullName); } return(htmlFileInfo); } try { using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(docxBytes, 0, docxBytes.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { var imageDirectoryName = Path.Combine(htmlFileInfo.DirectoryName, $"{Path.GetFileNameWithoutExtension(htmlFileInfo.FullName)}_files"); int imageCounter = 0; var pageTitle = Path.GetFileNameWithoutExtension(docxFileInfo.Name); // TODO: Determine max-width from size of content area. HtmlConverterSettings settings = new HtmlConverterSettings { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = pageTitle, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { imageFormat = ImageFormat.Png; } 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); } string imageSource = $"{htmlFileInfo.Directory.Name}/image_{imageCounter.ToString()}.{extension}"; XElement img = new XElement( Xhtml.img, new XAttribute(NoNamespace.src, imageSource), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; XElement htmlElement = HtmlConverter.ConvertToHtml(wDoc, settings); // Produce HTML document with <!DOCTYPE html > declaration to tell the // browser we are using HTML5. var html = new XDocument(new XDocumentType("html", null, null, null), htmlElement); // 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(htmlFileInfo.FullName, htmlString, Encoding.UTF8); } } } catch { // Ignore } return(htmlFileInfo); }
public async Task ConvertWordToRazorViewAsync(IFormFile BlogBody, Blog Blog) { fileName = ContentDispositionHeaderValue.Parse(BlogBody.ContentDisposition).FileName.Trim('"'); if (fileName.EndsWith(".docx")) { var oldBlogFileUploads = new DirectoryInfo($"{_environment.WebRootPath}\\BlogFiles"); if (oldBlogFileUploads.Exists) { if (oldBlogFileUploads.GetFiles() != null) { foreach (var file in oldBlogFileUploads.GetFiles()) { file.Delete(); } } oldBlogFileUploads.Delete(); } var BlogFiles = Directory.CreateDirectory("BlogFiles"); var BlogFileUploads = Path.Combine(_environment.WebRootPath, BlogFiles.FullName); var newFile = fileName; var newHtmldoc = ChangeFileExtension(".docx", ".cshtml", BlogBody); var newHtmldocPath = Path.Combine(BlogFileUploads, newHtmldoc); var newPath = Path.Combine(BlogFileUploads, newFile); await BlogBody.SaveAsAsync(newPath); using (WordprocessingDocument doc = WordprocessingDocument.Open(newPath, true)) { var settings = new HtmlConverterSettings { PageTitle = Blog.BlogTitle }; var htmlDocument = HtmlConverter.ConvertToHtml(doc, settings); File.WriteAllText(newHtmldocPath, htmlDocument.ToStringNewLineOnAttributes()); //get folder: "BlogFiles" var BlogUploadsdir = new DirectoryInfo(newHtmldocPath).Parent.ToString(); //get the parent folder full path, i.e the application name. var parentDir = new DirectoryInfo(BlogUploadsdir).Parent.FullName.ToString(); //get the "Views" folder of the application. var viewsDir = GetViewsDirectory(parentDir); //get a collection of directories under "Views" folder and create "BlogUploads" folder. var ViewsDirs = new DirectoryInfo(viewsDir).EnumerateDirectories(); var blogUploadsDir = ReturnHomeDir(ViewsDirs).CreateSubdirectory("BlogUploads"); //create new path for the newly created Razor view file and move it there. var viewHtmldocPath = Path.Combine(blogUploadsDir.FullName, newHtmldoc); File.Move(newHtmldocPath, viewHtmldocPath); //persist the path to the newly created Razor View in the database. Blog.BlogBody = $"{ConvertToRelativePath(viewHtmldocPath)}"; } } }
public override string ConvertToHtml(FileInfo file, EpubWriter writer) { this.LastStatus = $"Processing File: {file.Name}"; byte[] byteArray = File.ReadAllBytes(file.FullName); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { var pageTitle = file.FullName; var part = wDoc.CoreFilePropertiesPart; if (part != null) { pageTitle = (string)part.GetXDocument().Descendants(DC.title).FirstOrDefault() ?? file.FullName; } HtmlConverterSettings settings = new HtmlConverterSettings() { AdditionalCss = "body { margin: 1cm auto; max-width: 20cm; padding: 0; }", PageTitle = pageTitle, FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { string extension = imageInfo.ContentType.Split('/')[1].ToLower(); SysImg.ImageFormat imageFormat = null; if (extension == "png") { imageFormat = SysImg.ImageFormat.Png; } else if (extension == "gif") { imageFormat = SysImg.ImageFormat.Gif; } else if (extension == "bmp") { imageFormat = SysImg.ImageFormat.Bmp; } else if (extension == "jpeg") { imageFormat = SysImg.ImageFormat.Jpeg; } else if (extension == "tiff") { // Convert tiff to gif. extension = "gif"; imageFormat = SysImg.ImageFormat.Gif; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = SysImg.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 fileName = $"{Guid.NewGuid()}.{extension}"; string filePath = $"images/{fileName}"; using (var ms = new MemoryStream()) { imageInfo.Bitmap.Save(ms, SysImg.ImageFormat.Png); writer.AddFile(filePath, ms.ToArray(), EpubSharp.Format.EpubContentType.ImagePng); } XElement ximg = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, filePath), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(ximg); } }; XElement htmlElement = HtmlConverter.ConvertToHtml(wDoc, settings); var html = new XDocument( new XDocumentType("html", null, null, null), htmlElement); var htmlString = html.ToString(SaveOptions.DisableFormatting); return(htmlString); } } }
protected override void ProcessRecord() { foreach (var document in AllDocuments("Export-OpenXmlToHtml")) { try { if (!(document is WmlDocument)) { throw new PowerToolsDocumentException("Not a wordprocessing document."); } FileInfo info = null; if (document.FileName != null) { info = new FileInfo(document.FileName); } string htmlFileName; if (OutputPath != null) { htmlFileName = System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, OutputPath); } else { if (info == null) { throw new ArgumentException("No output file name available."); } htmlFileName = System.IO.Path.Combine(info.DirectoryName, info.Name.Substring(0, info.Name.Length - info.Extension.Length) + ".html"); } string imageFolder; FileInfo outputInfo = new FileInfo(htmlFileName); if (ImageFolder != null) { imageFolder = SessionState.Path.GetResolvedPSPathFromPSPath(ImageFolder).First().Path; } else { imageFolder = System.IO.Path.Combine(outputInfo.DirectoryName, "images"); } DirectoryInfo imageFolderInfo = new DirectoryInfo(imageFolder); if (!imageFolderInfo.Exists) { imageFolderInfo.Create(); } HtmlConverterSettings settings = new HtmlConverterSettings(); settings.PageTitle = PageTitle; if (CssPath != null) { settings.CssClassPrefix = CssClassPrefix; // Read CSS file into a string string cssFileName = SessionState.Path.GetResolvedPSPathFromPSPath(CssPath).First().Path; settings.Css = File.ReadAllText(cssFileName); } int imageCounter = 0; XElement html = HtmlConverter.ConvertToHtml((WmlDocument)document, settings, imageInfo => { ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { // Convert png to jpeg. extension = "jpeg"; imageFormat = ImageFormat.Jpeg; } else if (extension == "bmp") { imageFormat = ImageFormat.Bmp; } else if (extension == "jpeg") { imageFormat = ImageFormat.Jpeg; } else if (extension == "tiff") { imageFormat = ImageFormat.Tiff; } // 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 = System.IO.Path.Combine(imageFolder, "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); }); File.WriteAllText(htmlFileName, html.ToStringNewLineOnAttributes()); } catch (Exception e) { WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document)); } } }
private static void ConvertToHtml(byte[] byteArray, DirectoryInfo destDirectory, string htmlFileName) { FileInfo fiHtml = new FileInfo(Path.Combine(destDirectory.FullName, htmlFileName)); using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument wDoc = WordprocessingDocument.Open(memoryStream, true)) { var imageDirecotryFullName = fiHtml.FullName.Substring(0, fiHtml.FullName.Length - fiHtml.Extension.Length) + "_files"; var imageDirectoryRelativeName = fiHtml.Name.Substring(0, fiHtml.Name.Length - fiHtml.Extension.Length) + "_files"; int imageCounter = 0; //var pageTitle = (string)wDoc // .CoreFilePropertiesPart // .GetXDocument() // .Descendants(DC.title) // .FirstOrDefault(); HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "File", FabricateCssClasses = true, CssClassPrefix = "pt-", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { DirectoryInfo localDirInfo = new DirectoryInfo(imageDirecotryFullName); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { 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") { extension = "gif"; imageFormat = ImageFormat.Gif; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = ImageFormat.Wmf; } if (imageFormat == null) { return(null); } FileInfo imageFileName = new FileInfo(imageDirecotryFullName + "/image" + imageCounter.ToString() + "." + extension); try { imageInfo.Bitmap.Save(imageFileName.FullName, imageFormat); } catch (System.Runtime.InteropServices.ExternalException) { return(null); } XElement img = new XElement(Xhtml.img, new XAttribute(NoNamespace.src, imageDirectoryRelativeName + "/" + imageFileName.Name), imageInfo.ImgStyleAttribute, imageInfo.AltText != null ? new XAttribute(NoNamespace.alt, imageInfo.AltText) : null); return(img); } }; // XElement html = HtmlConverter.ConvertToHtml(wDoc, settings); XElement html = HtmlConverter.ConvertToHtml(wDoc, settings); var body = html.Descendants(Xhtml.body).First(); //body.AddFirst( // new XElement(Xhtml.p, // new XElement(Xhtml.a, // new XAttribute("href", "WebForm1.aspx"), "Go back to upload page"))); var htmlString = html.ToString(SaveOptions.DisableFormatting); File.WriteAllText(fiHtml.FullName, htmlString, Encoding.UTF8); } } }
public static void WordToHtml() { byte[] byteArray = File.ReadAllBytes(@"D:\OfficeDev\Word\201701\Test.docx"); string css = @" p.PtNormal {margin-bottom:10.0pt; font-size:11.0pt; font-family:""Times"";} span.PtDefaultParagraphFont {margin-top:24.0pt; font-size:14.0pt; font-family:""Helvetica""; color:yellow;} h1.PtHeading1 {margin-top:24.0pt; font-size:14.0pt; font-family:""Helvetica""; color:blue;} h2.PtHeading2 {margin-top:10.0pt; font-size:13.0pt; font-family:""Helvetica""; color:blue;}"; using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { int imageCounter = 0; HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "My Page Title", CssClassPrefix = "Pt", AdditionalCss = css, ImageHandler = imageInfo => { DirectoryInfo localDirInfo = new DirectoryInfo(@"D:\OfficeDev\Word\201701\img"); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { 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") { extension = "gif"; imageFormat = ImageFormat.Gif; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = ImageFormat.Wmf; } if (imageFormat == null) { return(null); } string imageFileName = @"D:\OfficeDev\Word\201701\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(doc, settings); File.WriteAllText(@"D:\OfficeDev\Word\201701\WordHtml.html", html.ToStringNewLineOnAttributes()); }; } }
public XElement ConvertToHTML(byte[] byteArray) { XElement html; using (MemoryStream memoryStream = new MemoryStream()) { memoryStream.Write(byteArray, 0, byteArray.Length); using (WordprocessingDocument doc = WordprocessingDocument.Open(memoryStream, true)) { int imageCounter = 0; HtmlConverterSettings settings = new HtmlConverterSettings() { PageTitle = "My Page Title", FabricateCssClasses = true, AdditionalCss = "", GeneralCss = "", CssClassPrefix = "", RestrictToSupportedLanguages = false, RestrictToSupportedNumberingFormats = false, ImageHandler = imageInfo => { DirectoryInfo localDirInfo = new DirectoryInfo("img"); if (!localDirInfo.Exists) { localDirInfo.Create(); } ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { 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") { extension = "gif"; imageFormat = ImageFormat.Gif; } else if (extension == "x-wmf") { extension = "wmf"; imageFormat = ImageFormat.Wmf; } if (imageFormat == null) { return(null); } string imageFileName = "img/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); } }; html = HtmlConverter.ConvertToHtml(doc, settings); //System.IO.File.WriteAllText("kk.html", html.ToStringNewLineOnAttributes()); }; } return(html); }
protected override void ProcessRecord() { foreach (var document in AllDocuments("Export-OpenXmlToHtml")) { try { if (!(document is WmlDocument)) throw new PowerToolsDocumentException("Not a wordprocessing document."); FileInfo info = null; if (document.FileName != null) info = new FileInfo(document.FileName); string htmlFileName; if (OutputPath != null) htmlFileName = System.IO.Path.Combine(SessionState.Path.CurrentLocation.Path, OutputPath); else { if (info == null) throw new ArgumentException("No output file name available."); htmlFileName = System.IO.Path.Combine(info.DirectoryName, info.Name.Substring(0, info.Name.Length - info.Extension.Length) + ".html"); } string imageFolder; FileInfo outputInfo = new FileInfo(htmlFileName); if (ImageFolder != null) imageFolder = SessionState.Path.GetResolvedPSPathFromPSPath(ImageFolder).First().Path; else imageFolder = System.IO.Path.Combine(outputInfo.DirectoryName, "images"); DirectoryInfo imageFolderInfo = new DirectoryInfo(imageFolder); if (!imageFolderInfo.Exists) imageFolderInfo.Create(); HtmlConverterSettings settings = new HtmlConverterSettings(); settings.PageTitle = PageTitle; if (CssPath != null) { settings.CssClassPrefix = CssClassPrefix; // Read CSS file into a string string cssFileName = SessionState.Path.GetResolvedPSPathFromPSPath(CssPath).First().Path; settings.Css = File.ReadAllText(cssFileName); } int imageCounter = 0; XElement html = HtmlConverter.ConvertToHtml((WmlDocument)document, settings, imageInfo => { ++imageCounter; string extension = imageInfo.ContentType.Split('/')[1].ToLower(); ImageFormat imageFormat = null; if (extension == "png") { // Convert png to jpeg. extension = "jpeg"; imageFormat = ImageFormat.Jpeg; } else if (extension == "bmp") imageFormat = ImageFormat.Bmp; else if (extension == "jpeg") imageFormat = ImageFormat.Jpeg; else if (extension == "tiff") imageFormat = ImageFormat.Tiff; // 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 = System.IO.Path.Combine(imageFolder, "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; }); File.WriteAllText(htmlFileName, html.ToStringNewLineOnAttributes()); } catch (Exception e) { WriteError(PowerToolsExceptionHandling.GetExceptionErrorRecord(e, document)); } } }