Skip to content

ES-842031- Add the sample Convert-chart-to-image-and-replace #381

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
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.31911.196
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C17B90BC-F559-456B-B189-90B53FF6CDD4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {EF357FC6-E9E5-4E3C-B932-43F727BE1DE4}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Convert_chart_to_image_and_replace</RootNamespace>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Data\Template.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Output\.gitkeep">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIORenderer;
using System.Collections.Generic;
using System.IO;

namespace Convert_chart_to_image_and_replace
{
class Program
{
static void Main(string[] args)
{
//Open the file as Stream.
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open))
{
//Load file stream into Word document.
using (WordDocument wordDocument = new WordDocument(fileStream, FormatType.Docx))
{
List<Entity> charts = wordDocument.FindAllItemsByProperty(EntityType.Chart, null, null);
for (int i=0; i<charts.Count; i++)
{
WChart chart = (WChart)charts[i];
//Get owner paragraph of chart.
WParagraph paragraph = chart.OwnerParagraph;
//Get index of the chart in the paragraph.
int chartIndex = paragraph.ChildEntities.IndexOf(chart);
//Create an instance of DocIORenderer.
using (DocIORenderer renderer = new DocIORenderer())
{
//Convert chart to an image.
using (Stream stream = chart.SaveAsImage())
{
//Create an instance of WPicture.
WPicture picture = new WPicture(wordDocument);
//Load image from stream.
picture.LoadImage((stream as MemoryStream).ToArray());
//Set width and height of the image.
picture.Width = chart.Width;
picture.Height = chart.Height;
//Add image to the paragraph at chart index.
if (chartIndex < paragraph.ChildEntities.Count)
paragraph.ChildEntities.Insert(chartIndex, picture);
//Remove chart from the paragraph.
paragraph.ChildEntities.Remove(chart);
}
}
}

//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.
wordDocument.Save(outputFileStream, FormatType.Docx);
}
}
}
}
}
}
Loading