Articles in this section
Category / Section

How to remove the placeholder text if the meta property is empty in a Word document?

5 mins read

Syncfusion Essential DocIO is a .NET Core Word library used to create, read, and edit Word documents programmatically without Microsoft Word or Interop dependencies. Using this library, you can remove the placeholder text if the meta property is empty in a Word document in C#.

Steps to remove the placeholder text if the meta property is empty in the Word document

  1. Create a new C# .NET Core console application project. Create .NET Core console application in Visual Studio in ASP.NET Core Word
  2. Install the Syncfusion.DocIO.Net.Core NuGet package as a reference to your .NET Core applications from NuGet.org. Add DocIO.Net.Core NuGet packages of ASP.NET Core Word
  3. Include the following namespace in the Program.cs file.

C#

using Syncfusion.DocIO;
using Syncfusion.DocIO.DLS;
using Syncfusion.Office;

 

Note:
  1. When opening a Word document in the Microsoft Word application, if the meta property value is empty, it displays the placeholder text. This is the default behavior.
  2. The meta property is then displayed as a content control in the Word document.
  1. To remove the placeholder text if the meta property is empty, iterate through the Word document elements and get the content control. Then, ensure that the content control is mapped with the meta property and its value is empty, and delete the content control from the Word document. To learn more about content controls, click here.

4.1 Use the following code example to remove the placeholder text if the meta property is empty in the Word document.

C#

//Open the file as a stream.
using (FileStream docStream = new FileStream(Path.GetFullPath(@"../../../Input.docx"), FileMode.Open, FileAccess.Read))
{
    //Load the file stream into a Word document.
    using (WordDocument document = new WordDocument(docStream, FormatType.Automatic))
    {
        //Iterate section in the Word document.
        foreach (WSection section in document.Sections)
        {
            //Access the Body of the section where all the contents in the document are apart.
            WTextBody sectionBody = section.Body;
            IterateTextBody(sectionBody);
        }
        //Create a file stream.
        using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"../../../Sample.docx"), FileMode.Create, FileAccess.ReadWrite))
        {
            //Save the Word document to the file stream.
            document.Save(outputFileStream, FormatType.Docx);
        }
    }
}

4.2 Use the following helper method to iterate the textBody of the Word document and get content control.

C#

/// <summary>
/// Iterate the TextBody of the Word document.
/// </summary>
private static void IterateTextBody(WTextBody textBody)
{
    //Iterate through child entities of the WTextBody.
    for (int i = 0; i < textBody.ChildEntities.Count; i++)
    {
        //IEntity is the basic unit in the DocIO DOM. 
        //Access the body items (should be either paragraph, table, or block content control) as IEntity.
        IEntity bodyItemEntity = textBody.ChildEntities[i];
        //Get the element type by using the EntityType.
        switch (bodyItemEntity.EntityType)
        {
            case EntityType.Paragraph:
                WParagraph paragraph = bodyItemEntity as WParagraph;
                //Iterate through the paragraph's DOM.
                IterateParagraph(paragraph.Items);
                break;
            case EntityType.Table:
                //Iterate through the table's DOM.
                IterateTable(bodyItemEntity as WTable);
                break;
            case EntityType.BlockContentControl:
                BlockContentControl blockContentControl = bodyItemEntity as BlockContentControl;
                //Check whether the content control is xml mapped with meta property.
                //Also check whether the corresponding meta property value is empty.
                //If the value is empty, remove the content control.
                if (IsRemoveContentControl(blockContentControl))
                {
                    textBody.ChildEntities.Remove(blockContentControl);
                    i--;
                }
                break;
        }
    }
}
/// <summary>
/// Iterate Table in the Word document.
/// </summary>
private static void IterateTable(WTable table)
{
    //Iterate the row collection in a table.
    foreach (WTableRow row in table.Rows)
    {
        //Iterate the cell collection in a table row.
        foreach (WTableCell cell in row.Cells)
        {
            //Reuse the code meant for iterating the TextBody.
            IterateTextBody(cell);
        }
    }
}
/// <summary>
/// Iterate Paragraph in the Word document.
/// </summary>
private static void IterateParagraph(ParagraphItemCollection paraItems)
{
    for (int i = 0; i < paraItems.Count; i++)
    {
        Entity entity = paraItems[i];
        //Get the element type by using EntityType.
        switch (entity.EntityType)
        {
            case EntityType.TextBox:
                //Iterate to the body items of the textbox.
                WTextBox textBox = entity as WTextBox;
                IterateTextBody(textBox.TextBoxBody);
                break;
            case EntityType.Shape:
                //Iterate to the body items of shape.
                Shape shape = entity as Shape;
                IterateTextBody(shape.TextBody);
                break;
            case EntityType.InlineContentControl:
                InlineContentControl inlineContentControl = entity as InlineContentControl;
                //Check whether the content control is xml mapped with meta property.
                //Also check whether the corresponding meta property value is empty.
                //If the value is empty, remove the content control.
                if (IsRemoveContentControl(inlineContentControl))
                {
                    paraItems.Remove(inlineContentControl);
                    i--;
                }
                break;
        }
    }
}

4.3 Use the following helper method to check if the meta property is empty and remove it.

C#

/// <summary>
/// Check whether the content control is xml mapped with meta property.
/// </summary>
/// <param name="entity">The content control.</param>
/// <returns>Returns true if content control is needed to remove. Otherwise, false.</returns>
private static bool IsRemoveContentControl(IEntity entity)
{
    switch (entity.EntityType)
    {
        case EntityType.BlockContentControl:
            BlockContentControl blockContentControl = entity as BlockContentControl;
            ContentControlProperties blockproperties = blockContentControl.ContentControlProperties;
            if (blockproperties.XmlMapping.IsMapped && !string.IsNullOrEmpty(blockproperties.XmlMapping.XPath)
                && IsEmptyMetaProperty(blockproperties.Title, entity.Document))
                return true;
            break;
        case EntityType.InlineContentControl:
            InlineContentControl inlineContentControl = entity as InlineContentControl;
            ContentControlProperties inlineProperties = inlineContentControl.ContentControlProperties;
            if (inlineProperties.XmlMapping.IsMapped && !string.IsNullOrEmpty(inlineProperties.XmlMapping.XPath)
                && IsEmptyMetaProperty(inlineProperties.Title, entity.Document))
                return true;
            break;
    }
    return false;
}
/// <summary>
/// Check whether the corresponding meta property value is empty.
/// </summary>
/// <param name="title">The content control title.</param>
/// <param name="document">The Word document.</param>
/// <returns>Returns true if the meta property value is empty. Otherwise, false.</returns>
private static bool IsEmptyMetaProperty(string title, WordDocument document)
{
    MetaProperties metaProperties = document.ContentTypeProperties;
    //Iterate through the child entities of metaproperties.
    for (int i = 0; i < metaProperties.Count; i++)
    {
        //Check for a particular display name of metadata and ensure its value is empty or not.
        if (metaProperties[i].DisplayName == title && metaProperties[i].Value == null)
            return true;
    }
    return false;
}

A complete working sample to remove the placeholder text if the meta property is empty in a Word document in C# can be downloaded from GitHub.

By executing the program, you will get the output document as follows.

Output document generated in ASP.NET Core Word

Take a moment to peruse the documentation where you can find basic Word document processing options along with the features like mail mergemerge and split documents, find and replace text in the Word document, protect the Word documents, and most importantly, the PDF and Image conversions with code examples.

Explore more about the rich set of Syncfusion Word Framework features.

Conclusion

I hope you enjoyed learning about how to remove the placeholder text if the meta property is empty in a Word document.

You can refer to our ASP.NET Core DocIO’s feature tour page to know about its other groundbreaking feature representations. You can also explore our ASP.NET Core DocIO documentation to understand how to present and manipulate data.

For current customers, you can check out our ASP.NET Core 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 ASP.NET Core DocIO and other ASP.NET Core components.

If you have any queries or require clarifications, please let us know in the comment section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied