Skip to content

Commit dfa6e4e

Browse files
Merge pull request #313 from SyncfusionExamples/ES-891648-How-to-insert-the-content-of-a-docx-document-into-a-BlockContentControl
ES-891648-How to insert the content of a docx document into a BlockContentControl
2 parents 546b46c + 7a14928 commit dfa6e4e

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-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 15
4+
VisualStudioVersion = 15.0.28307.136
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert-Word-document-in-content-control", "Insert-Word-document-in-content-control\Insert-Word-document-in-content-control.csproj", "{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}"
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+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.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 = {2D02C5F8-31E9-4F9E-96E9-D370D58B7826}
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>Insert_Word_document_in_content_control</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Syncfusion.DocIO.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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.IO;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIO.DLS;
4+
5+
// Creates a new Word document.
6+
using (WordDocument document = new WordDocument())
7+
{
8+
// Adds new section to the document.
9+
IWSection section = document.AddSection();
10+
WTextBody textBody = section.Body;
11+
// Adds block content control into Word document.
12+
BlockContentControl blockContentControl = textBody.AddBlockContentControl(ContentControlType.RichText) as BlockContentControl;
13+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
14+
{
15+
// Open the template Word document.
16+
using (WordDocument docxDocument = new WordDocument(docStream, FormatType.Docx))
17+
{
18+
// Insert the Word document into block content control.
19+
InsertContentIntoBlockContentControl(blockContentControl, docxDocument);
20+
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
21+
{
22+
// Saves the Word document.
23+
document.Save(outputFileStream, FormatType.Docx);
24+
}
25+
}
26+
}
27+
}
28+
29+
/// <summary>
30+
/// Inserts the content of a specified Word document into a block content control.
31+
/// </summary>
32+
void InsertContentIntoBlockContentControl(BlockContentControl blockContentControl, WordDocument document)
33+
{
34+
// Iterate through all sections of the source document.
35+
foreach (WSection section in document.Sections)
36+
{
37+
// Get the text body of the current section.
38+
WTextBody textBody = section.Body;
39+
40+
// Iterate through the child entities in the text body.
41+
foreach (IEntity entity in textBody.ChildEntities)
42+
{
43+
// Add each entity to the block content control's text body.
44+
blockContentControl.TextBody.ChildEntities.Add(entity.Clone());
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)