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
Binary file not shown.
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.
112 changes: 112 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,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);
}
}
}
}
/// <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.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);
}
}
/// <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