Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30804.86
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert-caption-to-chart", "Insert-caption-to-chart\Insert-caption-to-chart.csproj", "{E214E65C-980A-46F5-8207-6D02212A49F5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E214E65C-980A-46F5-8207-6D02212A49F5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E214E65C-980A-46F5-8207-6D02212A49F5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E214E65C-980A-46F5-8207-6D02212A49F5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E214E65C-980A-46F5-8207-6D02212A49F5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {404A7F36-0167-4219-AE4D-2B0626CEB7E3}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Insert_caption_to_chart</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
</ItemGroup>
<ItemGroup>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
154 changes: 154 additions & 0 deletions Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
using System.IO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.OfficeChart;

namespace Insert_caption_to_chart
{
class Program
{
static void Main(string[] args)
{
//Create a new Word document.
using (WordDocument document = new WordDocument())
{
//Add a section to the document.
IWSection section = document.AddSection();
//Add a paragraph to the section.
IWParagraph paragraph = section.AddParagraph();
//Create and append the chart to the paragraph.
WChart chart = paragraph.AppendChart(446, 270);
//Set chart type.
chart.ChartType = OfficeChartType.Line;
//Set chart data.
chart.ChartData.SetValue(1, 1, "Fruits");
chart.ChartData.SetValue(2, 1, "Apples");
chart.ChartData.SetValue(3, 1, "Grapes");
chart.ChartData.SetValue(4, 1, "Bananas");
chart.ChartData.SetValue(5, 1, "Oranges");
chart.ChartData.SetValue(6, 1, "Melons");
chart.ChartData.SetValue(1, 2, "Joey");
chart.ChartData.SetValue(2, 2, 5);
chart.ChartData.SetValue(3, 2, 4);
chart.ChartData.SetValue(4, 2, 4);
chart.ChartData.SetValue(5, 2, 2);
chart.ChartData.SetValue(6, 2, 2);
chart.ChartData.SetValue(1, 3, "Matthew");
chart.ChartData.SetValue(2, 3, 3);
chart.ChartData.SetValue(3, 3, 5);
chart.ChartData.SetValue(4, 3, 4);
chart.ChartData.SetValue(5, 3, 1);
chart.ChartData.SetValue(6, 3, 7);
chart.ChartData.SetValue(1, 4, "Peter");
chart.ChartData.SetValue(2, 4, 2);
chart.ChartData.SetValue(3, 4, 2);
chart.ChartData.SetValue(4, 4, 3);
chart.ChartData.SetValue(5, 4, 5);
chart.ChartData.SetValue(6, 4, 6);
//Set region of Chart data.
chart.DataRange = chart.ChartData[1, 1, 6, 4];
//Set chart series in the column for assigned data region.
chart.IsSeriesInRows = false;
//Set a Chart Title.
chart.ChartTitle = "Line Chart";
//Set Datalabels.
IOfficeChartSerie series1 = chart.Series[0];
IOfficeChartSerie series2 = chart.Series[1];
IOfficeChartSerie series3 = chart.Series[2];

series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
series2.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
series3.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
//Set legend.
chart.HasLegend = true;
chart.Legend.Position = OfficeLegendPosition.Bottom;
//Mention caption text here.
string captionName = "Figure";
//Add caption to the chart.
AddCaptionToChart(chart, captionName, CaptionNumberingFormat.Number, CaptionPosition.AfterImage);
//Update fields in the Word document.
document.UpdateDocumentFields();
//Create a file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the Word document to the file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
/// <summary>
/// Add caption to the chart.
/// </summary>
public static void AddCaptionToChart(WChart chart, string captionName, CaptionNumberingFormat format, CaptionPosition captionPosition)
{
IWParagraph ownerParagraph = chart.OwnerParagraph;
WTextBody body = (ownerParagraph.Owner as WTextBody);
WParagraph paragraph = null;
if (body != null)
{
//Get the index of the owner paragraph.
int index = GetIndexInOwnerCollection(ownerParagraph);
paragraph = new WParagraph(chart.Document);
paragraph.AppendText(captionName + " ");
captionName = captionName.Replace(" ", "_");
paragraph.ApplyStyle(BuiltinStyle.Caption);
WSeqField field = (WSeqField)paragraph.AppendField(captionName, FieldType.FieldSequence);
field.NumberFormat = format;
int chartIndex = ownerParagraph.Items.IndexOf(chart);

// Set needed formatting and paragraph location dependently on captionPosition value
if (captionPosition == CaptionPosition.AboveImage)
{
paragraph.ParagraphFormat.KeepFollow = true;
int captionIndex = (chartIndex == 0) ? index : index + 1;

body.ChildEntities.Insert(captionIndex, paragraph);

if (chartIndex > 0)
{
ownerParagraph.Items.RemoveAt(chartIndex);
WParagraph newParagraph = new WParagraph(chart.Document);
newParagraph.Items.Insert(0, chart);
body.ChildEntities.Insert(captionIndex + 1, newParagraph);
}
}
else
{
ownerParagraph.ParagraphFormat.KeepFollow = true;
body.ChildEntities.Insert(index + 1, paragraph);
}
ApplyFormattingForCaption(paragraph);
}
}
/// <summary>
/// This methode is used to get the index of the paragraph.
/// </summary>
public static int GetIndexInOwnerCollection(IWParagraph ownerParagraph)
{

ICompositeEntity composite = ownerParagraph.Owner as ICompositeEntity;

if (composite != null)
{
return composite.ChildEntities.IndexOf(ownerParagraph);
}
//If item is inside inline content control.
else if (ownerParagraph is InlineContentControl)
return (ownerParagraph as InlineContentControl).ParagraphItems.IndexOf(ownerParagraph);

return -1;
}
/// <summary>
/// Apply formattings for image caption paragraph
/// </summary>
public static void ApplyFormattingForCaption(WParagraph paragraph)
{
//Align the caption
paragraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center;
//Sets after spacing
paragraph.ParagraphFormat.AfterSpacing = 1.5f;
//Sets before spacing
paragraph.ParagraphFormat.BeforeSpacing = 1.5f;
}
}
}
Loading