Skip to content
Merged
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 15
VisualStudioVersion = 15.0.28307.136
MinimumVisualStudioVersion = 10.0.40219.1
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}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AE9996D9-17F0-4A69-AB1D-CA10E8605CF7}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {2D02C5F8-31E9-4F9E-96E9-D370D58B7826}
EndGlobalSection
EndGlobal
Binary file not shown.
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>Insert_Word_document_in_content_control</RootNamespace>
</PropertyGroup>

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

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

</Project>
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,47 @@
using System.IO;
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

// Creates a new Word document.
using (WordDocument document = new WordDocument())
{
// Adds new section to the document.
IWSection section = document.AddSection();
WTextBody textBody = section.Body;
// Adds block content control into Word document.
BlockContentControl blockContentControl = textBody.AddBlockContentControl(ContentControlType.RichText) as BlockContentControl;
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
// Open the template Word document.
using (WordDocument docxDocument = new WordDocument(docStream, FormatType.Docx))
{
// Insert the Word document into block content control.
InsertContentIntoBlockContentControl(blockContentControl, docxDocument);
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
// Saves the Word document.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
}

/// <summary>
/// Inserts the content of a specified Word document into a block content control.
/// </summary>
void InsertContentIntoBlockContentControl(BlockContentControl blockContentControl, WordDocument document)
{
// Iterate through all sections of the source document.
foreach (WSection section in document.Sections)
{
// Get the text body of the current section.
WTextBody textBody = section.Body;

// Iterate through the child entities in the text body.
foreach (IEntity entity in textBody.ChildEntities)
{
// Add each entity to the block content control's text body.
blockContentControl.TextBody.ChildEntities.Add(entity.Clone());
}
}
}
Loading