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/Data/Template.docx b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Data/Template.docx new file mode 100644 index 00000000..cfd44d15 Binary files /dev/null and b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Data/Template.docx differ 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..a101a54d 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..eed68f05 --- /dev/null +++ b/Charts/Insert-caption-to-chart/.NET/Insert-caption-to-chart/Program.cs @@ -0,0 +1,112 @@ +using System.IO; +using Syncfusion.DocIO; +using Syncfusion.DocIO.DLS; + +namespace Insert_caption_to_chart +{ + class Program + { + static void Main(string[] args) + { + // Load existing Word document. + using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Template.docx"), FileMode.Open, FileAccess.ReadWrite)) + { + // Initialize the Word document with the input file stream. + using (WordDocument document = new WordDocument(inputFileStream, FormatType.Automatic)) + { + Entity entity = document.FindItemByProperty(EntityType.Chart, null, null); + WChart chart = entity as WChart; + if (chart != null) + { + //Mention caption text here. + string captionName = "Chart"; + //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.AfterImage) + { + ownerParagraph.ParagraphFormat.KeepFollow = true; + body.ChildEntities.Insert(index + 1, paragraph); + } + else + { + 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); + } + } + 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; + } + } +}