public static void SetResolveFontNames(string dataDir) { // ExStart:SetResolveFontNames Document doc = new Document(dataDir + "Test File (docx).docx"); Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Html); saveOptions.PrettyFormat = true; saveOptions.ResolveFontNames = true; dataDir = dataDir + "ResolveFontNames_out.html"; doc.Save(dataDir, saveOptions); // ExEnd:SetResolveFontNames Console.WriteLine("\nFontSettings is used to resolve font family name.\nFile saved at " + dataDir); }
public static void SetExportCidUrlsForMhtmlResources(string dataDir) { // ExStart:SetExportCidUrlsForMhtmlResources var doc = new Aspose.Words.Document(dataDir + "CidUrls.docx"); Aspose.Words.Saving.HtmlSaveOptions saveOptions = new Aspose.Words.Saving.HtmlSaveOptions(SaveFormat.Mhtml); saveOptions.PrettyFormat = true; saveOptions.ExportCidUrlsForMhtmlResources = true; dataDir = dataDir + "SetExportCidUrlsForMhtmlResources_out.mhtml"; doc.Save(dataDir, saveOptions); // ExEnd:SetExportCidUrlsForMhtmlResources Console.WriteLine("\nDocument has saved with Content - Id URL scheme.\nFile saved at " + dataDir); }
/// <summary> /// word生成html字符串 /// </summary> /// <param name="path">word路径</param> /// <returns></returns> public override string ToHTML(string path) { //openxml引用 //string htmlPath=WmlToHtmlConverterHelper.ConvertToHtml(path, this.FilePath); //aspose引用 api网址http://www.aspose.com if (!File.Exists(path)) { return(string.Empty); } //PageNumberFinder Document doc = new Document(path); var paperSize = doc.FirstSection.PageSetup.PaperSize.ToString(); //纸张大小 var pageCount = doc.PageCount; //获得页数 doc.MailMerge.UseNonMergeFields = false; doc.MailMerge.UseWholeParagraphAsRegion = true; //doc.MailMergeSettings.ViewMergedData = false; //doc.MailMergeSettings.LinkToQuery = true; //doc.MailMergeSettings.MailAsAttachment = true; //html设置 ws.HtmlSaveOptions option = new ws.HtmlSaveOptions(SaveFormat.Html); option.ExportHeadersFootersMode = ws.ExportHeadersFootersMode.PerSection; option.PrettyFormat = true; option.UseAntiAliasing = true; option.ExportTocPageNumbers = false; option.ExportRoundtripInformation = true; //option.ExportListLabels = ExportListLabels.AsInlineText; string name = Path.GetFileNameWithoutExtension(path); //生成图片保存路径 option.ImagesFolderAlias = MapPathReverse(path); //Path.Combine(Path.GetDirectoryName(path), name); //string.Format("/Resource/emw/UserFile/{0}/", name); var savePath = Path.Combine(this.FilePath, name + "\\"); if (!Directory.Exists(savePath)) { Directory.CreateDirectory(savePath); } LayoutCollector layoutCollector = new LayoutCollector(doc); // 构建布局模型,收集必要的信息。 doc.UpdatePageLayout(); // 节点在文档分割成单独的页面。 DocumentPageSplitter splitter = new DocumentPageSplitter(layoutCollector); //for (int page = 1; page <= doc.PageCount; page++) //{ // Document pageDoc = splitter.GetDocumentOfPage(page); // NodeCollection paras = pageDoc.GetChildNodes(NodeType.Paragraph, true); // foreach (Paragraph para in paras) // { // if (para.GetText().Contains("Evaluation Only. Created with Aspose.Words. Copyright 2003-2016 Aspose Pty Ltd.")) // { // paras.Remove(para); // } // } // var pageFileName = Path.Combine(savePath, string.Format("{0}-page{1}{2}", name, page, ".html")); // pageDoc.Save(pageFileName,option); //} // 从文档中分离收集器。 layoutCollector.Document = null; //html保存地址 string htmlPath = savePath + name + ".html"; doc.Save(htmlPath, option); XElement xhtml = XElement.Load(htmlPath); var idCounter = 1000000; foreach (var d in xhtml.Descendants()) { if (d.Name.LocalName == "head" || d.Name.LocalName == "meta" || d.Name.LocalName == "title" || d.Name.LocalName == "body") { continue; } string idName = d.Name.LocalName + "-" + idCounter.ToString().Substring(1); XAttribute id = new XAttribute("data-uniqid", idName); if (d.Attribute("id") == null) { d.Add(id); } idCounter++; } File.WriteAllText(htmlPath, xhtml.ToString(), Encoding.UTF8); string htmlString = xhtml.ToString();// File.ReadAllText(htmlPath); string jsonStr = JsonConvert.SerializeObject(new { pageCount = pageCount, paperSize = paperSize, htmlString = htmlString, path = path }); return(jsonStr); }
/// <summary> /// email content uploader /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void emailButtonUpload_Click(object sender, EventArgs e) { if (emailTemplateFileUpload.HasFile) { FilePath = Server.MapPath("~/uploads"); fileName = Path.Combine(Server.MapPath("~/uploads"), emailTemplateFileUpload.FileName); emailTemplateFileUpload.SaveAs(fileName); filenames = Directory.GetFiles(Server.MapPath("~/uploads")); if (fileName.Length > 0) { Aspose.Words.Document doc = new Aspose.Words.Document(FilePath + "\\" + emailTemplateFileUpload.FileName); // Set an option to export form fields as plain text, not as HTML input elements. Aspose.Words.Saving.HtmlSaveOptions options = new Aspose.Words.Saving.HtmlSaveOptions(Aspose.Words.SaveFormat.Html); options.ExportTextInputFormFieldAsText = true; doc.Save(FilePath + "\\MailMergeHtml.html", options); string body = string.Empty; using (System.IO.StreamReader reader = new System.IO.StreamReader(FilePath + "\\MailMergeHtml.html")) { body = reader.ReadToEnd(); mailMergeHtml.Text = body; } GeneratePdf(body); //generate required pdf } message.Text = "MailMerge Template Loaded successfully"; message.Visible = true; message.Attributes.Add("class", "alert alert-success"); setValues(); } else { LoadData(); message.Text = "Please First Select MailMerge Document"; message.Visible = true; message.Attributes.Add("class", "alert alert-danger"); } }