When setText () is applied to a Hyperlink Run, the Hyperlink and all text elements within it appear to be lost (24.4 NG / 24.3 OK)

File source = new File("hyperlink.docx");
File target = new File("hyperlink_out.docx");
Document document = new Document(source.getAbsolutePath());
NodeCollection paragraphs = document.getChildNodes(NodeType.PARAGRAPH, true);
for (int i = 0; i < paragraphs.getCount(); i++)
{
    Paragraph paragraph = (Paragraph)paragraphs.get(i);
    RunCollection runs = paragraph.getRuns();
    for (int j = 0; j < runs.getCount(); j++)
    {
        Run run = runs.get(j);
        if (run.getText() == null || run.getText().length() == 0)
        {
            continue;
        }

        run.setText("[Test_" + j + "]" + run.getText());
    }
}
document.save(target.getAbsolutePath());

Original Document

24.3 (No problem)

24.4 (NG)

Is this a specification change or a bug?

@mrd.tokyo.mori Could you please attach your input document here for testing? We will check the issue and provide you more information. Unfortunately, it is impossible to analyze the issue using screenshots.

@alexey.noskov Thank you for your confirmation. I will attach the file.
hyperlink.docx (13.2 KB)

@mrd.tokyo.mori Actually behavior in 24.4 version looks more correct then behavior of 24.3 version. Structure of your input document is the following:

There are 4 Run nodes in the document. After editing runs with your code and 24.3 version the document structure looks like this:

As you can see Run between FieldStart and FieldSeparator is not changed.

After editing the document with 24.4 version all Run nodes have been updated:

But since the code edits the field code and makes the field invalid from MS Word point of view it looks incorrectly in MS Word.

@alexey.noskov Thank you for your verification.
I’m going to change my application so that it doesn’t touch Run, which is between FieldStart and FieldSeparator.

1 Like