Skip to content

Commit 49442f9

Browse files
Merge branch 'SyncfusionExamples:main' into main
2 parents 03a3484 + ea2e246 commit 49442f9

File tree

413 files changed

+307431
-70
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

413 files changed

+307431
-70
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>
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.8.34322.80
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Find-and-replace-with-multiple-list-paragraphs", "Find-and-replace-with-multiple-list-paragraphs\Find-and-replace-with-multiple-list-paragraphs.csproj", "{01BA5F87-72F1-4FA7-B85D-6768675A7DF2}"
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+
{01BA5F87-72F1-4FA7-B85D-6768675A7DF2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{01BA5F87-72F1-4FA7-B85D-6768675A7DF2}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{01BA5F87-72F1-4FA7-B85D-6768675A7DF2}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{01BA5F87-72F1-4FA7-B85D-6768675A7DF2}.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 = {BD00CDA9-FEA5-4C54-B803-39A387358F68}
24+
EndGlobalSection
25+
EndGlobal
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>Find_and_replace_with_multiple_list_paragraphs</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using Syncfusion.DocIO;
2+
using Syncfusion.DocIO.DLS;
3+
using System.Reflection.Metadata;
4+
5+
6+
string[] filePaths = {"../../../Data/Heading1Items.docx","../../../Data/Heading2Items.docx"};
7+
//Open the file as Stream.
8+
using (FileStream documentStream = new FileStream("../../../Data/Input.docx", FileMode.Open, FileAccess.Read))
9+
{
10+
//Open an existing Word document.
11+
using (WordDocument document = new WordDocument(documentStream, FormatType.Docx))
12+
{
13+
for(int documentIndex = 1; documentIndex <= filePaths.Length; documentIndex++)
14+
{
15+
//String to be found.
16+
string findText = "<<Heading"+ documentIndex + "Items>>";
17+
//Find the selection in the document.
18+
TextSelection selection = document.Find(findText, false, false);
19+
//Get the owner paragraph.
20+
WParagraph ownerPara = selection.GetAsOneRange().OwnerParagraph;
21+
//Open the file as Stream.
22+
using (FileStream subDocumentStream = new FileStream(filePaths[documentIndex-1], FileMode.Open, FileAccess.Read))
23+
{
24+
//Open an sub Word document.
25+
using (WordDocument subDocument = new WordDocument(subDocumentStream, FormatType.Docx))
26+
{
27+
//Create a text body part to be replaced.
28+
TextBodyPart textBodyPart = CreateBodyPart(subDocument, ownerPara);
29+
//Replace the text with the created text body part.
30+
document.Replace(findText, textBodyPart, true, true);
31+
}
32+
}
33+
}
34+
35+
//Save the Word document.
36+
using (FileStream output = new FileStream("../../../Result.docx", FileMode.Create, FileAccess.Write))
37+
{
38+
document.Save(output, FormatType.Docx);
39+
}
40+
}
41+
}
42+
43+
/// <summary>
44+
/// Create body parts that need to be replaced in the Word document.
45+
/// </summary>
46+
/// <param name="subDocument">Document contains the paragraph which need to be replace</param>
47+
/// <param name="ownerPara">The paragraph that needs to be copied.</param>
48+
TextBodyPart CreateBodyPart(WordDocument subDocument, WParagraph ownerPara)
49+
{
50+
//Creates new text body part.
51+
TextBodyPart bodyPart = new TextBodyPart(ownerPara.Document);
52+
//Iterate each section of the Word document.
53+
foreach (WSection section in subDocument.Sections)
54+
{
55+
//Accesses the Body of section where all the contents in document are apart
56+
WTextBody sectionBody = section.Body;
57+
IterateTextBody(sectionBody, ownerPara, bodyPart);
58+
}
59+
return bodyPart;
60+
}
61+
/// <summary>
62+
/// Iterates textbody child elements.
63+
/// </summary>
64+
void IterateTextBody(WTextBody sectionTextBody, WParagraph ownerPara, TextBodyPart bodyPart)
65+
{
66+
//Iterates through each of the child items of WTextBody
67+
for (int i = 0; i < sectionTextBody.ChildEntities.Count; i++)
68+
{
69+
//IEntity is the basic unit in DocIO DOM.
70+
//Accesses the body items (should be either paragraph, table or block content control) as IEntity
71+
IEntity bodyItemEntity = sectionTextBody.ChildEntities[i];
72+
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
73+
//Decides the element type by using EntityType
74+
switch (bodyItemEntity.EntityType)
75+
{
76+
case EntityType.Paragraph:
77+
WParagraph paragraph = bodyItemEntity as WParagraph;
78+
AddParagraphItemsToTextBody (paragraph, ownerPara, bodyPart);
79+
break;
80+
case EntityType.Table:
81+
//Add to text body part.
82+
bodyPart.BodyItems.Add(bodyItemEntity.Clone());
83+
break;
84+
case EntityType.BlockContentControl:
85+
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
86+
//Iterates to the body items of Block Content Control.
87+
IterateTextBody(blockContentControl.TextBody, ownerPara, bodyPart);
88+
break;
89+
}
90+
}
91+
}
92+
/// <summary>
93+
/// Add the paragraph items to the text body part.
94+
/// </summary>
95+
void AddParagraphItemsToTextBody(WParagraph paragraph, WParagraph ownerPara, TextBodyPart bodyPart)
96+
{
97+
//Clone the paragraph.
98+
WParagraph paratoInsert = ownerPara.Clone() as WParagraph;
99+
//Clear the paragraph's items.
100+
paratoInsert.ChildEntities.Clear();
101+
//Iterate the items of the paragraph.
102+
for (int paraItemIndex = 0; paraItemIndex < paragraph.ChildEntities.Count; paraItemIndex++)
103+
{
104+
//Add the paragraph items.
105+
paratoInsert.ChildEntities.Add(paragraph.ChildEntities[paraItemIndex].Clone());
106+
}
107+
//Add to text body part.
108+
bodyPart.BodyItems.Add(paratoInsert.Clone());
109+
}

0 commit comments

Comments
 (0)