Rendering a text in word template is rendered as date instead of text

I have a word template with a placeholder “<<[Title]>>”. Passing “11-Dec” in Json source to the template, It is rendering as “12/11/2024 12:00:00 AM” instead of “11-Dec”. How to render as text instead of converting to Date Time.

@ottap

To ensure that the placeholder “<<[Title]>>” in your Word template renders the text “11-Dec” instead of being interpreted as a date, you need to escape the special characters that are causing this behavior. In Aspose.Words, you can use a backslash to escape characters that have special meanings.

Here’s how you can modify your JSON source to prevent the placeholder from being treated as a date:

  1. Change your placeholder in the template to use escape characters. For example, you can modify it to <<[Title]>> to <<[Title]>> in your JSON data.
  2. When passing the value “11-Dec”, ensure that it is treated as a string and not a date.

Here’s a code example demonstrating how to set this up:

Document doc = new Document("path/to/your/template.docx");
doc.getMailMerge().execute(new String[] { "Title" }, new Object[] { "11-Dec" });
doc.save("path/to/your/output.docx");

By ensuring that the placeholder is correctly formatted and the value is passed as a string, you should be able to render “11-Dec” as intended without it being converted to a date format.

If you continue to experience issues, please check the documentation on handling special characters and formatting in Aspose.Words for further guidance.

Sources:
[1]: LINQ - Date Time Formatting Escape Characters

@ottap

In this case, it is required to restrict parsing of date-time values or skip their parsing at all while loading JSON. This can be done as follows:

List<String> formats = new List<String>();
formats.Add("MM/dd/yyyy");

JsonDataLoadOptions options = new JsonDataLoadOptions();
options.ExactDateTimeParseFormats = formats;
JsonDataSource dataSource = new JsonDataSource(..., options);

In this example, only strings conforming the format “MM/dd/yyyy” are going to be recognized as date-time values. To skip recognizing of date-time values at all, you can set the exact date-time parse formats to an empty list.

See Accessing JSON Data for more information.