|
| 1 | +using Microsoft.AspNetCore.Mvc; |
| 2 | +using Syncfusion.DocIO; |
| 3 | +using Syncfusion.DocIO.DLS; |
| 4 | +using Syncfusion.DocIORenderer; |
| 5 | + |
| 6 | +namespace Create_and_update_TOC.Data |
| 7 | +{ |
| 8 | + public class WordService |
| 9 | + { |
| 10 | + public MemoryStream CreateTOC() |
| 11 | + { |
| 12 | + using (WordDocument document = new WordDocument()) |
| 13 | + { |
| 14 | + document.EnsureMinimal(); |
| 15 | + document.LastSection.PageSetup.Margins.All = 72; |
| 16 | + WParagraph paragraph = document.LastParagraph; |
| 17 | + paragraph.AppendText("Essential DocIO - Table of Contents"); |
| 18 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 19 | + paragraph.ApplyStyle(BuiltinStyle.Heading4); |
| 20 | + paragraph = document.LastSection.AddParagraph() as WParagraph; |
| 21 | + paragraph = document.LastSection.AddParagraph() as WParagraph; |
| 22 | + |
| 23 | + TableOfContent toc = paragraph.AppendTOC(1, 3); |
| 24 | + WSection section = document.LastSection; |
| 25 | + WParagraph newPara = section.AddParagraph() as WParagraph; |
| 26 | + newPara.AppendBreak(BreakType.PageBreak); |
| 27 | + AddHeading(section, BuiltinStyle.Heading1, "Document with built-in styles", "This is the built-in heading 1 style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can insert TOC field in a word document. It can refresh or update TOC field by using UpdateTableOfContents method. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); |
| 28 | + AddHeading(section, BuiltinStyle.Heading2, "Section 1", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); |
| 29 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); |
| 30 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); |
| 31 | + |
| 32 | + section = document.AddSection() as WSection; |
| 33 | + section.PageSetup.Margins.All = 72; |
| 34 | + section.BreakCode = SectionBreakCode.NewPage; |
| 35 | + AddHeading(section, BuiltinStyle.Heading2, "Section 2", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); |
| 36 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); |
| 37 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); |
| 38 | + document.UpdateTableOfContents(); |
| 39 | + |
| 40 | + //Saves the Word document to MemoryStream. |
| 41 | + MemoryStream stream = new MemoryStream(); |
| 42 | + document.Save(stream, FormatType.Docx); |
| 43 | + stream.Position = 0; |
| 44 | + return stream; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private void AddHeading(WSection section, BuiltinStyle builtinStyle, string headingText, string paragraphText) |
| 49 | + { |
| 50 | + WParagraph paragraph = section.AddParagraph() as WParagraph; |
| 51 | + WTextRange text = paragraph.AppendText(headingText) as WTextRange; |
| 52 | + paragraph.ApplyStyle(builtinStyle); |
| 53 | + paragraph = section.AddParagraph() as WParagraph; |
| 54 | + paragraph.AppendText(paragraphText); |
| 55 | + section.AddParagraph(); |
| 56 | + } |
| 57 | + |
| 58 | + private void AddHeading(WSection section, string styleName, string headingText, string paragraphText) |
| 59 | + { |
| 60 | + WParagraph paragraph = section.AddParagraph() as WParagraph; |
| 61 | + WTextRange text = paragraph.AppendText(headingText) as WTextRange; |
| 62 | + paragraph.ApplyStyle(styleName); |
| 63 | + paragraph = section.AddParagraph() as WParagraph; |
| 64 | + paragraph.AppendText(paragraphText); |
| 65 | + section.AddParagraph(); |
| 66 | + } |
| 67 | + |
| 68 | + public MemoryStream EditTOC() |
| 69 | + { |
| 70 | + using (WordDocument document = new WordDocument()) |
| 71 | + { |
| 72 | + FileStream inputStream = new FileStream(Path.GetFullPath(@"wwwroot/TOC.docx"), FileMode.Open, FileAccess.Read); |
| 73 | + document.Open(inputStream, FormatType.Docx); |
| 74 | + inputStream.Dispose(); |
| 75 | + TableOfContent toc = document.Sections[0].Body.Paragraphs[2].Items[0] as TableOfContent; |
| 76 | + toc.LowerHeadingLevel = 1; |
| 77 | + toc.UpperHeadingLevel = 2; |
| 78 | + toc.IncludePageNumbers = false; |
| 79 | + |
| 80 | + document.UpdateTableOfContents(); |
| 81 | + //Saves the Word document to MemoryStream. |
| 82 | + MemoryStream stream = new MemoryStream(); |
| 83 | + document.Save(stream, FormatType.Docx); |
| 84 | + stream.Position = 0; |
| 85 | + return stream; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public MemoryStream CustomStyleTOC() |
| 90 | + { |
| 91 | + using (WordDocument document = new WordDocument()) |
| 92 | + { |
| 93 | + document.EnsureMinimal(); |
| 94 | + document.LastSection.PageSetup.Margins.All = 72; |
| 95 | + WParagraph para = document.LastParagraph; |
| 96 | + para.AppendText("Essential DocIO - Table of Contents"); |
| 97 | + para.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 98 | + para.ApplyStyle(BuiltinStyle.Heading4); |
| 99 | + para = document.LastSection.AddParagraph() as WParagraph; |
| 100 | + WParagraphStyle pStyle1 = (WParagraphStyle)document.AddParagraphStyle("MyStyle1"); |
| 101 | + pStyle1.CharacterFormat.FontSize = 18f; |
| 102 | + WParagraphStyle pStyle2 = (WParagraphStyle)document.AddParagraphStyle("MyStyle2"); |
| 103 | + pStyle2.CharacterFormat.FontSize = 16f; |
| 104 | + WParagraphStyle pStyle3 = (WParagraphStyle)document.AddParagraphStyle("MyStyle3"); |
| 105 | + pStyle3.CharacterFormat.FontSize = 14f; |
| 106 | + para = document.LastSection.AddParagraph() as WParagraph; |
| 107 | + |
| 108 | + TableOfContent toc = para.AppendTOC(1, 3); |
| 109 | + toc.UseHeadingStyles = false; |
| 110 | + toc.SetTOCLevelStyle(1, "MyStyle1"); |
| 111 | + toc.SetTOCLevelStyle(2, "MyStyle2"); |
| 112 | + toc.SetTOCLevelStyle(3, "MyStyle3"); |
| 113 | + WSection section = document.LastSection; |
| 114 | + WParagraph newPara = section.AddParagraph() as WParagraph; |
| 115 | + newPara.AppendBreak(BreakType.PageBreak); |
| 116 | + |
| 117 | + AddHeading(section, "MyStyle1", "Document with custom styles", "This is the 1st custom style. This sample demonstrates the TOC insertion in a Word document. Note that DocIO can insert TOC fields in a Word document. It can refresh or update the TOC field by using the UpdateTableOfContents method. MS Word refreshes the TOC field after insertion. Please update the field or press F9 to refresh the TOC."); |
| 118 | + AddHeading(section, "MyStyle2", "Section 1", "This is the 2nd custom style. A document can contain any number of sections. Sections are used to apply the same formatting to a group of paragraphs. You can insert sections by inserting section breaks."); |
| 119 | + AddHeading(section, "MyStyle3", "Paragraph 1", "This is the 3rd custom style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives meaning to the text."); |
| 120 | + AddHeading(section, "MyStyle3", "Paragraph 2", "This is the 3rd custom style. This demonstrates the paragraphs at the same level and with the same style as the previous one. A paragraph can have any kind of formatting. This can be attained by formatting each text range in the paragraph."); |
| 121 | + AddHeading(section, "Normal", "Paragraph with normal", "This is the paragraph with the Normal style. This demonstrates the paragraph with outline level 4 and the Normal style. This can be attained by formatting the outline level of the paragraph."); |
| 122 | + |
| 123 | + section = document.AddSection() as WSection; |
| 124 | + section.PageSetup.Margins.All = 72; |
| 125 | + section.BreakCode = SectionBreakCode.NewPage; |
| 126 | + AddHeading(section, "MyStyle2", "Section 2", "This is the 2nd custom style. A document can contain any number of sections. Sections are used to apply the same formatting to a group of paragraphs. You can insert sections by inserting section breaks."); |
| 127 | + AddHeading(section, "MyStyle3", "Paragraph 1", "This is the 3rd custom style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives meaning to the text."); |
| 128 | + AddHeading(section, "MyStyle3", "Paragraph 2", "This is the 3rd custom style. This demonstrates the paragraphs at the same level and with the same style as the previous one. A paragraph can have any kind of formatting. This can be attained by formatting each text range in the paragraph."); |
| 129 | + |
| 130 | + document.UpdateTableOfContents(); |
| 131 | + //Saves the Word document to MemoryStream. |
| 132 | + MemoryStream stream = new MemoryStream(); |
| 133 | + document.Save(stream, FormatType.Docx); |
| 134 | + stream.Position = 0; |
| 135 | + return stream; |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + public MemoryStream CustomTOCEntries() |
| 140 | + { |
| 141 | + using (WordDocument document = new WordDocument()) |
| 142 | + { |
| 143 | + document.EnsureMinimal(); |
| 144 | + document.LastSection.PageSetup.Margins.All = 72; |
| 145 | + WParagraph paragraph = document.LastParagraph; |
| 146 | + paragraph.AppendText("Essential DocIO - Table of Contents"); |
| 147 | + paragraph.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center; |
| 148 | + paragraph.ApplyStyle(BuiltinStyle.Heading4); |
| 149 | + paragraph = document.LastSection.AddParagraph() as WParagraph; |
| 150 | + paragraph = document.LastSection.AddParagraph() as WParagraph; |
| 151 | + |
| 152 | + TableOfContent toc = paragraph.AppendTOC(1, 3); |
| 153 | + WSection section = document.LastSection; |
| 154 | + WParagraph newPara = section.AddParagraph() as WParagraph; |
| 155 | + newPara.AppendBreak(BreakType.PageBreak); |
| 156 | + AddHeading(section, BuiltinStyle.Heading1, "Document with built-in styles", "This is the built-in heading 1 style. This sample demonstrates the TOC insertion in a word document. Note that DocIO can insert TOC field in a word document. It can refresh or update TOC field by using UpdateTableOfContents method. MS Word refreshes the TOC field after insertion. Please update the field or press F9 key to refresh the TOC."); |
| 157 | + AddHeading(section, BuiltinStyle.Heading2, "Section 1", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); |
| 158 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); |
| 159 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); |
| 160 | + |
| 161 | + section = document.AddSection() as WSection; |
| 162 | + section.PageSetup.Margins.All = 72; |
| 163 | + section.BreakCode = SectionBreakCode.NewPage; |
| 164 | + AddHeading(section, BuiltinStyle.Heading2, "Section 2", "This is the built-in heading 2 style. A document can contain any number of sections. Sections are used to apply same formatting for a group of paragraphs. You can insert sections by inserting section breaks."); |
| 165 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 1", "This is the built-in heading 3 style. Each section contains any number of paragraphs. A paragraph is a set of statements that gives a meaning for the text."); |
| 166 | + AddHeading(section, BuiltinStyle.Heading3, "Paragraph 2", "This is the built-in heading 3 style. This demonstrates the paragraphs at the same level and style as that of the previous one. A paragraph can have any number formatting. This can be attained by formatting each text range in the paragraph."); |
| 167 | + |
| 168 | + IWParagraphStyle toc1Style = document.AddParagraphStyle("TOC 1"); |
| 169 | + toc1Style.CharacterFormat.FontName = "Calibri"; |
| 170 | + toc1Style.CharacterFormat.FontSize = 14; |
| 171 | + toc1Style.CharacterFormat.Bold = true; |
| 172 | + toc1Style.CharacterFormat.Italic = true; |
| 173 | + toc1Style.ParagraphFormat.AfterSpacing = 8; |
| 174 | + |
| 175 | + IWParagraphStyle toc2style = document.AddParagraphStyle("TOC 2"); |
| 176 | + toc2style.CharacterFormat.FontName = "Calibri"; |
| 177 | + toc2style.CharacterFormat.FontSize = 12; |
| 178 | + toc2style.ParagraphFormat.AfterSpacing = 5; |
| 179 | + toc2style.CharacterFormat.Bold = true; |
| 180 | + toc2style.ParagraphFormat.LeftIndent = 11; |
| 181 | + |
| 182 | + IWParagraphStyle toc3style = document.AddParagraphStyle("TOC 3"); ; |
| 183 | + toc3style.CharacterFormat.FontName = "Calibri"; |
| 184 | + toc3style.CharacterFormat.FontSize = 12; |
| 185 | + toc3style.ParagraphFormat.AfterSpacing = 3; |
| 186 | + toc3style.CharacterFormat.Italic = true; |
| 187 | + toc3style.ParagraphFormat.LeftIndent = 22; |
| 188 | + |
| 189 | + document.UpdateTableOfContents(); |
| 190 | + |
| 191 | + //Saves the Word document to MemoryStream. |
| 192 | + MemoryStream stream = new MemoryStream(); |
| 193 | + document.Save(stream, FormatType.Docx); |
| 194 | + stream.Position = 0; |
| 195 | + return stream; |
| 196 | + } |
| 197 | + } |
| 198 | + } |
| 199 | +} |
0 commit comments