Spire.Doc is a professional Word .NET library specifically designed for developers to create, read, write, convert and print Word document files from any .NET platform (C#, VB.NET, ASP.NET, .NET Core) and Java applications (J2SE and J2EE) with fast and high quality performance.

Fri Sep 24, 2021 8:15 am

Hi
How can I delete the content of a word document based on the condition?
For example this is my condition in docx file:

{%if age==”30”%}
{%if gender==”male”%}
Adult - male
{%endif%}
{%if gender==”female”%}
Adult - female
{%endif%}
{%endif%}

And this os output that i wanted(if age variable equal to 30 and gender equal to male) :
Adult - male

Johnmc13
 
Posts: 2
Joined: Fri Sep 24, 2021 8:11 am

Fri Sep 24, 2021 10:12 am

Hello,

Thanks for your inquiry.
For your requirements, we recommend that you use if fields and nest mail merge fields to achieve your needs. The sample code is as follows.
Code: Select all
            Document document = new Document();
            Section section = document.AddSection();
            Paragraph paragraph = section.AddParagraph();

            IfField ifField = new IfField(document);
            ifField.Type = FieldType.FieldIf;
            ifField.Code = "IF ";
            paragraph.Items.Add(ifField);
            paragraph.AppendField("age", FieldType.FieldMergeField);
            paragraph.AppendText(" = \"30\" ");

            IfField ifField2 = new IfField(document);
            ifField2.Type = FieldType.FieldIf;
            ifField2.Code = "IF ";
            paragraph.ChildObjects.Add(ifField2);
            paragraph.Items.Add(ifField2);
            paragraph.AppendField("gender", FieldType.FieldMergeField);
            paragraph.AppendText(" = \"male\"  \"Adult - male\"  \"\" ");
            IParagraphBase embeddedEnd = document.CreateParagraphItem(ParagraphItemType.FieldMark);
            (embeddedEnd as FieldMark).Type = FieldMarkType.FieldEnd;
            paragraph.Items.Add(embeddedEnd);
            ifField2.End = embeddedEnd as FieldMark;

            IfField ifField3 = new IfField(document);
            ifField3.Type = FieldType.FieldIf;
            ifField3.Code = "IF ";
            paragraph.ChildObjects.Add(ifField3);
            paragraph.Items.Add(ifField3);
            paragraph.AppendField("gender", FieldType.FieldMergeField);
            paragraph.AppendText(" = \"female\"  \"Adult - female\"  \"\" ");
            IParagraphBase embeddedEnd2 = document.CreateParagraphItem(ParagraphItemType.FieldMark);
            (embeddedEnd2 as FieldMark).Type = FieldMarkType.FieldEnd;
            paragraph.Items.Add(embeddedEnd2);
            ifField3.End = embeddedEnd2 as FieldMark;

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

            string[] fieldNames = new string[] { "age", "gender"};

            string[] fieldValues = new string[] { "20", "male" };

            document.MailMerge.Execute(fieldNames, fieldValues);
            document.IsUpdateFields = true;
            document.SaveToFile("result.docx", FileFormat.Docx);

If there are any other issues related to our products, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
User avatar

Brian.Li
 
Posts: 1253
Joined: Mon Oct 19, 2020 3:04 am

Fri Sep 24, 2021 1:17 pm

I have tried this solution before but I do not think it will solve the problem.
The output after each condition can include a photo, table, figure, and so on based on what user add in document.
In fact, I want to create nested conditions on the document and the content of the document will be processed and changed by the user.

Johnmc13
 
Posts: 2
Joined: Fri Sep 24, 2021 8:11 am

Sun Sep 26, 2021 11:14 am

Hello,

Thanks for your feedback.
According to your needs, we recommend that you modify your template file like the attached file and refer to the following sample code for testing.
Code: Select all
        static void Main(string[] args)
        {
            Document document = new Document();
            document.LoadFromFile(@"sample.docx");

            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>();
            keyValuePairs.Add("age", "0");
            keyValuePairs.Add("gender", "male");

            foreach (KeyValuePair<string, string> kvp in keyValuePairs)
            {
                DeleteContent(document, kvp.Key, kvp.Value);
            }
            document.SaveToFile("result.docx", FileFormat.Docx);
        }

        public static void DeleteContent(Document doc, string key, string value)
        {
            //Find the contents that contain the string "{%if [[key]]"
            TextSelection[] textSelection = doc.FindAllString("{%if [[" + key + "]]", false, false);
            if (textSelection != null)
            {
                for (int i = 0; i < textSelection.Length; i++)
                {
                    //Find the index of the paragraph to which the string "{%if [[key]]" belongs
                    TextSelection textSelection1 = textSelection[i];
                    Paragraph ownerParagraph1 = textSelection1.GetAsOneRange().OwnerParagraph;
                    int ifIndex = ownerParagraph1.OwnerTextBody.ChildObjects.IndexOf(ownerParagraph1);

                    //Find the first index of the paragraph to which the string "{%endif [[key]]" belongs
                    TextSelection textSelection2 = doc.FindString("{%endif [[" + key + "]]", false, false);
                    Paragraph ownerParagraph2 = textSelection2.GetAsOneRange().OwnerParagraph;
                    int endIfIndex = ownerParagraph2.OwnerTextBody.ChildObjects.IndexOf(ownerParagraph2);

                    //Find the content contained in the second "[[]]" in the paragraph to which the string "{%if [[key]]" belongs
                    Match match = Regex.Matches(ownerParagraph1.Text, @"\[\[\w+\]\]")[1];
                    string val = match.Value.Replace("[[", "").Replace("]]", "");

                    //If the content consistent with "if[[key]]" can be found in the document, only the current "if" paragraph and the corresponding "endif" paragraph will be deleted
                    //Otherwise delete the current "if" paragraph and the corresponding "endif" paragraph and all the content between them
                    if (val.Equals(value))
                    {
                        ownerParagraph2.OwnerTextBody.ChildObjects.RemoveAt(endIfIndex);
                        ownerParagraph1.OwnerTextBody.ChildObjects.RemoveAt(ifIndex);
                    }
                    else
                    {
                        Body ownerTextBody = ownerParagraph2.OwnerTextBody;
                        for (int j = endIfIndex; j >= ifIndex; j--)
                        {
                            ownerTextBody.ChildObjects.RemoveAt(j);
                        }
                    }
                }
            }
        }

If there are any other issues related to our products, just feel free to contact us.

Sincerely,
Brian
E-iceblue support team
You do not have the required permissions to view the files attached to this post.
User avatar

Brian.Li
 
Posts: 1253
Joined: Mon Oct 19, 2020 3:04 am

Return to Spire.Doc

cron