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 use conditional fields to add check boxes during mail-merge #290

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,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.12.35417.141 d17.12
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-checkbox-using-IF-field", "Add-checkbox-using-IF-field\Add-checkbox-using-IF-field.csproj", "{CDB67E3F-6956-4FC5-8BC1-E4DB0A71C62E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{CDB67E3F-6956-4FC5-8BC1-E4DB0A71C62E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDB67E3F-6956-4FC5-8BC1-E4DB0A71C62E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDB67E3F-6956-4FC5-8BC1-E4DB0A71C62E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDB67E3F-6956-4FC5-8BC1-E4DB0A71C62E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">

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

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

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

</Project>
Binary file not shown.
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,37 @@
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;

using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
//Opens the template Word document.
using (WordDocument wordDocument = new WordDocument(inputStream, FormatType.Docx))
{
// Set default values for the merge fields
string[] fieldNames = { "Name", "Email", "AgreeToTerms", "SubscribeNewsletter" };
string[] fieldValues = { "Nancy", "[email protected]", "Yes", "No" };

// Execute mail merge to replace merge fields with actual values.
wordDocument.MailMerge.Execute(fieldNames, fieldValues);

// Update any fields in the document to reflect the changes made during the mail merge.
wordDocument.UpdateDocumentFields();

// Set up font substitution settings for handling missing fonts.
wordDocument.FontSettings.SubstituteFont += FontSettings_SubstituteFont;

// Save the modified document.
using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
wordDocument.Save(outputStream1, FormatType.Docx);
}
}
}

/// <summary>
/// Handles font substitution when a required font is unavailable in the document.
/// </summary>
static void FontSettings_SubstituteFont(object sender, SubstituteFontEventArgs args)
{
// Display the name of the original font that needs substitution
Console.WriteLine(args.OriginalFontName);
}