Skip to content

Commit 8baecec

Browse files
KB-900986-Create-table-of-figure-if-captions-exist
1 parent c5955e2 commit 8baecec

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-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.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Create-table-of-figure-if-captions-exist", "Create-table-of-figure-if-captions-exist\Create-table-of-figure-if-captions-exist.csproj", "{981C37EF-64E1-4914-9886-AAA5C0F8D6B6}"
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+
{981C37EF-64E1-4914-9886-AAA5C0F8D6B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{981C37EF-64E1-4914-9886-AAA5C0F8D6B6}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{981C37EF-64E1-4914-9886-AAA5C0F8D6B6}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{981C37EF-64E1-4914-9886-AAA5C0F8D6B6}.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 = {78DC923B-2AD2-4FB7-84DF-4EA6E8B3D01D}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Create_table_of_figure_if_captions_exist</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\InputDocument.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIORenderer;
4+
5+
namespace Create_table_of_figure_if_captions_exist
6+
{
7+
internal class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
// Open the existing Word document from file stream
12+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/InputDocument.docx"), FileMode.Open, FileAccess.Read))
13+
{
14+
// Load the Word document
15+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
16+
{
17+
// Define the target caption name (e.g., "Figure")
18+
string targetCaption = "Figure";
19+
20+
// Find all SEQ fields in the document with the given caption name
21+
List<Entity> seqField = document.FindAllItemsByProperty(EntityType.SeqField, "CaptionName", targetCaption);
22+
23+
// Check if any SEQ fields with the target caption exist
24+
if (seqField.Count > 0)
25+
{
26+
// Create a new paragraph for the "List of Figures" title
27+
WParagraph paragraph = new WParagraph(document);
28+
paragraph.AppendText("List of Figures");
29+
// Apply Heading1 style to the paragraph
30+
paragraph.ApplyStyle(BuiltinStyle.Heading1);
31+
// Insert the paragraph at the beginning of the document
32+
document.LastSection.Body.ChildEntities.Insert(0, paragraph);
33+
34+
// Create a new paragraph for the Table of Contents (TOC)
35+
paragraph = new WParagraph(document);
36+
// Append the TOC for figures (based on SEQ fields)
37+
TableOfContent tableOfContent = paragraph.AppendTOC(1, 3);
38+
39+
// Exclude heading style paragraphs from TOC entries
40+
tableOfContent.UseHeadingStyles = false;
41+
42+
// Set the SEQ field identifier for the table of figures (targeting "Figure")
43+
tableOfContent.TableOfFiguresLabel = "Figure";
44+
45+
// Exclude caption labels and numbers in TOC entries
46+
tableOfContent.IncludeCaptionLabelsAndNumbers = false;
47+
48+
// Insert the TOC paragraph into the document
49+
document.LastSection.Body.ChildEntities.Insert(1, paragraph);
50+
}
51+
52+
// Update the Table of Contents to reflect any changes
53+
document.UpdateTableOfContents();
54+
55+
// Save the modified document to a new file
56+
using (FileStream docStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.Write))
57+
{
58+
document.Save(docStream1, FormatType.Docx);
59+
}
60+
}
61+
}
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)