How to format bookmark content in ASP.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 format bookmark content in a Word document using C#.
In the sample below, we have applied basic formatting by removing the background color from the text in the bookmark content. Based on your requirements, you can apply additional formatting to the paragraphs and tables within the bookmark content.
Steps to format bookmark content in a Word document:
- 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 a trial message.
- Include the following namespaces in the Program.cs file:
C#
using Syncfusion.DocIO.DLS;
using Syncfusion.DocIO;
- Use the following code example to format bookmark content in a Word document.
C#
// Create an input file stream to open the document
using (FileStream inputStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
{
//Creates a new Word document.
using (WordDocument document = new WordDocument(inputStream,FormatType.Docx))
{
// Create the bookmark navigator instance
BookmarksNavigator bookmarkNavigator = new BookmarksNavigator(document);
// Move to the bookmark
bookmarkNavigator.MoveToBookmark("Adventure_Bkmk");
// Get the bookmark content
TextBodyPart part = bookmarkNavigator.GetBookmarkContent();
// Iterate through the content
ModifyTextBodyPartFormat(part.BodyItems);
// Replace the bookmark content with modified content
bookmarkNavigator.ReplaceBookmarkContent(part);
//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);
}
}
}
- Use the following code example to iterate text body part body items
C#
private static void ModifyTextBodyPartFormat(EntityCollection textBodyItems)
{
// Iterates through each of the child items of WTextBody
for (int i = 0; i < textBodyItems.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 = textBodyItems[i];
// A Text body has 3 types of elements - Paragraph, Table and Block Content Control
// Decide the element type by using EntityType
switch (bodyItemEntity.EntityType)
{
case EntityType.Paragraph:
{
WParagraph paragraph = bodyItemEntity as WParagraph;
if (paragraph != null)
{
// 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
WTable table = bodyItemEntity as WTable;
if (table != null)
{
IterateTable(table);
}
break;
}
case EntityType.BlockContentControl:
{
BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
if (blockContentControl != null && blockContentControl.TextBody != null)
{
// Iterates the body items of Block Content Control.
ModifyTextBodyPartFormat(blockContentControl.TextBody.ChildEntities);
}
break;
}
}
}
}
- Use the following code example to iterates all rows and cells in a table, processing each cell’s text body.
C#
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
ModifyTextBodyPartFormat(cell.ChildEntities);
}
}
}
- Use the following code example to iterates all paragraph items and applies formatting.
C#
private static void IterateParagraph(ParagraphItemCollection paraItems)
{
for (int i = 0; i < paraItems.Count; i++)
{
Entity entity = paraItems[i];
//A paragraph can have child elements such as text, image, hyperlink, symbols, etc.,
//Decides the element type by using EntityType
switch (entity.EntityType)
{
case EntityType.TextRange:
//Remove the text back color
WTextRange textRange = entity as WTextRange;
textRange.CharacterFormat.TextBackgroundColor = Syncfusion.Drawing.Color.Empty;
break;
case EntityType.TextBox:
//Iterates to the body items of textbox.
WTextBox textBox = entity as WTextBox;
ModifyTextBodyPartFormat(textBox.TextBoxBody.ChildEntities);
break;
case EntityType.Shape:
//Iterates to the body items of shape.
Shape shape = entity as Shape;
ModifyTextBodyPartFormat(shape.TextBody.ChildEntities);
break;
case EntityType.InlineContentControl:
//Iterates to the paragraph items of inline content control.
InlineContentControl inlineContentControl = entity as InlineContentControl;
IterateParagraph(inlineContentControl.ParagraphItems);
break;
}
}
}
You can download a complete working sample to format bookmark content in a Word document from GitHub
By executing the program, you will get the Word document as follows:
Take a moment to peruse the documentation where you can find basic Word document processing options along with features like mail merge, merge, split, and compare Word documents, find and replace text in the Word document, protect Word documents, and most importantly, the PDF and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to format bookmark content in a Word document.
You can refer to our ASP.NET Core DocIO feature tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with 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!