Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files. Get free and professional technical support for Spire.Doc for .NET, Java, Android, C++, Python.

Tue Apr 30, 2024 11:50 am

Hello,

Is it possible to Convert Field to Text akka ctrl+shift+F9 in MS Word with word document?

Thanks,

BranislavNemec
 
Posts: 5
Joined: Fri Apr 26, 2024 10:07 am

Wed May 01, 2024 2:25 am

Hello,

Thanks for your inquiry.
our Spire.Doc supports converting word fields to text. You can refer to the code below for testing.

Code: Select all
 //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);

If you have any other questions, you can provide your test document for further testing. You can send them here, or to the email [email protected].

Sincerely,
Lisa
E-iceblue support team
User avatar

Lisa.Li
 
Posts: 1510
Joined: Wed Apr 25, 2018 3:20 am

Sun May 05, 2024 1:27 pm

Hi Lisa,

Thanks a lot for your solution!
It fits my requirement.

Here I enclose my Java-version helper method inspired by your code:

Code: Select all
   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;
   }


Sincerely,
Branislav;

BranislavNemec
 
Posts: 5
Joined: Fri Apr 26, 2024 10:07 am

Return to Spire.Doc

cron