public HttpResponseMessage ToHtml(WordToHtmlData wth) { try { OfficeConvertHandle officeConvertHandle = new OfficeConvertHandle(); officeConvertHandle.ConvertToHtml(wth); } catch (Exception ex) { return(tool.MsgFormat(ResponseCode.操作失败, "转换html失败", ex.ToString())); } return(tool.MsgFormat(ResponseCode.成功, "转换html成功", "0")); }
public HttpResponseMessage Download(ChangTestList data) { string docUrl = "D:\\workspace2015\\docxFile\\对经营高危险性体育项目的行政许可.docx"; try { WriteWordTableHandle writeWordTable = new WriteWordTableHandle(); ChangeTextData ctData = null; #region 处理json数组 JArray info = (JArray)JsonConvert.DeserializeObject(data.CtdListString); if (info != null) { for (int i = 0; i < info.Count; i++) { JObject joRet = (JObject)info[i]; if (joRet["bindingMethod"].ToString().Equals("1")) { //ctData = ChangeTextData.FromJson(joRet); //ctData.DocUrl = docUrl; ctData = new ChangeTextData() { DocUrl = docUrl, Line = joRet["line"].ToString() != string.Empty ? Convert.ToInt32(joRet["line"]) : -1, Column = joRet["column"].ToString() != string.Empty ? Convert.ToInt32(joRet["column"]) : -1, Value = joRet["value"].ToString() != string.Empty ? joRet["column"].ToString() : "", }; if (ctData.Line >= 0 && ctData.Column >= 0) { writeWordTable.ChangeTextInCell(ctData); } } } } #endregion #region word 转 html OfficeConvertHandle officeConvertHandle = new OfficeConvertHandle(); WordToHtmlData wth = new WordToHtmlData() { Sourcedocx = docUrl, TargetDirectory = "D:\\workspace2015\\docxFile" }; officeConvertHandle.ConvertToHtml(wth); #endregion #region html 转 pdf HtmlToPdfData htp = new HtmlToPdfData() { SourceHtml = "D:\\workspace2015\\docxFile\\对经营高危险性体育项目的行政许可.html", TargetPdf = "D:\\workspace2015\\docxFile\\对经营高危险性体育项目的行政许可.pdf" }; officeConvertHandle.HtmlToPdf(htp); #endregion #region 载文档 string fn = "对经营高危险性体育项目的行政许可.pdf"; string fileName = fn ; if (fileName.LastIndexOf(".") == -1) { fileName = string.Format("{0}.pdf", fn); } string filePath = "D:\\workspace2015\\docxFile\\" + fileName;//测试下载的路劲 FileStream stream = new FileStream(filePath, FileMode.Open); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); response.Content = new StreamContent(stream); response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") { FileName = HttpUtility.UrlEncode(fileName) }; response.Headers.Add("Access-Control-Expose-Headers", "FileName"); response.Headers.Add("FileName", HttpUtility.UrlEncode(fileName)); return(response); #endregion } catch (Exception ex) { return(tool.MsgFormat(ResponseCode.操作失败, "操作失败", ex.ToString())); } //return tool.MsgFormat(ResponseCode.成功, "操作成功", "0"); }
public void ConvertToHtml(WordToHtmlData wth) { var fi = new FileInfo(wth.Sourcedocx); 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 (wth.TargetDirectory != null && wth.TargetDirectory != string.Empty) { DirectoryInfo di = new DirectoryInfo(wth.TargetDirectory); if (!di.Exists) { throw new Exception("存储目录不存在"); } 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); } } }