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.

Thu Feb 26, 2026 3:43 pm

Hello,

My name is Dmytro, and I am currently developing a project that uses Spire.Doc for .NET 13.12 . We use document template logic frequently and store the generated document in PDF and Word formats. In templates, we often use fill-in and if fields. In the generated Pdf file, the result value for fill-in and if fields are appropriately stored. But for the generated Word document, the fields themselves stay in the file. That is why we remove fill-in fields from the generated file and insert the TextArea field that contains the 'FieldText' property value. But for the if fields, no property contains a result value available to it. Could you provide a solution to get a result value for If fields? Or the possibility to replace the if fields with the result value?

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Fri Feb 27, 2026 2:21 am

Hello Dmytro,

Thank you for your inquiry.

Could you please provide your Word document? This will help us analyze the specific types of field codes contained within it. Additionally, please provide examples of the replacement values you expect for these fields. This information will enable us to offer you a precise solution. Thanks for your assistance.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Mon Mar 02, 2026 7:02 am

Hello Amy,

In the attached file, you can find an example of an if field that is frequently used in a template document. It also contains an example of the expected result.

Kind Regards,
Dmytro

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Mon Mar 02, 2026 9:27 am

Hello Dmytro,

Thank you for your assistance.

Through testing, I have confirmed the issue regarding the failure to read the IF field value. Additionally, I discovered that after populating the MergedField value, saving the result document, and reopening it to read (as shown in the code snippet below), the value can be retrieved via ifField.FieldText. However, the result obtained is "Dmytro * MERGEFORMAT " instead of just "Dmytro".

Code: Select all
Document document = new Document();
document.LoadFromFile(path + "If field example.docx");

String[] fieldName = { "FormattedName" };
String[] fieldValue = { "Dmytro" };

document.MailMerge.Execute(fieldName, fieldValue);
document.IsUpdateFields = true;

MemoryStream memoryStream = new MemoryStream();

document.SaveToStream(memoryStream, FileFormat.Docx2019);
document = new Document();
document.LoadFromStream(memoryStream, FileFormat.Auto);

Body body = document.Sections[0].Body;

for (int i = 0; i < body.ChildObjects.Count; i++)
{
    if (body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
    {
        Paragraph paragraph = (Paragraph)body.ChildObjects[i];
        for (int j = 0; j < paragraph.ChildObjects.Count; j++)
        {
            if(paragraph.ChildObjects[j].DocumentObjectType == DocumentObjectType.Field)
            {
                Field field = (Field)paragraph.ChildObjects[j];
                if (field.Type == FieldType.FieldIf)
                {
                    IField ifField = (IField)field;
                    String fieldText = field.FieldText;
                }
            }   
        }
    } 
}


I have logged both identified issues in our tracking system under ticket number SPIREDOC-11845. Our development team will conduct a thorough investigation and work on a resolution. We will keep you updated on the progress.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Mon Mar 02, 2026 11:09 am

Hello Amy,

Thank you for your code example. After reviewing it, I discovered the issue in my code. I discovered that after MailMerge execution, you save the file to a memory stream and reload it. I used the same approach, and it helps me get the correct result text from the 'FieldText' property for if fields.

Kind Regards,
Dmytro

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Tue Mar 03, 2026 2:12 am

Hi Dmytro,

You're very welcome. I'm glad to hear that the code example helped you identify the issue.

Additionally, regarding the underlying issue you encountered, we are tracking this under ticket SPIREDOC-11845. We will be sure to inform you as soon as it is resolved.

If you have any further questions in the meantime, feel free to reach out.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Mon Mar 16, 2026 8:26 am

Hello Amy,

Can you please take a look at the current template example? I receive an error for this if-field structure. Also wondering if there's any other way to initialise the if-field result without saving the file to a memory stream and reloading it?

Kind Regards,
Dmytro

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Mon Mar 16, 2026 10:19 am

Hello Dmytro,

Thank you for your feedback.

I tested your new tempate.docx document using the previous code and was unable to reproduce the error you encountered. Please check if there are any differences between my code and yours. Regarding issue SPIREDOC-11845, our development team is currently investigating it. We will provide you with an update as soon as the issue is resolved.

Code: Select all
Document document = new Document();
document.LoadFromFile(path + "Template.docx");

String[] fieldName = { "IdentificatieNummer", "IdentificatieSoort" , "VerblijfsvergunningNummer", "WerkvergunningNummer" };
String[] fieldValue = { "123456" , "test1" , "test2", "test3", "test4" };

document.MailMerge.Execute(fieldName, fieldValue);
document.IsUpdateFields = true;
MemoryStream memoryStream = new MemoryStream();

document.SaveToStream(memoryStream, FileFormat.Docx);
document = new Document();
document.LoadFromStream(memoryStream,FileFormat.Auto);

Body body = document.Sections[0].Body;


for (int i = 0; i < body.ChildObjects.Count; i++)
{
    if (body.ChildObjects[i].DocumentObjectType == DocumentObjectType.Paragraph)
    {
        Paragraph paragraph = (Paragraph)body.ChildObjects[i];
        for (int j = 0; j < paragraph.ChildObjects.Count; j++)
        {
            if (paragraph.ChildObjects[j].DocumentObjectType == DocumentObjectType.Field)
            {
                Field field = (Field)paragraph.ChildObjects[j];
                if (field.Type == FieldType.FieldMergeField)
                {
                    MergeField mergeField = (MergeField)field;
                }
                if (field.Type == FieldType.FieldIf)
                {
                    IField ifField = (IField)field;
                    String fieldText = field.FieldText;
                }
            }
        }
    }
}



Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Mon Mar 16, 2026 11:46 am

Hi Amy,

Can I omit this part of the code and still get result value in "field.FieldText" for IfField:

Code: Select all
MemoryStream memoryStream = new MemoryStream();
document.SaveToStream(memoryStream, FileFormat.Docx);
document = new Document();
document.LoadFromStream(memoryStream,FileFormat.Auto);


If yes, can you give some examples?

My goal is to remove IfField and replace it with "field.FieldText". Can you give me an example?
Code: Select all
if (field.Type == FieldType.FieldFillIn) {
   // Get the result text of the field
   string fieldResult = field.FieldText;

   // Replace logic
}


I'm using this code for it:
Code: Select all
Field field = templateDocument.Fields[i];

if (field.Type == FieldType.FieldFillIn) {
   // Get the result text of the field
   string fieldResult = field.FieldText;

   // Create a new text range with the result
   TextRange tr = new TextRange(templateDocument);
   tr.Text = fieldResult;
   tr.ApplyStyle(field.StyleName);
   tr.ApplyCharacterFormat(field.CharacterFormat);

   // Replace the field with its result
   Paragraph ownerParagraph = field.OwnerParagraph;
   int index = ownerParagraph.ChildObjects.IndexOf(field);
   ownerParagraph.ChildObjects.Insert(index, tr);
   ownerParagraph.ChildObjects.Remove(field);
}


I got a "Stack empty" error while saving the document into a stream as a PDF after ifFields was replaced by TextRange.
Code: Select all
ToPdfParameterList toPdf = new ToPdfParameterList {
   UsePSCoversion = true,
   IsEmbeddedAllFonts = true
};
templateDocument.SaveToStream(outputStream, toPdf);


Sincerely,
Dmytro

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Tue Mar 17, 2026 10:15 am

Hello Dmytro,

Thank you for sharing more information.

I have successfully reproduced the exception you encountered and logged it in our issue tracking system under ticket number SPIREDOC-11859. We apologize for the inconvenience this has caused. We will implement a fix that does not require the code snippet provided below.

Code: Select all
MemoryStream memoryStream = new MemoryStream();
document.SaveToStream(memoryStream, FileFormat.Docx);
document = new Document();
document.LoadFromStream(memoryStream,FileFormat.Auto);


Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Tue Mar 17, 2026 10:34 am

Hi Amy.

Thank you for the information. Glad that my explanation helped you. Waiting for a fix.

Is my example of replacing fields with TextRange the right way to do it?

Sincerely,
Dmytro

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Thu Mar 19, 2026 1:26 am

Hello Dmytro,

Thank you for your reply.

Removing the field structure and retaining only the field value is the correct approach, your method of replacing the field with a new TextRange is accurate. We will notify you once SPIREDOC-11859 has been resolved.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Thu May 28, 2026 7:20 am

Hello,

Thank you for your patience.

We have added a new method doc.UnlinkFields() to remove the field structure and keep only the result. Welcome to download and test [Spire.Doc Pack (hot fix) Version: 14.5.14].
Our website link: https://www.e-iceblue.com/Download/download-word-for-net-now.html
NuGet link: https://www.nuget.org/packages/Spire.Doc/14.5.14
https://www.nuget.org/packages/Spire.Docfor.NETStandard/14.5.14

Sample code:
Code: Select all
String[] fieldName = { "FormattedName" };
String[] fieldValue = { "Dmytro" };
doc.MailMerge.Execute(fieldName, fieldValue);
doc.Fields[0].Update();
doc.UnlinkFields();


If you have any questions, please let us know.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Fri May 29, 2026 5:54 am

Hi Amy.

Thank you for this news. I will try to use this new method ASAP.

Sincerely,
Dmytro

dchabanSharpminds
 
Posts: 7
Joined: Thu Feb 26, 2026 1:20 pm

Tue Jun 02, 2026 2:43 am

Hi Dmytro,

Just following up to see if you've had a chance to test the new method doc.UnlinkFields()?
Please let me know if it resolves your issue. I look forward to your feedback.

Sincerely,
Amy
E-iceblue support team
User avatar

amy.zhao
 
Posts: 3004
Joined: Wed Jun 27, 2012 8:50 am

Return to Spire.Doc