/// <summary> /// Add a Table of content to a document by inserting it just before a reference paragraph. /// </summary> public static void InsertTableOfContentWithReference() { Console.WriteLine("\tInsertTableOfContentWithReference()"); // Create a document. using (DocX document = DocX.Create(TableOfContentSample.TableOfContentSampleOutputDirectory + @"InsertTableOfContentWithReference.docx")) { // Add a title. document.InsertParagraph("Insert Table of content with reference").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center; // Add an intro paragraph. var intro = document.InsertParagraph("This page will show the team rosters of the American League East Division."); intro.SpacingAfter(150d); // Create a paragraph and fill it in method AddTeams(). var p = document.InsertParagraph(); var rosters = TableOfContentSample.AddTeams(p); document.InsertParagraph(rosters); // Insert a table of content with a page break just before the paragraph p. document.InsertTableOfContents(p, "Teams", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading4"); document.Save(); Console.WriteLine("\tCreated: InsertTableOfContentWithReference.docx\n"); } }
/// <summary> /// 根据书签地址生成目录 /// </summary> /// <param name="document"></param> /// <param name="markname"></param> static public void AddtocByBookmark(DocX document, string markname) { try { document.InsertTableOfContents(document.Bookmarks[markname].Paragraph, "目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2", 5); } catch (System.Exception ex) { LogHelper.WriteLog(typeof(tocHelper), ex); } }
/// <summary> /// 添加目录 /// </summary> /// <param name="document"></param> void Addtoc(DocX document) { try { document.InsertTableOfContents("目录", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading3"); } catch (System.Exception ex) { LogHelper.WriteLog(typeof(Program), ex); } }
public override TemplMatchText Handler(DocX doc, object model, TemplMatchText m) { if (m.Removed) { return(m); } doc.InsertTableOfContents(m.Paragraph, m.Body, Switches); /* Additional options: * string headerStyle = null * int maxIncludeLevel = 3 * int? rightTabPos = null) */ m.Expired = true; return(m); }
/// <summary> /// btnGenerate_Click event handler /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGenerate_Click(object sender, EventArgs e) { string FilePath = SaveDialog();//Gets the save location if (FilePath != "") { //Replace the picture bookmarks with pictures InsertPictures(); //Replace the text bookmarks with data InsertText(); //Create a table of context document.Bookmarks["bmkTableOfContext"].SetText(""); document.InsertTableOfContents(document.Bookmarks["bmkTableOfContext"].Paragraph, "Table of context", TableOfContentsSwitches.H, "Normal", 2, null); //Save the report Report.SaveDoc(document, FilePath); } }
/// <summary> /// 添加目录 /// </summary> /// <param name="document"></param> public virtual void Addtoc(DocX document) { try { Formatting f = new Formatting(); f.Size = 22; Paragraph p = document.InsertParagraph("目 录", false, f).Bold(); p.Alignment = Alignment.center; Paragraph toc = document.InsertParagraph(); document.InsertTableOfContents(toc, "", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H, "Heading2", 4); toc.InsertPageBreakAfterSelf(); } catch (System.Exception ex) { LogHelper.WriteLog(typeof(CreateCompany), ex); } }
/// <summary> /// Add a Table of content to a document. /// </summary> public static void InsertTableOfContent() { Console.WriteLine("\tInsertTableOfContent()"); // Creates a document using (DocX document = DocX.Create(TableOfContentSample.TableOfContentSampleOutputDirectory + @"InsertTableOfContent.docx")) { // Add a title document.InsertParagraph("Insert Table of content").FontSize(15d).SpacingAfter(50d).Alignment = Alignment.center; // Insert a table of content with a page break. document.InsertTableOfContents("Teams", TableOfContentsSwitches.O | TableOfContentsSwitches.U | TableOfContentsSwitches.Z | TableOfContentsSwitches.H); document.InsertSectionPageBreak(); // Create a paragraph and fill it in method AddTeams(). var p = document.InsertParagraph(); var rosters = TableOfContentSample.AddTeams(p); document.InsertParagraph(rosters); document.Save(); Console.WriteLine("\tCreated: InsertTableOfContent.docx\n"); } }