Skip to content

Commit 76e5f18

Browse files
Merge pull request #168 from Venkateshmuruganandam/main
Sample added for Table of contents
2 parents c416d1d + 31f2871 commit 76e5f18

File tree

8 files changed

+260
-0
lines changed

8 files changed

+260
-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.9.34414.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Exclude_caption_label_numbers", "Exclude_caption_label_numbers\Exclude_caption_label_numbers.csproj", "{9B4F9924-3FFA-4164-A893-89833C0C274E}"
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+
{9B4F9924-3FFA-4164-A893-89833C0C274E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{9B4F9924-3FFA-4164-A893-89833C0C274E}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{9B4F9924-3FFA-4164-A893-89833C0C274E}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{9B4F9924-3FFA-4164-A893-89833C0C274E}.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 = {A725E3E4-4C85-49AA-822C-F1946E868001}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
12+
</ItemGroup>
13+
</Project>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using System.IO;
5+
6+
7+
//Load an existing Word document.
8+
using FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read);
9+
using WordDocument document = new WordDocument(fileStream, FormatType.Docx);
10+
WParagraph paragraph = new WParagraph(document);
11+
paragraph.AppendText("List of Figures");
12+
//Apply Heading1 style for paragraph.
13+
paragraph.ApplyStyle(BuiltinStyle.Heading1);
14+
//Insert the paragraph
15+
document.LastSection.Body.ChildEntities.Insert(0, paragraph);
16+
//Create new paragraph and append TOC
17+
paragraph = new WParagraph(document);
18+
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
19+
//Disable a flag to exclude heading style paragraphs in TOC entries.
20+
tableOfContent.UseHeadingStyles = false;
21+
//Set the name of SEQ field identifier for table of figures.
22+
tableOfContent.TableOfFiguresLabel = "Figure";
23+
//Disable the flag, to exclude caption's label and number in TOC entries.
24+
tableOfContent.IncludeCaptionLabelsAndNumbers = false;
25+
//Insert the paragraph to the text body.
26+
document.LastSection.Body.ChildEntities.Insert(1, paragraph);
27+
28+
//Find all pictures from the document
29+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
30+
// Iterate each picture and add caption.
31+
foreach (WPicture picture in pictures)
32+
{
33+
//Set alternate text as caption for picture.
34+
WParagraph captionPara = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage) as WParagraph;
35+
captionPara.AppendText(" " + picture.AlternativeText);
36+
//Apply formatting to the caption.
37+
captionPara.ApplyStyle(BuiltinStyle.Caption);
38+
captionPara.ParagraphFormat.BeforeSpacing = 8;
39+
captionPara.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
40+
}
41+
42+
// Create a new paragraph
43+
paragraph = new WParagraph(document);
44+
paragraph.AppendText("List of Tables");
45+
// Apply Heading1 style for paragraph.
46+
paragraph.ApplyStyle(BuiltinStyle.Heading1);
47+
// Insert the paragraph
48+
document.LastSection.Body.ChildEntities.Insert(2, paragraph);
49+
50+
//Create a new paragraph and append TOC.
51+
paragraph = new WParagraph(document);
52+
tableOfContent = paragraph.AppendTOC(1, 3);
53+
//Disable a flag to exclude heading style paragraphs in TOC entries.
54+
tableOfContent.UseHeadingStyles = false;
55+
//Set the name of SEQ field identifier for table of tables.
56+
tableOfContent.TableOfFiguresLabel = "Table";
57+
//Disable the flag, to exclude caption's label and number in TOC entries.
58+
tableOfContent.IncludeCaptionLabelsAndNumbers = false;
59+
// Insert the paragraph to the text body.
60+
document.LastSection.Body.ChildEntities.Insert(3, paragraph);
61+
62+
63+
// Find all tables from the document
64+
List<Entity> tables = document.FindAllItemsByProperty(EntityType.Table, null, null);
65+
//Iterate each table and add caption.
66+
foreach (WTable table in tables)
67+
{
68+
//Gets the table index
69+
int tableIndex = table.OwnerTextBody.ChildEntities.IndexOf(table);
70+
//Create a new paragraph and appends the sequence field to use as a caption.
71+
WParagraph captionPara = new WParagraph(document);
72+
captionPara.AppendText("Table ");
73+
captionPara.AppendField("Table", FieldType.FieldSequence);
74+
//Set alternate text as caption for table.
75+
captionPara.AppendText(" " + table.Description);
76+
// Apply formatting to the paragraph
77+
captionPara.ApplyStyle(BuiltinStyle.Caption);
78+
captionPara.ParagraphFormat.BeforeSpacing = 8;
79+
captionPara.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
80+
//Insert the paragraph next to the table
81+
table.OwnerTextBody.ChildEntities.Insert(tableIndex + 1, captionPara);
82+
}
83+
84+
//Update all document fields to update SEQ fields.
85+
document.UpdateDocumentFields();
86+
//Update the table of contents.
87+
document.UpdateTableOfContents();
88+
89+
//Create a FileStream to save the Word document.
90+
using FileStream outputStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
91+
//Save the Word document.
92+
document.Save(outputStream, FormatType.Docx);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.9.34414.90
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Table_of_figures", "Table_of_figures\Table_of_figures.csproj", "{E78E0F19-7466-4A3B-9A46-58032CFB17F8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
Release-Xml|Any CPU = Release-Xml|Any CPU
13+
EndGlobalSection
14+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
15+
{E78E0F19-7466-4A3B-9A46-58032CFB17F8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
16+
{E78E0F19-7466-4A3B-9A46-58032CFB17F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
17+
{E78E0F19-7466-4A3B-9A46-58032CFB17F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
18+
{E78E0F19-7466-4A3B-9A46-58032CFB17F8}.Release|Any CPU.Build.0 = Release|Any CPU
19+
{E78E0F19-7466-4A3B-9A46-58032CFB17F8}.Release-Xml|Any CPU.ActiveCfg = Release|Any CPU
20+
{E78E0F19-7466-4A3B-9A46-58032CFB17F8}.Release-Xml|Any CPU.Build.0 = Release|Any CPU
21+
EndGlobalSection
22+
GlobalSection(SolutionProperties) = preSolution
23+
HideSolutionNode = FALSE
24+
EndGlobalSection
25+
GlobalSection(ExtensibilityGlobals) = postSolution
26+
SolutionGuid = {607316D8-5A15-4688-B9FB-67CF1A442403}
27+
EndGlobalSection
28+
EndGlobal
Binary file not shown.
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using System.IO;
5+
6+
7+
//Load an existing Word document.
8+
using FileStream fileStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open, FileAccess.Read);
9+
using WordDocument document = new WordDocument(fileStream, FormatType.Docx);
10+
WParagraph paragraph = new WParagraph(document);
11+
paragraph.AppendText("List of Figures");
12+
//Apply Heading1 style for paragraph.
13+
paragraph.ApplyStyle(BuiltinStyle.Heading1);
14+
//Insert the paragraph
15+
document.LastSection.Body.ChildEntities.Insert(0, paragraph);
16+
//Create new paragraph and append TOC
17+
paragraph = new WParagraph(document);
18+
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
19+
//Disable a flag to exclude heading style paragraphs in TOC entries.
20+
tableOfContent.UseHeadingStyles = false;
21+
//Set the name of SEQ field identifier for table of figures.
22+
tableOfContent.TableOfFiguresLabel = "Figure";
23+
//Insert the paragraph to the text body.
24+
document.LastSection.Body.ChildEntities.Insert(1, paragraph);
25+
26+
//Find all pictures from the document
27+
List<Entity> pictures = document.FindAllItemsByProperty(EntityType.Picture, null, null);
28+
// Iterate each picture and add caption.
29+
foreach (WPicture picture in pictures)
30+
{
31+
//Set alternate text as caption for picture.
32+
WParagraph captionPara = picture.AddCaption("Figure", CaptionNumberingFormat.Number, CaptionPosition.AfterImage) as WParagraph;
33+
captionPara.AppendText(" " + picture.AlternativeText);
34+
//Apply formatting to the caption.
35+
captionPara.ApplyStyle(BuiltinStyle.Caption);
36+
captionPara.ParagraphFormat.BeforeSpacing = 8;
37+
captionPara.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
38+
}
39+
40+
// Create a new paragraph
41+
paragraph = new WParagraph(document);
42+
paragraph.AppendText("List of Tables");
43+
// Apply Heading1 style for paragraph.
44+
paragraph.ApplyStyle(BuiltinStyle.Heading1);
45+
// Insert the paragraph
46+
document.LastSection.Body.ChildEntities.Insert(2, paragraph);
47+
48+
//Create a new paragraph and append TOC.
49+
paragraph = new WParagraph(document);
50+
tableOfContent = paragraph.AppendTOC(1, 3);
51+
//Disable a flag to exclude heading style paragraphs in TOC entries.
52+
tableOfContent.UseHeadingStyles = false;
53+
//Set the name of SEQ field identifier for table of tables.
54+
tableOfContent.TableOfFiguresLabel = "Table";
55+
// Insert the paragraph to the text body.
56+
document.LastSection.Body.ChildEntities.Insert(3, paragraph);
57+
58+
59+
// Find all tables from the document
60+
List<Entity> tables = document.FindAllItemsByProperty(EntityType.Table, null, null);
61+
//Iterate each table and add caption.
62+
foreach (WTable table in tables)
63+
{
64+
//Gets the table index
65+
int tableIndex = table.OwnerTextBody.ChildEntities.IndexOf(table);
66+
//Create a new paragraph and appends the sequence field to use as a caption.
67+
WParagraph captionPara = new WParagraph(document);
68+
captionPara.AppendText("Table ");
69+
captionPara.AppendField("Table", FieldType.FieldSequence);
70+
//Set alternate text as caption for table.
71+
captionPara.AppendText(" " + table.Description);
72+
// Apply formatting to the paragraph
73+
captionPara.ApplyStyle(BuiltinStyle.Caption);
74+
captionPara.ParagraphFormat.BeforeSpacing = 8;
75+
captionPara.ParagraphFormat.HorizontalAlignment = HorizontalAlignment.Center;
76+
//Insert the paragraph next to the table
77+
table.OwnerTextBody.ChildEntities.Insert(tableIndex + 1, captionPara);
78+
}
79+
80+
//Update all document fields to update SEQ fields.
81+
document.UpdateDocumentFields();
82+
//Update the table of contents.
83+
document.UpdateTableOfContents();
84+
85+
//Create a FileStream to save the Word document.
86+
using FileStream outputStream = new FileStream("Result.docx", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite);
87+
//Save the Word document.
88+
document.Save(outputStream, FormatType.Docx);
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
</Project>

0 commit comments

Comments
 (0)