Skip to content

Commit a08e712

Browse files
Merge pull request #381 from SyncfusionExamples/ES-842031-Convert-chart-to-image-and-replace
ES-842031- Add the sample Convert-chart-to-image-and-replace
2 parents fb5952b + 39e102b commit a08e712

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-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 16
4+
VisualStudioVersion = 16.0.31911.196
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Convert-chart-to-image-and-replace", "Convert-chart-to-image-and-replace\Convert-chart-to-image-and-replace.csproj", "{C17B90BC-F559-456B-B189-90B53FF6CDD4}"
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+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.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 = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Convert_chart_to_image_and_replace</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<None Update="Data\Template.docx">
15+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
16+
</None>
17+
<None Update="Output\.gitkeep">
18+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
19+
</None>
20+
</ItemGroup>
21+
22+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using Syncfusion.DocIORenderer;
4+
using System.Collections.Generic;
5+
using System.IO;
6+
7+
namespace Convert_chart_to_image_and_replace
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
//Open the file as Stream.
14+
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
15+
{
16+
//Load file stream into Word document.
17+
using (WordDocument wordDocument = new WordDocument(fileStream, FormatType.Docx))
18+
{
19+
List<Entity> charts = wordDocument.FindAllItemsByProperty(EntityType.Chart, null, null);
20+
for (int i=0; i<charts.Count; i++)
21+
{
22+
WChart chart = (WChart)charts[i];
23+
//Get owner paragraph of chart.
24+
WParagraph paragraph = chart.OwnerParagraph;
25+
//Get index of the chart in the paragraph.
26+
int chartIndex = paragraph.ChildEntities.IndexOf(chart);
27+
//Create an instance of DocIORenderer.
28+
using (DocIORenderer renderer = new DocIORenderer())
29+
{
30+
//Convert chart to an image.
31+
using (Stream stream = chart.SaveAsImage())
32+
{
33+
//Create an instance of WPicture.
34+
WPicture picture = new WPicture(wordDocument);
35+
//Load image from stream.
36+
picture.LoadImage((stream as MemoryStream).ToArray());
37+
//Set width and height of the image.
38+
picture.Width = chart.Width;
39+
picture.Height = chart.Height;
40+
//Add image to the paragraph at chart index.
41+
if (chartIndex < paragraph.ChildEntities.Count)
42+
paragraph.ChildEntities.Insert(chartIndex, picture);
43+
//Remove chart from the paragraph.
44+
paragraph.ChildEntities.Remove(chart);
45+
}
46+
}
47+
}
48+
49+
//Create a file stream.
50+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
51+
{
52+
//Save the Word document to the file stream.
53+
wordDocument.Save(outputFileStream, FormatType.Docx);
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)