Hello,
Is it possible to Convert Field to Text akka ctrl+shift+F9 in MS Word with word document?
Thanks,
//Load word document
Document document = new Document();
document.LoadFromFile(@"in.docx");
//Get the first field
FieldCollection fields = document.Fields;
Field field = fields[0];
//Convert field to text
string s = field.FieldText;
int index = field.OwnerParagraph.ChildObjects.IndexOf(field);
TextRange textRange = new TextRange(document);
textRange.Text = s;
textRange.CharacterFormat.FontSize = 24f;
field.OwnerParagraph.ChildObjects.Insert(index, textRange);
field.OwnerParagraph.ChildObjects.Remove(field);
//Save the file.
document.SaveToFile(@"out.docx", FileFormat.Docx);
public TextRange convertFieldToText(Field field, Document document) {
TextRange tr = new TextRange(document);
tr.applyCharacterFormat(field.getCharacterFormat());
tr.setText(field.getText());
int index = field.getOwnerParagraph().getChildObjects().indexOf(field);
field.getOwnerParagraph().getChildObjects().insert(index, tr);
field.getOwnerParagraph().getChildObjects().remove(field);
return tr;
}