Skip to content
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,3 @@
<Solution>
<Project Path="insert-merge-field-at-bookmark/insert-merge-field-at-bookmark.csproj" />
</Solution>
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

namespace Insert_merge_field_at_bookmark
{
class Program
{
static void Main(string[] args)
{
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Load an existing Word document into DocIO instance
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Docx))
{
#region Insert paragraph at bookmark
// Create text bodypart
TextBodyPart bodyPart = new TextBodyPart(document);
// Create new paragraph and append merge field
WParagraph para = new WParagraph(document);
para.AppendField("Product", FieldType.FieldMergeField);
bodyPart.BodyItems.Add(para);

//Create the bookmark navigator instance to access the bookmark
BookmarksNavigator bkmk = new BookmarksNavigator(document);
//Move the virtual cursor to the location before the end of the bookmark
bkmk.MoveToBookmark("bookmark");
// Replace the bookmark content with our body part
bkmk.ReplaceBookmarkContent(bodyPart);
#endregion

#region Execute mailmerge
string[] fieldNames = { "Product", "ProductNo", "Size" };
string[] fieldValues = { "Cycle", "1234", "32" };
document.MailMerge.Execute(fieldNames, fieldValues);
#endregion

//Creates file stream
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Save the loaded document in WordML format to the output stream
document.Save(outputFileStream, FormatType.Docx);
//Close the Word document
document.Close();
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>insert_merge_field_at_bookmark</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

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

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

</Project>
Loading