Skip to content

Commit 0541e12

Browse files
921651-Added create and update TOC Blazor sample
1 parent f6b4450 commit 0541e12

39 files changed

+1674
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32901.215
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-and-update-TOC", "Create-and-update-TOC\Create-and-update-TOC.csproj", "{00133E37-330A-4884-8501-E9BD4245AC7F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{00133E37-330A-4884-8501-E9BD4245AC7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{00133E37-330A-4884-8501-E9BD4245AC7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{00133E37-330A-4884-8501-E9BD4245AC7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{00133E37-330A-4884-8501-E9BD4245AC7F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {7EDE62C3-7D32-4293-BDF8-8A81BA7EDC9F}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<Router AppAssembly="@typeof(App).Assembly">
2+
<Found Context="routeData">
3+
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
4+
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
5+
</Found>
6+
<NotFound>
7+
<PageTitle>Not found</PageTitle>
8+
<LayoutView Layout="@typeof(MainLayout)">
9+
<p role="alert">Sorry, there's nothing at this address.</p>
10+
</LayoutView>
11+
</NotFound>
12+
</Router>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<RootNamespace>Create_and_update_TOC</RootNamespace>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Create_and_update_TOC.Data
2+
{
3+
public class WeatherForecast
4+
{
5+
public DateTime Date { get; set; }
6+
7+
public int TemperatureC { get; set; }
8+
9+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
10+
11+
public string? Summary { get; set; }
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace Create_and_update_TOC.Data
2+
{
3+
public class WeatherForecastService
4+
{
5+
private static readonly string[] Summaries = new[]
6+
{
7+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
8+
};
9+
10+
public Task<WeatherForecast[]> GetForecastAsync(DateTime startDate)
11+
{
12+
return Task.FromResult(Enumerable.Range(1, 5).Select(index => new WeatherForecast
13+
{
14+
Date = startDate.AddDays(index),
15+
TemperatureC = Random.Shared.Next(-20, 55),
16+
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
17+
}).ToArray());
18+
}
19+
}
20+
}
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Microsoft.JSInterop;
2+
3+
namespace Create_and_update_TOC
4+
{
5+
public static class FileUtils
6+
{
7+
public static ValueTask<object> SaveAs(this IJSRuntime js, string filename, byte[] data)
8+
=> js.InvokeAsync<object>(
9+
"saveAsFile",
10+
filename,
11+
Convert.ToBase64String(data));
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
@page "/counter"
2+
3+
<PageTitle>Counter</PageTitle>
4+
5+
<h1>Counter</h1>
6+
7+
<p role="status">Current count: @currentCount</p>
8+
9+
<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>
10+
11+
@code {
12+
private int currentCount = 0;
13+
14+
private void IncrementCount()
15+
{
16+
currentCount++;
17+
}
18+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@page "/docio"
2+
@using System.IO;
3+
@using Create_and_update_TOC;
4+
@inject Create_and_update_TOC.Data.WordService service
5+
@inject Microsoft.JSInterop.IJSRuntime JS
6+
7+
<h2>Syncfusion Word (DocIO) library</h2>
8+
<p>Syncfusion Word (DocIO) library is used to create, read, edit, and convert Word files in your applications without Microsoft Office dependencies.</p>
9+
<button class="btn btn-primary" @onclick="@CreateTOC">Create TOC</button>
10+
<button class="btn btn-primary" @onclick="@EditTOC">Edit TOC</button>
11+
<button class="btn btn-primary" @onclick="@CustomStyleTOC">Custom Style TOC</button>
12+
<button class="btn btn-primary" @onclick="@CustomTOCEntries">Custom TOC entries</button>
13+
14+
@code {
15+
MemoryStream documentStream;
16+
17+
protected async void CreateTOC()
18+
{
19+
documentStream = service.CreateTOC();
20+
await JS.SaveAs("TOC.docx", documentStream.ToArray());
21+
}
22+
protected async void EditTOC()
23+
{
24+
documentStream = service.EditTOC();
25+
await JS.SaveAs("TOC-edited.docx", documentStream.ToArray());
26+
}
27+
protected async void CustomStyleTOC()
28+
{
29+
documentStream = service.CustomStyleTOC();
30+
await JS.SaveAs("TOC-customstyle.docx", documentStream.ToArray());
31+
}
32+
protected async void CustomTOCEntries()
33+
{
34+
documentStream = service.CustomTOCEntries();
35+
await JS.SaveAs("TOC-customentries.docx", documentStream.ToArray());
36+
}
37+
}

0 commit comments

Comments
 (0)