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

Free Support Forum - aspose.com

Form Field Issue

Hi,

We have some code that replaces runs in a document and then puts them back later. The attached txt file has some c# code that closely mimics the functionality of the code I’m dealing with. Yes, it looks a little strange, but in the production code, it makes more sense… hard to explain without going into a lot of detail.

The problem that we’re running into is that form fields come out corrupted in the output document. When we run UpdateFields() or manually update fields in the output document, some of the fields disappear. They don’t disappear in the source document.

Thanks for your help!
Greg

Hi Greg,


Thanks for your inquiry. After an initial test with Aspose.Words 14.2.0, I was unable to reproduce this issue on my side (please see attached out.docx). I would suggest you please upgrade to the latest version of Aspose.Words. You can download it from the following link. I hope, this helps.
http://www.aspose.com/community/files/51/.net-components/aspose.words-for-.net/default.aspx

Edit: The problem occurs because your code modifies the field code of FORMTEXT field. Please see the following code to avoid this:

protected static IList<string>
Parse(string fileNameIn, MemoryStream s)

{

IList<string> runs = new List<string>();

var doc = new Document(fileNameIn);

NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);

for (int p = 0; p < paragraphs.Count; p++)

{

for (int r = 0; r < ((Paragraph)paragraphs[p]).Runs.Count; r++)

{

runs.Add(((Paragraph)paragraphs[p]).Runs[r].Text);

if (CheckIfRunBelongsToFieldCode(((Paragraph)paragraphs[p]).Runs[r]))

{

// do not change this run as its part of Field's Code

}

else

{

((Paragraph)paragraphs[p]).Runs[r].Text = "Dead, gone, bye-bye.";

}

}

}

doc.Save(s, SaveFormat.Docx);

return runs;

}

protected static bool CheckIfRunBelongsToFieldCode(Run run)

{

bool isInside = false;

foreach (Field field in run.Document.Range.Fields)

{

Node currentNode = field.Start;

while (currentNode != field.Separator && !isInside)

{

if (currentNode.NodeType == NodeType.Run)

if (currentNode.Equals(run))

isInside = true;

Node nextNode = currentNode.NextPreOrder(currentNode.Document);

currentNode = nextNode;

}

}

return isInside;

}


Best regards,

Thanks, this is very helpful!