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.

Wed Nov 08, 2023 9:58 pm

Hi.

We recently updated from Spire.doc v5.8 -- where we had great success in converting DOCX templates containing Word Merge IF THEN ELSE structures into PDF documents for storage.

However - after updating to Spire.doc v11.9 -- it appears that IF THEN ELSE structures will not evaluate during DOCX to PDF - and we are only seeing the "False" associated elements. No structures evaluate to "True"

Appreciate any thoughts on this topic.....

billweils
 
Posts: 3
Joined: Fri Apr 07, 2017 6:17 pm

Thu Nov 09, 2023 3:45 am

Hi,

Thanks for your inquiry.

Please refer to the following code for reference.
Code: Select all
//Create a Word document.
Document document = new Document();

//Add a new section.
Section section = document.AddSection();

//Add a new paragraph.
Paragraph paragraph = section.AddParagraph();

// Define a method of creating an IF Field.
CreateIfField(document, paragraph);

//Define merged data.
string[] fieldName = { "Count" };
string[] fieldValue = { "2" };

//Merge data into the IF Field.
document.MailMerge.Execute(fieldName, fieldValue);

//Update all fields in the document.
document.IsUpdateFields = true;

string result = "Result-CreateAnIFField.docx";

//Save to file.
document.SaveToFile(result, FileFormat.Docx2013);

//Create the IF Field like:{IF { MERGEFIELD Count } > "100" "Thanks" " The minimum order is 100 units "}
void CreateIfField(Document document, Paragraph paragraph)
{
    IfField ifField = new IfField(document);
    ifField.Type = FieldType.FieldIf;
    ifField.Code = "IF ";

    paragraph.Items.Add(ifField);
    paragraph.AppendField("Count", FieldType.FieldMergeField);
    paragraph.AppendText(" > ");
    paragraph.AppendText("\"100\" ");
    paragraph.AppendText("\"Thanks\" ");
    paragraph.AppendText("\"The minimum order is 100 units\"");

    IParagraphBase end = document.CreateParagraphItem(ParagraphItemType.FieldMark);
    (end as FieldMark).Type = FieldMarkType.FieldEnd;
    paragraph.Items.Add(end);
    ifField.End = end as FieldMark;
}


If the code does not meet your requirement, please share us with your original code. Thanks for your assistance.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Thu Nov 09, 2023 10:55 pm

Thank you for your response. However, we are not using Spire.Doc to create the Word document.

We are only using Spire.Doc to read an existing Word.docx and convert to PDF.

Thus, our code looks like this:

Document document = new Document();
document.LoadFromFile(pathSrc);
ToPdfParameterList ppl = new ToPdfParameterList();
ppl.IsEmbeddedAllFonts = true;
document.SaveToFile(pathDst, ppl);

The Word document at pathSrc already has IF THEN ELSE fields in it.

These IF fields were correctly interpreted by Spire.Doc v5.8.

However, after our upgrade to Spire.Doc v11.9, all IF conditions are being interpreted as “false”.

billweils
 
Posts: 3
Joined: Fri Apr 07, 2017 6:17 pm

Fri Nov 10, 2023 7:05 am

Hello,

Thanks for your feedback.
I made a sample Word that contains IF conditions and converted it to PDF using your code, but didn't reproduce your issue. To help us investigate your issue accurately and timely, please provide your input Word document. You can attached your file here or send to our email ([email protected]). Thanks in advance.

Sincerely,
Nina
E-iceblue support team
User avatar

Nina.Tang
 
Posts: 1379
Joined: Tue Sep 27, 2016 1:06 am

Fri Nov 10, 2023 8:51 pm

Thank you for taking a closer look.

Here is a Word docx we are working with -- when submitted to Spire 5.8 using the code from earlier post -- it outputs as expected in PDF format.

However - when submitted to Spire 11.9 - the PDF output shows all IF THEN ELSE evaluated as the "False" option.

billweils
 
Posts: 3
Joined: Fri Apr 07, 2017 6:17 pm

Mon Nov 13, 2023 2:50 am

Hi,

Thanks for your feedback.

In the latest version, some Word fields will not automatically update. To resolve this issue, please use the following method before saving the document:
Code: Select all
doc.IsUpdateFields = true
This will ensure that the fields are updated before the conversion process.

If you have any further questions or encounter any other issues, please don't hesitate to reach out to us. We are here to assist you at any time.

Best regards,
Triste
E-iceblue support team
User avatar

Triste.Dai
 
Posts: 1000
Joined: Tue Nov 15, 2022 3:59 am

Return to Spire.Doc