Date Field not being updated with current date

I'm am using Aspose.Words ver 3.6.1.0 and Aspose.PDF 3.1.1.0. My word documents have a date field that is set to update automatically, however, when I merge data using Aspose.Words and export to PDF, the date field does not get updated to the current date that the document is generated. I entered the date field two different ways. First by going to Insert - Date Field (checked update automatically). And the second way was Insert - Field - Date. In both cases the field code is mask as { DATE \@ "MMMM/dd/yyyy" }. I have attached the word document and the pdf document for your reference.

Thanks,

ryan

Aspose.Words does not update Date fields automatically. You need to update these fields in MS Word to have the actual data. I will check if it can be done by some coding with the current Aspose.Words API and let you know.

Best regards,

Thanks for the quick response.

if Aspose.Words currently does not update the Date field automatically, is there a code around that I might be able to implement?

Thanks

Here is an example of how DATE field updates can be done with the current API:

private void UpdateDates(Document doc)

{

NodeCollection fieldStarts = doc.GetChildNodes(NodeType.FieldStart, true);

ArrayList dateFieldSeparators = new ArrayList();

ArrayList dates = new ArrayList();

ArrayList nodesForRemoval = new ArrayList();

foreach (FieldStart fieldStart in fieldStarts)

{

if (fieldStart.FieldType == FieldType.FieldDate)

{

Node node = fieldStart;

string fieldCode = "";

while (node.NodeType != NodeType.FieldSeparator)

{

if (node.NodeType == NodeType.Run)

{

fieldCode += ((Run)node).GetText();

}

node = node.NextSibling;

}

dateFieldSeparators.Add(node);

node = node.NextSibling;

Match match = Regex.Match(fieldCode, @".*?\\@\s*""(?.*?)""");

string dateFormat = match.Groups["dateformat"].Value;

string date = DateTime.Now.ToString(dateFormat, CultureInfo.CurrentCulture);

dates.Add(date);

while (node.NodeType != NodeType.FieldEnd)

{

nodesForRemoval.Add(node);

node = node.NextSibling;

}

}

}

for (int i = 0; i < dateFieldSeparators.Count; i++)

{

Node node = ((Node)dateFieldSeparatorsIdea [I]);

node.ParentNode.InsertAfter(new Run(doc, (string)datesIdea [I]), node);

}

foreach (Node node in nodesForRemoval)

{

node.Remove();

}

}

In this example we look through all fields, search for DATE fields among them, parse the field code with the regular expression to get formatting strings like M/d/yyyy. Then we take current system date/time, format it using parsed format string from the field code and CultureInfo of the current thread. The resulting string is inserted as a new field value. Nodes that were previously in the field value are removed.

Field code is composed of the text from nodes that are between FieldStart and FiledSeparator nodes in a document. Field value consists of the nodes that are between FieldSeparator and FieldEnd.

You can use DocumentExplorer demo to get a visualized view of how the document is represented by Aspose.Words node tree. It will help you to understand better how documen nodes can be used and manipulated.

Best regards,

Aspose Team,

Thanks, I'll put it to use.

Legend - thanks for this post - exactly what I needed.

We had a similiar problem - a document had some dates in it that were designed to be updated on print. Doing a merge to Word worked fine, but when we tried to print a document or merge to PDF - the date wouldn't get updated.

This code works like a dream and does the job (and gives a bit of insight how todo other field types as well!)