How to set proofing language for contents in .NET Core Word document?
Syncfusion® Essential® DocIO is a .NET Word library used to create, read, edit, and convert Word documents programmatically without Microsoft Word or interop dependencies. Using this library, you can set proofing language for the contents in the Word document in .NET using C#.
Use case for proofing language: when generating bills for customers in different countries, you would set the proofing language for the content to match the language of each respective country. This ensures that the spell check, grammar check, and other linguistic tools are applied correctly for the specific language of the bill, such as French for France or Spanish for Spain.
Steps to set proofing language for the contents in the Word document in .NET:
-
Create a new .NET Core console application project.
-
Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your project from NuGet.org.
Starting with v16.2.0.x, if you reference Syncfusion® assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to the link to learn about generating and registering a Syncfusion® license key in your application to use the components without trail message.
- Include the following namespaces in the Program.cs file
C#
using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Office;
- Use the following code example to set proofing language for the contents in the Word document in .NET.
C#
To enable proofing, need to configure the LocaleIDs values.
// Open an existing word document
using (FileStream fileStreamPath = new FileStream(Path.GetFullPath(@"Data/Input.docx"), FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
//Opens an existing document from file system through constructor of WordDocument class.
using (WordDocument document = new WordDocument(fileStreamPath, FormatType.Automatic))
{
foreach (WSection section in document.Sections)
{
//Accesses the Body of section where all the contents in document are apart.
WTextBody sectionBody = section.Body;
IterateTextBody(sectionBody);
WHeadersFooters headersFooters = section.HeadersFooters;
//Consider that OddHeader and OddFooter are applied to this document.
//Iterates through the TextBody of OddHeader and OddFooter.
IterateTextBody(headersFooters.OddHeader);
IterateTextBody(headersFooters.OddFooter);
}
//Creates file stream.
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Result.docx"), FileMode.Create, FileAccess.ReadWrite))
{
//Saves the Word document to file stream.
document.Save(outputFileStream, FormatType.Docx);
}
}
}
The following code example provides supporting methods for the above code.
private static void IterateTextBody(WTextBody textBody)
{
//Iterates through each of the child items of WTextBody.
for (int i = 0; i < textBody.ChildEntities.Count; i++)
{
//IEntity is the basic unit in DocIO DOM.
//Accesses the body items (should be either paragraph, table or block content control) as IEntity.
IEntity bodyItemEntity = textBody.ChildEntities[i];
//A Text body has 3 types of elements - Paragraph, Table and Block Content Control
//Decides the element type by using EntityType.
switch (bodyItemEntity.EntityType)
{
case EntityType.Paragraph:
WParagraph paragraph = bodyItemEntity as WParagraph;
//Processes the paragraph contents.
//Iterates through the paragraph's DOM.
IterateParagraph(paragraph.Items);
break;
case EntityType.Table:
//Table is a collection of rows and cells.
//Iterates through table's DOM.
IterateTable(bodyItemEntity as WTable);
break;
case EntityType.BlockContentControl:
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
//Iterates to the body items of Block Content Control.
IterateTextBody(blockContentControl.TextBody);
break;
}
}
}
The following code example provides supporting methods for the above code.
private static void IterateParagraph(ParagraphItemCollection paraItems)
{
for (int i = 0; i < paraItems.Count; i++)
{
Entity entity = paraItems[i];
//Decides the element type by using EntityType.
switch (entity.EntityType)
{
case EntityType.TextRange:
//Replaces the text with another.
WTextRange textRange = entity as WTextRange;
textRange.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR;
break;
case EntityType.Field:
WField field = entity as WField;
field.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR;
break;
case EntityType.Picture:
WPicture picture = entity as WPicture;
picture.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR;
break;
case EntityType.TextBox:
//Iterates to the body items of textbox.
WTextBox textBox = entity as WTextBox;
IterateTextBody(textBox.TextBoxBody);
break;
case EntityType.Shape:
//Iterates to the body items of shape.
Shape shape = entity as Shape;
IterateTextBody(shape.TextBody);
break;
case EntityType.InlineContentControl:
//Iterates to the paragraph items of inline content control.
InlineContentControl inlineContentControl = entity as InlineContentControl;
IterateParagraph(inlineContentControl.ParagraphItems);
break;
case EntityType.Symbol:
WSymbol symbol = entity as WSymbol;
symbol.CharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR;
break;
case EntityType.Footnote:
WFootnote footer = entity as WFootnote;
footer.MarkerCharacterFormat.LocaleIdASCII = (short)LocaleIDs.fr_FR;
// Also iterate its TextBody to set on inner WTextRange
IterateTextBody(footer.TextBody);
break;
}
}
}
private static void IterateTable(WTable table)
{
//Iterates the row collection in a table.
foreach (WTableRow row in table.Rows)
{
//Iterates the cell collection in a table row.
foreach (WTableCell cell in row.Cells)
{
//Table cell is derived from (also a) TextBody.
//Reusing the code meant for iterating TextBody.
IterateTextBody(cell);
}
}
}
You can download a complete working sample to set proofing language for the contents in the Word document in .NET from the GitHub.
A sample output of set proofing language for the contents in the Word document is shown below:
Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to set proofing language for the contents in the Word document in .NET.
You can refer to our ASP.NET Core DocIO feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core DocIO example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!