Skip to content

Commit f95c6e3

Browse files
Added sample
1 parent 93e3e6e commit f95c6e3

5 files changed

Lines changed: 197 additions & 0 deletions

File tree

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 16
4+
VisualStudioVersion = 16.0.30804.86
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert-caption-to-chart", "Insert-caption-to-chart\Insert-caption-to-chart.csproj", "{E214E65C-980A-46F5-8207-6D02212A49F5}"
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+
{E214E65C-980A-46F5-8207-6D02212A49F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{E214E65C-980A-46F5-8207-6D02212A49F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{E214E65C-980A-46F5-8207-6D02212A49F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{E214E65C-980A-46F5-8207-6D02212A49F5}.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 = {404A7F36-0167-4219-AE4D-2B0626CEB7E3}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Insert_caption_to_chart</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<None Update="Output\.gitkeep">
14+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Binary file not shown.
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
using System.IO;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
using Syncfusion.OfficeChart;
5+
6+
namespace Insert_caption_to_chart
7+
{
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
//Create a new Word document.
13+
using (WordDocument document = new WordDocument())
14+
{
15+
//Add a section to the document.
16+
IWSection section = document.AddSection();
17+
//Add a paragraph to the section.
18+
IWParagraph paragraph = section.AddParagraph();
19+
//Create and append the chart to the paragraph.
20+
WChart chart = paragraph.AppendChart(446, 270);
21+
//Set chart type.
22+
chart.ChartType = OfficeChartType.Line;
23+
//Set chart data.
24+
chart.ChartData.SetValue(1, 1, "Fruits");
25+
chart.ChartData.SetValue(2, 1, "Apples");
26+
chart.ChartData.SetValue(3, 1, "Grapes");
27+
chart.ChartData.SetValue(4, 1, "Bananas");
28+
chart.ChartData.SetValue(5, 1, "Oranges");
29+
chart.ChartData.SetValue(6, 1, "Melons");
30+
chart.ChartData.SetValue(1, 2, "Joey");
31+
chart.ChartData.SetValue(2, 2, 5);
32+
chart.ChartData.SetValue(3, 2, 4);
33+
chart.ChartData.SetValue(4, 2, 4);
34+
chart.ChartData.SetValue(5, 2, 2);
35+
chart.ChartData.SetValue(6, 2, 2);
36+
chart.ChartData.SetValue(1, 3, "Matthew");
37+
chart.ChartData.SetValue(2, 3, 3);
38+
chart.ChartData.SetValue(3, 3, 5);
39+
chart.ChartData.SetValue(4, 3, 4);
40+
chart.ChartData.SetValue(5, 3, 1);
41+
chart.ChartData.SetValue(6, 3, 7);
42+
chart.ChartData.SetValue(1, 4, "Peter");
43+
chart.ChartData.SetValue(2, 4, 2);
44+
chart.ChartData.SetValue(3, 4, 2);
45+
chart.ChartData.SetValue(4, 4, 3);
46+
chart.ChartData.SetValue(5, 4, 5);
47+
chart.ChartData.SetValue(6, 4, 6);
48+
//Set region of Chart data.
49+
chart.DataRange = chart.ChartData[1, 1, 6, 4];
50+
//Set chart series in the column for assigned data region.
51+
chart.IsSeriesInRows = false;
52+
//Set a Chart Title.
53+
chart.ChartTitle = "Line Chart";
54+
//Set Datalabels.
55+
IOfficeChartSerie series1 = chart.Series[0];
56+
IOfficeChartSerie series2 = chart.Series[1];
57+
IOfficeChartSerie series3 = chart.Series[2];
58+
59+
series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
60+
series2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
61+
series3.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
62+
//Set legend.
63+
chart.HasLegend = true;
64+
chart.Legend.Position = OfficeLegendPosition.Bottom;
65+
//Mention caption text here.
66+
string captionName = "Figure";
67+
//Add caption to the chart.
68+
AddCaptionToChart(chart, captionName, CaptionNumberingFormat.Number, CaptionPosition.AfterImage);
69+
//Update fields in the Word document.
70+
document.UpdateDocumentFields();
71+
//Create a file stream.
72+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
73+
{
74+
//Save the Word document to the file stream.
75+
document.Save(outputFileStream, FormatType.Docx);
76+
}
77+
}
78+
}
79+
/// <summary>
80+
/// Add caption to the chart.
81+
/// </summary>
82+
public static void AddCaptionToChart(WChart chart, string captionName, CaptionNumberingFormat format, CaptionPosition captionPosition)
83+
{
84+
IWParagraph ownerParagraph = chart.OwnerParagraph;
85+
WTextBody body = (ownerParagraph.Owner as WTextBody);
86+
WParagraph paragraph = null;
87+
if (body != null)
88+
{
89+
//Get the index of the owner paragraph.
90+
int index = GetIndexInOwnerCollection(ownerParagraph);
91+
paragraph = new WParagraph(chart.Document);
92+
paragraph.AppendText(captionName + " ");
93+
captionName = captionName.Replace(" ", "_");
94+
paragraph.ApplyStyle(BuiltinStyle.Caption);
95+
WSeqField field = (WSeqField)paragraph.AppendField(captionName, FieldType.FieldSequence);
96+
field.NumberFormat = format;
97+
int chartIndex = ownerParagraph.Items.IndexOf(chart);
98+
99+
// Set needed formatting and paragraph location dependently on captionPosition value
100+
if (captionPosition == CaptionPosition.AboveImage)
101+
{
102+
paragraph.ParagraphFormat.KeepFollow = true;
103+
int captionIndex = (chartIndex == 0) ? index : index + 1;
104+
105+
body.ChildEntities.Insert(captionIndex, paragraph);
106+
107+
if (chartIndex > 0)
108+
{
109+
ownerParagraph.Items.RemoveAt(chartIndex);
110+
WParagraph newParagraph = new WParagraph(chart.Document);
111+
newParagraph.Items.Insert(0, chart);
112+
body.ChildEntities.Insert(captionIndex + 1, newParagraph);
113+
}
114+
}
115+
else
116+
{
117+
ownerParagraph.ParagraphFormat.KeepFollow = true;
118+
body.ChildEntities.Insert(index + 1, paragraph);
119+
}
120+
ApplyFormattingForCaption(paragraph);
121+
}
122+
}
123+
/// <summary>
124+
/// This methode is used to get the index of the paragraph.
125+
/// </summary>
126+
public static int GetIndexInOwnerCollection(IWParagraph ownerParagraph)
127+
{
128+
129+
ICompositeEntity composite = ownerParagraph.Owner as ICompositeEntity;
130+
131+
if (composite != null)
132+
{
133+
return composite.ChildEntities.IndexOf(ownerParagraph);
134+
}
135+
//If item is inside inline content control.
136+
else if (ownerParagraph is InlineContentControl)
137+
return (ownerParagraph as InlineContentControl).ParagraphItems.IndexOf(ownerParagraph);
138+
139+
return -1;
140+
}
141+
/// <summary>
142+
/// Apply formattings for image caption paragraph
143+
/// </summary>
144+
public static void ApplyFormattingForCaption(WParagraph paragraph)
145+
{
146+
//Align the caption
147+
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
148+
//Sets after spacing
149+
paragraph.ParagraphFormat.AfterSpacing = 1.5f;
150+
//Sets before spacing
151+
paragraph.ParagraphFormat.BeforeSpacing = 1.5f;
152+
}
153+
}
154+
}

0 commit comments

Comments
 (0)