/// <summary> /// 替换书签内容 /// </summary> /// <param name="bookmarkName">书签名</param> /// <param name="value">替换内容</param> public void ReplaceBookMark(string bookmarkName, string value, string type = "", double width = 320, double height = 320) { var bm = doc.Range.Bookmarks[bookmarkName]; if (bm == null) { return; } try { if (type == "IMG") { bm.Text = ""; builder.MoveToBookmark(bookmarkName); var img = builder.InsertImage(@value, width, height); // width 350像素, height 350像素 //Console.WriteLine(bookmarkName+"替换图片成功!"); doc.UpdateFields(); //更新域 } else { bm.Text = ""; builder.MoveToBookmark(bookmarkName); //bm.Text = value;//会把文字格式情况 builder.Write(value); //修改书签内容的第二种方法,不会更改文字字体等属性 //Console.WriteLine(bookmarkName+"替换书签内容成功!"); } } catch (Exception e) { Console.WriteLine("错误:" + e.Message); } }
/// <summary> /// 生成word with BookMark /// </summary> /// <param name="templatePath">模版路径</param> /// <param name="filePath">生成文件路径</param> /// <param name="dataTable">数据源</param> /// <param name="tables">需要生成表格的数据源,每个DataTable的TableName为对应的书签名</param> public static void GenerateWordWithBookmark(string templatePath, string filePath, DataTable dataTable, DataSet tables = null, List <double> columnsWidth = null) { //载入模板 var doc = new Aspose.Words.Document(templatePath); DocumentBuilder builder = new DocumentBuilder(doc); foreach (DataColumn col in dataTable.Columns) { //判断是否有该标签 if (doc.Range.Bookmarks[col.ColumnName] != null) { //标签值替换 doc.Range.Bookmarks[col.ColumnName].Text = dataTable.Rows[0][col.ColumnName].ToString(); } } //判断需要创建表格 if (tables != null) { doc.MailMerge.ExecuteWithRegions(tables); } //更新所有域的值(为公式域重新计算值) doc.UpdateFields(); //保存合并后的文档 doc.Save(filePath); }
public void UpdateTOC() { Aspose.Words.Document doc = new Aspose.Words.Document(); //ExStart //ExId:UpdateTOC //ExSummary:Shows how to completely rebuild TOC fields in the document by invoking field update. doc.UpdateFields(); //ExEnd }
/// <summary> /// 生成word /// </summary> /// <param name="templatePath">模版路径</param> /// <param name="dataTable">数据源</param> public static void GenerateWord(string templatePath, string filePath, DataTable dataTable) { //载入模板 var doc = new Aspose.Words.Document(templatePath); //合并模版,相当于页面的渲染 doc.MailMerge.Execute(dataTable); //更新所有域的值(为公式域重新计算值) doc.UpdateFields(); //生成文件 doc.Save(filePath); }
public void UpdateFieldsBeforeRendering() { //ExStart //ExFor:Document.UpdateFields //ExId:UpdateFieldsBeforeRendering //ExSummary:Shows how to update all fields before rendering a document. Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Rendering.doc"); // This updates all fields in the document. doc.UpdateFields(); doc.Save(MyDir + "Rendering.UpdateFields Out.pdf"); //ExEnd }
private void AddBodyAndSetData() { var jklcDocument = new Aspose.Words.Document(GetSroce("Tunnel.Word.TunnelMonthFiles.监控量测.docx")); var builder = new DocumentBuilder(jklcDocument); if (this._bodyModel != null) { WordManage.SetModel(this._bodyModel, jklcDocument, builder); } jklcDocument.UpdateFields(); _coverCount = jklcDocument.PageCount - 2; _word.Document.AppendDocument(jklcDocument, ImportFormatMode.KeepSourceFormatting); SetEnclosure(); builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary); WordManage.BookMarkReplace(_word.Document, builder, "totalPage", _coverCount.ToString()); }
/// <summary> /// 生成多个模版内容合并的文档 /// </summary> /// <param name="templatePath">模版路径</param> /// <param name="filePath">生成文件路径</param> /// <param name="dataSet">每个模版对应的数据,每个DataTable的列名对应模版签名名</param> /// <param name="insertPageBreak">每段内容间是否插入分页符</param> public static void GenerateWordWithBookmarkMultiple(string templatePath, string filePath, DataSet dataSet, bool insertPageBreak = false) { //载入模板 var temp_doc = new Aspose.Words.Document(templatePath); //创建生成文件 var new_doc = new Aspose.Words.Document(); //新文档build var new_bulid = new DocumentBuilder(new_doc); int i = 0; foreach (DataTable dataTable in dataSet.Tables) { foreach (DataColumn col in dataTable.Columns) { //判断是否有该标签 if (temp_doc.Range.Bookmarks[col.ColumnName] != null) { //标签值替换 temp_doc.Range.Bookmarks[col.ColumnName].Text = dataTable.Rows[0][col.ColumnName].ToString(); } } //追加到新文档 new_bulid.MoveToDocumentEnd();//移动到文档末尾 if (i != 0 && insertPageBreak) { new_bulid.InsertBreak(BreakType.SectionBreakNewPage); //插入分页符 } InsertDocument(new_bulid.CurrentParagraph, temp_doc); //插入模版内容 //删除空行 if (!new_bulid.CurrentParagraph.HasChildNodes) { new_bulid.CurrentParagraph.Remove(); } i++; } //更新所有域的值(为公式域重新计算值) new_doc.UpdateFields(); //保存合并后的文档 new_doc.Save(filePath); }
/// <summary> /// 生成word /// </summary> /// <param name="templatePath">模版路径</param> /// <param name="dataTable">数据源</param> /// <param name="dataSet">生成表格数据源,每个DataTable的TableName为表格的名称</param> public static byte[] GenerateWord(string templatePath, DataTable dataTable, DataSet dataSet = null) { //载入模板 var doc = new Aspose.Words.Document(templatePath); //合并模版,相当于页面的渲染 if (dataTable != null) { doc.MailMerge.Execute(dataTable); } if (dataSet != null) { doc.MailMerge.ExecuteWithRegions(dataSet); } //更新所有域的值(为公式域重新计算值) doc.UpdateFields(); var docStream = new MemoryStream(); doc.Save(docStream, SaveOptions.CreateSaveOptions(SaveFormat.Doc)); return(docStream.ToArray()); }
public ActionResult Okay() { // Load document var STYLE = db.Database.SqlQuery <MASTER_SNI_STYLE>("SELECT * FROM MASTER_SNI_STYLE WHERE SNI_STYLE_STATUS = 1 ORDER BY SNI_STYLE_SORT ASC").ToList(); Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Users\Ghany Cahyadi\Desktop\Res\WEW.docx"); // Keep track if the Paragraph containing the heading text is found bool isParagraphFound = false; // Stores nodes that are to be removed from document ArrayList nodesToBeDeleted = new ArrayList(); bool start = true; // Get a collection of all paragraph nodes in the document Node[] xxxx = doc.GetChildNodes(NodeType.Run, true).ToArray(); Node[] paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).ToArray(); foreach (Paragraph paragraph in paragraphs) { if (paragraph.ParagraphFormat.StyleName.Equals("Judul")) { start = paragraph.Range.Text.StartsWith("Prakata"); // Filter Heading 1 paras and find one that contains the search string if (paragraph.Range.Text.StartsWith("Prakata")) { isParagraphFound = true; // We need to delete all nodes present in between the startPara node // and the next Paragraph with Heading 1 Paragraph startPara = paragraph; do { nodesToBeDeleted.Add(startPara); startPara = startPara.NextSibling as Paragraph; }while (!startPara.ParagraphFormat.StyleName.Equals("Judul")); // Break the main loop as we got the list of nodes to be removed if (isParagraphFound) { break; } } } //var AksesSelect = db.Database.SqlQuery<SYS_DOC_ACCESS_DETAIL_SELECT>("SELECT * FROM SYS_DOC_ACCESS_DETAIL_SELECT WHERE DOC_ACCESS_DETAIL_STYLE_STATUS = 0 ORDER BY SNI_STYLE_SORT ASC").ToList(); //// We are interested in only Paragraphs with Heading 1 style //if (AksesSelect.Count() > 0) //{ // foreach (var i in AksesSelect) // { // //TEST += i.SNI_STYLE_NAME +"|"+i.SNI_STYLE_VALUE + "<BR>"; // var SNI_STYLE_VALUE = Convert.ToString(i.SNI_STYLE_VALUE); // var SNI_STYLE_NAME = Convert.ToString(i.SNI_STYLE_NAME); // if (paragraph.ParagraphFormat.StyleName.Equals(SNI_STYLE_VALUE)) // { // // Filter Heading 1 paras and find one that contains the search string // if (paragraph.Range.Text.StartsWith(SNI_STYLE_NAME)) // { // isParagraphFound = true; // // We need to delete all nodes present in between the startPara node // // and the next Paragraph with Heading 1 // Paragraph startPara = paragraph; // do // { // nodesToBeDeleted.Add(startPara); // startPara = startPara.NextSibling as Paragraph; // } // while (!startPara.ParagraphFormat.StyleName.Equals(SNI_STYLE_VALUE)); // // Break the main loop as we got the list of nodes to be removed // if (isParagraphFound) // break; // } // } // } //} } // Remove all nodes foreach (Node node in nodesToBeDeleted) { node.Remove(); } doc.UpdateFields(); var Jml = doc.PageCount; doc.Save(@"C:\Users\Ghany Cahyadi\Desktop\Res\out.docx"); doc.Save(@"C:\Users\Ghany Cahyadi\Desktop\Res\out.xml"); return(Json(new { //TEST, //data = nodesToBeDeleted.Count, Jml }, JsonRequestBehavior.AllowGet)); }
public ActionResult Okay2() { // Load document Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Users\Ghany Cahyadi\Desktop\Res\WEW.docx"); // Keep track if the Paragraph containing the heading text is found bool isParagraphFound = false; // Stores nodes that are to be removed from document ArrayList nodesToBeDeleted = new ArrayList(); // Get a collection of all paragraph nodes in the document Node[] paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).ToArray(); foreach (Paragraph paragraph in paragraphs) { if (paragraph.ParagraphFormat.StyleName.Equals("Judul")) { // Filter Heading 1 paras and find one that contains the search string if (paragraph.Range.Text.StartsWith("Ruang lingkup")) { isParagraphFound = true; // We need to delete all nodes present in between the startPara node // and the next Paragraph with Heading 1 Paragraph startPara = paragraph; do { nodesToBeDeleted.Add(startPara); startPara = startPara.NextSibling as Paragraph; }while (!startPara.ParagraphFormat.StyleName.Equals("Judul")); // Break the main loop as we got the list of nodes to be removed if (isParagraphFound) { break; } } } } // Remove all nodes foreach (Node node in nodesToBeDeleted) { node.Remove(); } doc.UpdateFields(); var Jml = doc.PageCount; doc.Save(@"C:\Users\Ghany Cahyadi\Desktop\Res\out.docx"); doc.Save(@"C:\Users\Ghany Cahyadi\Desktop\Res\out.xml"); return(Json(new { nodesToBeDeleted }, JsonRequestBehavior.AllowGet)); }
public void UpdateFields() { //ExStart //ExFor:Document.UpdateFields //ExId:UpdateFieldsInDocument //ExSummary:Shows how to update all fields in a document. Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + "Document.doc"); doc.UpdateFields(); //ExEnd }
public void DocumentBuilderInsertTOC() { //ExStart //ExId:DocumentBuilderInsertTOC //ExSummary:Shows how to insert a Table of Contents field into a document. Aspose.Words.Document doc = new Aspose.Words.Document(); DocumentBuilder builder = new DocumentBuilder(doc); // Insert a table of contents at the beginning of the document. builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u"); // The newly inserted table of contents will be initially empty. // It needs to be populated by updating the fields in the document. doc.UpdateFields(); //ExEnd }
public void InsertToc() { //ExStart //ExFor:DocumentBuilder.InsertTableOfContents //ExFor:Document.UpdateFields //ExFor:DocumentBuilder.#ctor(Document) //ExFor:ParagraphFormat.StyleIdentifier //ExFor:DocumentBuilder.InsertBreak //ExFor:BreakType //ExId:InsertTableOfContents //ExSummary:Demonstrates how to insert a Table of contents (TOC) into a document using heading styles as entries. // Use a blank document Aspose.Words.Document doc = new Aspose.Words.Document(); // Create a document builder to insert content with into document. DocumentBuilder builder = new DocumentBuilder(doc); // Insert a table of contents at the beginning of the document. builder.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u"); // Start the actual document content on the second page. builder.InsertBreak(BreakType.PageBreak); // Build a document with complex structure by applying different heading styles thus creating TOC entries. builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; builder.Writeln("Heading 1"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln("Heading 1.1"); builder.Writeln("Heading 1.2"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; builder.Writeln("Heading 2"); builder.Writeln("Heading 3"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln("Heading 3.1"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading3; builder.Writeln("Heading 3.1.1"); builder.Writeln("Heading 3.1.2"); builder.Writeln("Heading 3.1.3"); builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading2; builder.Writeln("Heading 3.2"); builder.Writeln("Heading 3.3"); // Call the method below to update the TOC. doc.UpdateFields(); //ExEnd doc.Save(ExDir + "DocumentBuilder.InsertToc Out.docx"); }
public void UpdateFieldsBeforeRendering() { //ExStart //ExFor:Document.UpdateFields //ExId:UpdateFieldsBeforeRendering //ExSummary:Shows how to update all fields before rendering a document. Aspose.Words.Document doc = new Aspose.Words.Document(ExDir + "Rendering.doc"); // This updates all fields in the document. doc.UpdateFields(); doc.Save(ExDir + "Rendering.UpdateFields Out.pdf"); //ExEnd }
public ActionResult Coba3() { // Load document Aspose.Words.Document doc = new Aspose.Words.Document(@"C:\Users\Ghany Cahyadi\Desktop\Res\TestFile.doc"); // Keep track if the Paragraph containing the heading text is found bool isParagraphFound = false; // Stores nodes that are to be removed from document ArrayList nodesToBeDeleted = new ArrayList(); // Get a collection of all paragraph nodes in the document Node[] paragraphs = doc.GetChildNodes(NodeType.Paragraph, true).ToArray(); foreach (Paragraph paragraph in paragraphs) { // We are interested in only Paragraphs with Heading 1 style if (paragraph.ParagraphFormat.Style.Name == "Heading 1") { // Filter Heading 1 paras and find one that contains the search string if (paragraph.Range.Text.StartsWith("Themes")) { isParagraphFound = true; // We need to delete all nodes present in between the startPara node // and the next Paragraph with Heading 1 Paragraph startPara = paragraph; nodesToBeDeleted.Add(startPara); startPara = startPara.NextSibling as Paragraph; var styleName = startPara.ParagraphFormat.Style.Name; while (startPara.ParagraphFormat.Style.Name == styleName) { nodesToBeDeleted.Add(startPara); startPara = startPara.NextSibling as Paragraph; } //do //{ // nodesToBeDeleted.Add(startPara); // startPara = startPara.NextSibling as Paragraph; //} //while (startPara.ParagraphFormat.Style.Name != "Heading 1"); //while (startPara.ParagraphFormat.Style.Name != "Heading 1") //{ // nodesToBeDeleted.Add(startPara); // startPara = startPara.NextSibling as Paragraph; //} //!startPara.ParagraphFormat.StyleIdentifier.Equals(StyleIdentifier.Heading1) // Break the main loop as we got the list of nodes to be removed if (isParagraphFound) { break; } } } } // Remove all nodes foreach (Node node in nodesToBeDeleted) { node.Remove(); } // Re-build the TOC field doc.UpdateFields(); // Save the final document doc.Save(@"C:\Users\Ghany Cahyadi\Desktop\Res\Coba3.docx"); return(Json(new { Status = true }, JsonRequestBehavior.AllowGet)); }