Skip to content

Commit 7fc7940

Browse files
Merge pull request #192 from SyncfusionExamples/607514-To_Add_sample_Replace_picture_content_control_with_chart
Replace-picture-content-control-with-chart
2 parents 6fb95f0 + ecbb1d3 commit 7fc7940

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed
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 17
4+
VisualStudioVersion = 17.9.34622.214
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Replace-picture-content-control-with-chart", "Replace-picture-content-control-with-chart\Replace-picture-content-control-with-chart.csproj", "{3F42250C-E397-40D5-9F0B-AF60BE03A221}"
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+
{3F42250C-E397-40D5-9F0B-AF60BE03A221}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{3F42250C-E397-40D5-9F0B-AF60BE03A221}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{3F42250C-E397-40D5-9F0B-AF60BE03A221}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{3F42250C-E397-40D5-9F0B-AF60BE03A221}.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 = {A3DDA0C7-80BA-4659-93A8-3C3DD692D5AB}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.OfficeChart;
5+
6+
7+
// Open the file as stream.
8+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Data/Input.docx"), FileMode.Open))
9+
{
10+
// Load file stream into Word document.
11+
using (WordDocument document = new WordDocument(docStream, Syncfusion.DocIO.FormatType.Automatic))
12+
{
13+
string[] propertyNames = { "ContentControlProperties.Title", "ContentControlProperties.Type" };
14+
string[] propertyValues = { "Chart", "Picture" };
15+
// Find BlockContentControl by given properties
16+
BlockContentControl contentControl = document.FindItemByProperties(EntityType.BlockContentControl, propertyNames, propertyValues) as BlockContentControl;
17+
18+
if (contentControl != null)
19+
{
20+
int index = contentControl.OwnerTextBody.ChildEntities.IndexOf(contentControl);
21+
22+
// Create a new paragraph to hold the chart image.
23+
WParagraph paragraph = new WParagraph(document);
24+
25+
// Generate the chart and get it as an image stream.
26+
Stream chartImage = GenerateChartAndGetChartAsImage();
27+
28+
// Create a new image instance and load the chart image.
29+
WPicture picture = (WPicture)paragraph.AppendPicture(chartImage);
30+
31+
// Set picture dimensions.
32+
picture.Height = 300;
33+
picture.Width = 300;
34+
35+
// To replace Picture content Control insert Picture paragraph and remove the content control.
36+
contentControl.OwnerTextBody.ChildEntities.Insert(index, paragraph);
37+
contentControl.OwnerTextBody.ChildEntities.RemoveAt(index + 1);
38+
}
39+
40+
41+
// Create file stream for output document.
42+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Data/Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
43+
{
44+
// Save the Word document to the file stream.
45+
document.Save(outputFileStream, FormatType.Docx);
46+
}
47+
}
48+
}
49+
50+
51+
/// <summary>
52+
/// Generates a chart and saves it as an image.
53+
/// </summary>
54+
/// <returns>A stream containing the chart image.</returns>
55+
static Stream GenerateChartAndGetChartAsImage()
56+
{
57+
// Create a new Word document.
58+
using (WordDocument document = new WordDocument())
59+
{
60+
// Add a section to the document.
61+
IWSection section = document.AddSection();
62+
// Add a paragraph to the section.
63+
IWParagraph paragraph = section.AddParagraph();
64+
// Create and append the chart to the paragraph.
65+
WChart chart = paragraph.AppendChart(446, 270);
66+
67+
// Set chart data.
68+
chart.ChartData.SetValue(1, 1, "Month");
69+
chart.ChartData.SetValue(2, 1, "Jan");
70+
chart.ChartData.SetValue(3, 1, "Feb");
71+
chart.ChartData.SetValue(4, 1, "Mar");
72+
chart.ChartData.SetValue(5, 1, "Apr");
73+
chart.ChartData.SetValue(6, 1, "May");
74+
chart.ChartData.SetValue(7, 1, "Jun");
75+
chart.ChartData.SetValue(8, 1, "Jul");
76+
chart.ChartData.SetValue(9, 1, "Aug");
77+
chart.ChartData.SetValue(10, 1, "Sep");
78+
chart.ChartData.SetValue(11, 1, "Oct");
79+
chart.ChartData.SetValue(12, 1, "Nov");
80+
chart.ChartData.SetValue(13, 1, "Dec");
81+
chart.ChartData.SetValue(1, 2, "Rainy Days");
82+
chart.ChartData.SetValue(2, 2, 12);
83+
chart.ChartData.SetValue(3, 2, 11);
84+
chart.ChartData.SetValue(4, 2, 10);
85+
chart.ChartData.SetValue(5, 2, 9);
86+
chart.ChartData.SetValue(6, 2, 8);
87+
chart.ChartData.SetValue(7, 2, 6);
88+
chart.ChartData.SetValue(8, 2, 4);
89+
chart.ChartData.SetValue(9, 2, 6);
90+
chart.ChartData.SetValue(10, 2, 7);
91+
chart.ChartData.SetValue(11, 2, 8);
92+
chart.ChartData.SetValue(12, 2, 10);
93+
chart.ChartData.SetValue(13, 2, 11);
94+
chart.ChartData.SetValue(1, 3, "Profit");
95+
chart.ChartData.SetValue(2, 3, 3574);
96+
chart.ChartData.SetValue(3, 3, 4708);
97+
chart.ChartData.SetValue(4, 3, 5332);
98+
chart.ChartData.SetValue(5, 3, 6693);
99+
chart.ChartData.SetValue(6, 3, 8843);
100+
chart.ChartData.SetValue(7, 3, 12347);
101+
chart.ChartData.SetValue(8, 3, 15180);
102+
chart.ChartData.SetValue(9, 3, 11198);
103+
chart.ChartData.SetValue(10, 3, 9739);
104+
chart.ChartData.SetValue(11, 3, 9846);
105+
chart.ChartData.SetValue(12, 3, 6620);
106+
chart.ChartData.SetValue(13, 3, 5085);
107+
108+
// Set region of chart data.
109+
chart.DataRange = chart.ChartData[1, 1, 13, 3];
110+
// Set chart series in the column for assigned data region.
111+
chart.IsSeriesInRows = false;
112+
// Set chart title.
113+
chart.ChartTitle = "Combination Chart";
114+
115+
// Set data labels.
116+
IOfficeChartSerie series1 = chart.Series[0];
117+
IOfficeChartSerie series2 = chart.Series[1];
118+
// Set series type.
119+
series1.SerieType = OfficeChartType.Column_Clustered;
120+
series2.SerieType = OfficeChartType.Line;
121+
series2.UsePrimaryAxis = false;
122+
123+
// Set data labels.
124+
series1.DataPoints.DefaultDataPoint.DataLabels.IsValue = true;
125+
126+
// Set legend.
127+
chart.HasLegend = true;
128+
chart.Legend.Position = OfficeLegendPosition.Bottom;
129+
130+
// Set chart type.
131+
chart.ChartType = OfficeChartType.Combination_Chart;
132+
133+
// Set secondary axis on right side.
134+
chart.SecondaryValueAxis.TickLabelPosition = OfficeTickLabelPosition.TickLabelPosition_High;
135+
136+
// Create an instance of DocIORenderer.
137+
using (DocIORenderer renderer = new DocIORenderer())
138+
{
139+
// Convert chart to an image.
140+
Stream stream = chart.SaveAsImage();
141+
return stream;
142+
}
143+
}
144+
}
145+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<RootNamespace>Replace_picture_content_control_with_chart</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>

0 commit comments

Comments
 (0)