Skip to content
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

Add sample for insert content of the Word document into Block content control #292

Open
wants to merge 1 commit 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 15
VisualStudioVersion = 15.0.28307.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Insert-content-into-block-content-control", "Insert-content-into-block-content-control\Insert-content-into-block-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_block_content_control</RootNamespace>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insert Word document in content control -- Change the sample name like this

</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,40 @@
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)
{
// Get the textbody of the another Word document and add it to the block content control.
WTextBody textBody = document.LastSection.Body;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Iterate all sections of document and perform the same for all textbody items

foreach (IEntity entity in textBody.ChildEntities)
{
blockContentControl.TextBody.ChildEntities.Add(entity.Clone());
}
}