Field name="PRINTDATE" update problem

Dear Sir/Madam,

I downloaded Aspose.Words for .NET 18.12 trial version and perform the Word Automation evaluation and testing.

I use below sample program for MailMerge to output pdf.

//Open document
Aspose.Words.Document doc = new Aspose.Words.Document(FilePathName);

//Update fields.
doc.UpdateFields();

//Execute the nested mail merge without regions
doc.MailMerge.Execute(DataTable);

doc.Save(“test.pdf”);

But I found for Field code =PRINTDATE @ “MMMM d, yyyy” , it outputs as “January 1, 0001” instead of current calendar date.

Please advise if there have any method to deal with the PRINTDATE field update. Thank in advance.

Thanks and Regards,
Sam Liu

@sam.tsliu

Thanks for your inquiry. Please ZIP and attach your input Word document here for testing. We will investigate the issue on our side and provide you more information.

Dear Sir/Madam,

Thank for your reply. Would you please provide any code sample to replace Field name=“PRINTDATE” to “DATE”? Thank you.

Thanks and Regards,
Sam

Attached zip file for your references.

Thanks and Regards,
Sam

@sam.tsliu

Thanks for your inquiry.

Please use the following code example to replace PRINTDATE to DATE field. Hope this helps you.

Document doc = new Document(MyDir + "in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (FieldPrintDate fielddate in doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldPrintDate).ToList())
{
    builder.MoveToField(fielddate, true);
    builder.InsertField(FieldType.FieldDate, true);
    fielddate.Remove();
}

doc.Save(MyDir + "18.12.docx");

Unfortunately, we have not found the input document in this thread. Please attach it for testing. We will investigate the issue and provide you more information about your query.

Dear Sir,

Thank for your help. The PRINTDATE can be replaced to DATE. Would you please provide the code sample for extracting MERGEFORMAT from MergeField. So that I can apply back to DATE MergeField. Thanks a lot.

Thanks and Regards,
Sam

@sam.tsliu

Thanks for your inquiry. Please use Field.GetFieldCode method to get the text between field start and field separator. You can get the desired information from the text returned by this method.

If you still face problem, please ZIP and attach your input Word document and expected output. We will then provide you more information about your query.

PrintDateTest.docx.zip (17.2 KB)

Dear Sir,

Attached with the Word document zip for your references. Thank you.

Thanks and Regards,
Sam

@sam.tsliu

Thanks for sharing the detail. Please use Format.DateTimeFormat property as shown below to get the desired output. Hope this helps you.

Document doc = new Document(MyDir + "PrintDateTest.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (FieldPrintDate fielddate in doc.Range.Fields.Cast<Field>().Where(f => f.Type == FieldType.FieldPrintDate).ToList())
{
    builder.MoveToField(fielddate, true);
    Console.WriteLine(fielddate.Format.DateTimeFormat);
    FieldDate date = (FieldDate)builder.InsertField(FieldType.FieldDate, true);
    date.Format.DateTimeFormat = fielddate.Format.DateTimeFormat;
    fielddate.Remove();
}

doc.Save(MyDir + "18.12.docx");