diff --git a/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart.sln b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart.sln new file mode 100644 index 00000000..008c62c5 --- /dev/null +++ b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart.sln @@ -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 diff --git a/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Insert-caption-to-chart.csproj b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Insert-caption-to-chart.csproj new file mode 100644 index 00000000..99827a3d --- /dev/null +++ b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Insert-caption-to-chart.csproj @@ -0,0 +1,17 @@ + + + + Exe + net8.0 + Insert_caption_to_chart + + + + + + + + Always + + + diff --git a/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Output/.gitkeep b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Output/.gitkeep new file mode 100644 index 00000000..5f282702 --- /dev/null +++ b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Output/.gitkeep @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Output/Result.docx b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Output/Result.docx new file mode 100644 index 00000000..fdec96fa Binary files /dev/null and b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Output/Result.docx differ diff --git a/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Program.cs b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Program.cs new file mode 100644 index 00000000..87dabbe5 --- /dev/null +++ b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Program.cs @@ -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); + } + } + } + /// + /// Add caption to the chart. + /// + 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); + } + } + /// + /// This methode is used to get the index of the paragraph. + /// + 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; + } + /// + /// Apply formattings for image caption paragraph + /// + 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; + } + } +}