Issue with setResult in updating the field

I am doing

field.setResult("My field") 

In aspose word it is updating the field but field styles are getting removed from word document. Is there any better way to do that.
For example:
Hello (style : bold, All_caps) is there previously
Now after updating My field is showing with any style

@lucifer90

To update a field in an Aspose.Words document while preserving its formatting, you can follow a specific approach. Instead of directly using field.setResult("My field"), which may remove the existing styles, you can manipulate the text nodes within the field to maintain the desired formatting.

Here’s a sample Java code snippet that demonstrates how to update the text of a content control while preserving its formatting:

Document doc = new Document("C:\\temp\\content_control_issue\\Testing_Header_Fonts.docx");

// Get the target content control (StructuredDocumentTag)
StructuredDocumentTag contentControl = (StructuredDocumentTag) doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true).get(0);

// Get the first Run of text to keep its formatting
Run firstRun = (Run) contentControl.getChildNodes(NodeType.RUN, true).get(0);

// Remove all other nodes to keep the formatting of the first Run
if (firstRun != null) {
    for (Node node : (Iterable<Node>) contentControl.getChildNodes(NodeType.ANY, true)) {
        if (node != firstRun) {
            node.remove();
        }
    }
    // Update the text of the first Run
    firstRun.setText("My field");
}

doc.save("C:\\Temp\\content_control_issue\\Updated_Document.docx");

In this code:

  • You first load the document and access the content control.
  • Then, you identify the first Run node, which contains the formatting you want to preserve.
  • By removing other nodes and updating the text of the first Run, you ensure that the formatting remains intact.

This method allows you to update the field’s content without losing the existing styles such as bold or all caps.

If you have any further questions or need additional assistance, feel free to ask!

@lucifer90 Could you please attach your sample input and expected output documents here for testing? We will check them and provide you more information.

Same input create a word document with fill in field with style bold and uppercase
Then update the field with setResult .style will get removed from the field

@lucifer90 Could you please attach your sample input and expected output documents here for testing?

Input file :
FIELD.docx (14.4 KB)

Expected Output: On Updating the field using setResult to “MY field” style should persue the old field style

@lucifer90 As I can see formatting is properly preserved:

Document doc = new Document("C:\\Temp\\in.docx");
    
Field f = doc.getRange().getFields().get(0);
f.setResult("This is new result");
        
doc.save("C:\\Temp\\out.docx");

out.docx (11.5 KB)

But i can see in out.docx the content is not in uppercase

@lucifer90 In the input document field is not formatted as uppercase:

Thanks for the reply my bad.

1 Like