We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Clarifications On Field Codes

Hi,



We are working on field codes especially Date field code. As per Microsoft, the date field code refers to current date. We do not want this field code to get updated automatically. We want it to have the same date when it was created. We want this requirement in both word document and also when its saved in pdf format.

We started our investigation with a word document say “old” which was created on "June 12, 2014"

Scenario 1: When we load “old” into Aspose Words Document instance and try to get the result of this date field, we get “June 12, 2014”. Afterwards, we save this document in docx and pdf formats. The saved docx file’s document.xml has result for this field code as June 12, 2014 but Pdf file shows today’s date i.e., December 7, 2015.

When loaded document gives June 12, 2014 as result of date field code and document.xml also has same date then how come date field code got updated in its pdf format. Even if we open saved docx file using Microsoft Word UI then also it shows today’s date but document.xml has older value??? Please explain this. I am very much confused whats happening here.

code for scenario 1:

public void test1(){
Document doc = new Document(“Valid012Doclet.docx”);

FieldCollection fields = doc.getRange().getFields();
for (Field field : fields) {
if (field.getType() == FieldType.FIELD_DATE) {
System.out.println("Current Date is: " + field.getResult());
}
}

doc.save(“docletSaved.docx”);
doc.save(“docletSaved.pdf”);
}
Output: Current Date is: June 12, 2014

Scenario 2: First opened the “old” in Microsoft Word UI and then saved it as a new word file. Then loaded this new word file using Aspose API and retrieved the result of this date field code and got December 7, 2015. Afterwards. I saved this document in docx and pdf formats. The docx file’s document.xml has result for this field code as December 7, 2015 and Pdf file also shows today’s date i.e., December 7, 2015.


code for scenario 2:

public void test1(){
Document doc = new Document(“docletSavedMS.docx”);

FieldCollection fields = doc.getRange().getFields();
for (Field field : fields) {
if (field.getType() == FieldType.FIELD_DATE) {
System.out.println("Current Date is: " + field.getResult());
}
}

doc.save(“docletSavedMS1.docx”);
doc.save(“docletSavedMS1.pdf”);
}
Output: Current Date is: December 7, 2015

I am unable to understand why I am seeing different behaviors in both scenarios when both scenarios are more or less same. Aspose API does not seem to be consistent with MS Word behavior in case of field codes.

The documents are also attached with this post for your reference.

Also attaching the snapshot where left side is scenario 1 saved word document’s document.xml and right side is scenario 2 saved word document’s document.xml.

Looking for your reply.

Thanks
Manisha

Hi Manisha,


Thanks for your inquiry. Please try using the following code:
Document doc = new Document(getMyDir() + “docletSaved.docx”);

FieldCollection fields = doc.getRange().getFields();

for (Field field : fields) {
if (field.getType() == FieldType.FIELD_DATE) {
System.out.println("Current Date is: " + field.getResult());
field.isLocked(true);
}
}

doc.save(
getMyDir() + “awjava-15.11.0.docx”);
doc.save(
getMyDir() + “awjava-15.11.0.pdf”);
Hope, this helps.

Best regards,