Date time field control to show actual date vs current date in aspose words

Hi,
A sample docx file for your reference which opens in MS Word and shows current date for 1st and 2nd fields but not in aspose. Also when i call doc.updateFields(); it does show current date but the output is not same as in MS Word. My request is on two points:

  1. How to ensure the render is same as in MS Word.
  2. Lets say i want the actual date to be rendered and not to show current date ( Not calling doc.updateFields(); can do this but doesnt match)
    docxFileTimeIssue.docx (12.4 KB)

@maheibm By default Aspose.Words does not update Created Time, Last Printed and Last Saved Time properties, so the corresponding fields shows values stored in the original document. You can configure this behavior using the appropriate PdfSaveOptions: UpdateCreatedTimeProperty, UpdateLastPrintedProperty and UpdateLastSavedTimeProperty. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

PdfSaveOptions opt = new PdfSaveOptions();
opt.setUpdateCreatedTimeProperty(true);
opt.setUpdateLastPrintedProperty(true);
opt.setUpdateLastSavedTimeProperty(true);
        
doc.save("C:\\Temp\\out.pdf", opt);

Thanks for your quick response. Currently relying on doc.renderToSize and hence is there a way to set these properties on document and i don’t need to convert to pdf.

@maheibm There are similar options in ImageSaveOptions. So you can save the document to image instead of using renderToSize and then work with image.

Hi,
Thanks again for the prompt response. Note that using ImageSaveOptions would be a two step process to save to PNG (for example) and then read the saved image to convert to graphics for the requirement we have. Hence would request you to let us know the best possible way to deal with this scenario.

@maheibm Unfortunately, currently saving the document to image is the only possible solution. We will consider providing a wat to specify these options while using Document.RenderToSize and Document.RenderToSale methods.

We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): WORDSNET-26787

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

Thanks a lot for the consideration. Appreciate it. Would however check with the properties you’ve specified via save options, till the issue is handled. Shall let you know for any observations on the same

1 Like

Hi @alexey.noskov,
After changing the implementation to save it as png i still see that the output doesnt match(check the print date section) with the one rendered by MS Word. Attached screenshot of the same where the left one is from aspose and right one from MS Word. I did use the below code as i only wanted to update the created date.

ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
opt.setUpdateCreatedTimeProperty(true);
opt.setUpdateLastPrintedProperty(false);
opt.setUpdateLastSavedTimeProperty(false);

Basically the requirement is to retain the last computed field value stored in the MS Word document and on certain requirement (by setting a flag) update with current system date. Need the flexibility to turn this on and off.

HI @alexey.noskov
Also to bring to your notice wrt the earlier attached doc file with these date time fields, the whole requirement is not to update the created, saved, print date. But the first and second rows (date and time sextions in the document) is the only one that should be having the flexibility to update to

  1. actual saved date in document
  2. use current system date

So a flag or a way to enable and disable this is the need. Hence i dont need the use of setUpdateCreatedTimeProperty, setUpdateLastPrintedProperty, setUpdateLastSavedTimeProperty at all.
Please do suggest. Just like in MSWord is what the requirement is but with an option to either show saved date or use current date as chosen by user.

  1. calling doc.updateFields() shows the value right except for createDate and printDate as in the document, other fields match the MSWord output.
    Expected


    Actual

  2. not calling doc.updateFields() shows the fields right except savedate.
    Expected


    Actual

@maheibm You can lock a particular field to prevent updating it upon updating field or rendering the document. For example see the following code:

Document doc = new Document("C:\\Temp\\in.docx");

// Lock PRINTDATE field, so it's value will not be updated upon updating fields or rendering.
for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_PRINT_DATE)
        f.isLocked(true);
}

doc.save("C:\\Temp\\out.png");

In this case the result is the following:

The values used by CREATEDATE, PRINTDATE and SAVEDATE fields are taken from the appropriate built-in document properties. So you can specify these values as it is required:

Document doc = new Document("C:\\Temp\\in.docx");

// Update values of CreatedTime, LastPrinted and LastSavedTime values to the current date.
// `CREATEDATE`, `PRINTDATE` and `SAVEDATE` fields will show the appropriate values in the output.
doc.getBuiltInDocumentProperties().setCreatedTime(new Date());
doc.getBuiltInDocumentProperties().setLastPrinted(new Date());
doc.getBuiltInDocumentProperties().setLastSavedTime(new Date());

doc.updateFields();

doc.save("C:\\Temp\\out.png");

The above code produces the following output:

So in your code you ca specify the required values of the fields or lock the particular fields to preserve the values shown by MS Word.

@alexey.noskov Thanks for the above inputs, really helped a lot. Howver there is one issue, even when save date is locked as in first code snippet (f.islocked), i see the save date field showing up as
SaveDate: Friday, September 8,2023 at 08:57:00 (this is the same when viewed the file using aspose online viewer too), however the expected value as in MS Word or as in your screenshot is SaveDate: Friday, October 6, 2023 at 12:51:00, basically month and date difference in value. How are you able to get the right value for save date that i’m failing to ?

@maheibm Friday, September 8,2023 at 08:57:00value is the value of CREATEDATE field in the source document:

<w:r w:rsidRPr="002D7560">
	<w:rPr>
		<w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
		<w:kern w:val="0"/>
		<w:lang w:val="en-US"/>
	</w:rPr>
	<w:instrText xml:space="preserve"> CREATEDATE \@ "dddd, MMMM d, yyyy 'at' hh:mm:ss" </w:instrText>
</w:r>
<w:r w:rsidRPr="002D7560">
	<w:rPr>
		<w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
		<w:kern w:val="0"/>
		<w:lang w:val="en-US"/>
	</w:rPr>
	<w:fldChar w:fldCharType="separate"/>
</w:r>
<w:r w:rsidRPr="002D7560">
	<w:rPr>
		<w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
		<w:noProof/>
		<w:kern w:val="0"/>
		<w:lang w:val="en-US"/>
	</w:rPr>
	<w:t>Thursday, September 15, 2022 at 01:27:00</w:t>
</w:r>
<w:r w:rsidRPr="002D7560">
	<w:rPr>
		<w:rFonts w:eastAsia="Times New Roman" w:cs="Times New Roman"/>
		<w:kern w:val="0"/>
		<w:lang w:val="en-US"/>
	</w:rPr>
	<w:fldChar w:fldCharType="end"/>
</w:r>

But in document properties there is the following code:

<dcterms:modified xsi:type="dcterms:W3CDTF">2023-10-06T07:21:00Z</dcterms:modified>

It looks like CREATEDATE field value is not updated on your side for some reason. I cannot reproduce this behavior on my side. Even the following simple code returns correct value:

Document doc = new Document("C:\\Temp\\in.docx");
doc.save("C:\\Temp\\out.png");

So you can try updating this particular field to show the correct value:

for (Field f : doc.getRange().getFields())
{
    if (f.getType() == FieldType.FIELD_CREATE_DATE)
        f.update();
}