Free Support Forum - aspose.com

Preserving or copying font settings during field value replacement

Since UpdateField doesn't seem to support CREATEDATE fields I used the workaround suggested in this thread http://www.aspose.com/community/forums/thread/55456/date-field-not-being-updated-with-current-date.aspx and modified it to work with CREATEDATE.

The problem with this workaround is that I can't get it to preserve the font settings for the field in the original document.

How can I get Aspose.Words to either preserve or copy the font settings from the original field value or is it possible to update the CREATEDATE field in some other way?

Regards,

Patrik

Hi Patrik,

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. Could you please attach your document and expected output here for testing? I will check the issue on my side and provide you more information.

Best regards.

Here is a test document containing two CREATEDATE fields with different font settings. I first change the created time using getBuiltInDocumentProperties().setCreatedTime() and then I want the CREATEDATE fields to be updated.
If I use Document.UpdateFields() they are not updated and if I use the workaround code from the forum thread above they are updated but the font settings are somehow changed so both fields have the same settings.

Regards,

Patrik

Hi

<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

Thanks for your request. You can try using the following code to achieve this:

private static void updateCreatedDate(Document doc, Date newDate) throws Exception

{

// First, we should update Document property.

doc.getBuiltInDocumentProperties().setCreatedTime(newDate);

// Then we should update values of CREATEDATE fields.

// DocumentBuilder will help us to achieve this.

DocumentBuilder builder = new DocumentBuilder(doc);

// We should get all field starts to find CREATEDATE fields.

NodeCollection starts = doc.getChildNodes(NodeType.FIELD_START, true, true);

for(int i=0; i<starts.getCount(); i++)

{

FieldStart start = (FieldStart)starts.get(i);

if(start.getFieldType() == FieldType.FIELD_CREATE_DATE)

{

// To update field, we should update text between FieldSeparator and FieldEnd nodes.

// Search for FieldSeparator

Node currentNode = start;

while(currentNode.getNodeType() != NodeType.FIELD_SEPARATOR)

currentNode = currentNode.getNextSibling();

// Remove old value of field.

while(currentNode.getNodeType() != NodeType.FIELD_END)

{

if(currentNode.getNodeType() == NodeType.RUN)

((Run)currentNode).setText("");

currentNode = currentNode.getNextSibling();

}

// Move DocumentBuilder cursor to the cuurent node

// and insert updated value.

builder.moveTo(currentNode);

builder.write(newDate.toString());

}

}

}

But note, this method does not aware formatting switches specified in your fields. I think, you can modify the method to do so. Hope this helps.

Best regards.