Aspose.Words.Examples.CSharp.Mail_Merge.ApplyCustomLogicToEmptyRegions.EmptyRegionsHandler_MergeTable.FieldMerging C# (CSharp) Method

FieldMerging() public method

Called for each field belonging to an unmerged region in the document.
public FieldMerging ( FieldMergingArgs args ) : void
args FieldMergingArgs
return void
            public void FieldMerging(FieldMergingArgs args)
            {
                 // ExStart:RemoveExtraParagraphs
                // Store the parent paragraph of the current field for easy access.
                Paragraph parentParagraph = args.Field.Start.ParentParagraph;

                // Define the logic to be used when the ContactDetails region is encountered.
                // The region is removed and replaced with a single line of text stating that there are no records.
                if (args.TableName == "ContactDetails")
                {
                    // Called for the first field encountered in a region. This can be used to execute logic on the first field
                    // In the region without needing to hard code the field name. Often the base logic is applied to the first field and 
                    // Different logic for other fields. The rest of the fields in the region will have a null FieldValue.
                    if ((string)args.FieldValue == "FirstField")
                    {
                        FindReplaceOptions options = new FindReplaceOptions();
                        // Remove the "Name:" tag from the start of the paragraph
                        parentParagraph.Range.Replace("Name:", string.Empty, options);
                        // Set the text of the first field to display a message stating that there are no records.
                        args.Text = "No records to display";
                    }
                    else
                    {
                        // We have already inserted our message in the paragraph belonging to the first field. The other paragraphs in the region
                        // will still remain so we want to remove these. A check is added to ensure that the paragraph has not already been removed.
                        // which may happen if more than one field is included in a paragraph.
                        if (parentParagraph.ParentNode != null)
                            parentParagraph.Remove();
                    }
                }
                // ExEnd:RemoveExtraParagraphs
                // ExStart:MergeAllCells
                // Replace the unused region in the table with a "no records" message and merge all cells into one.
                if (args.TableName == "Suppliers")
                {
                    if ((string)args.FieldValue == "FirstField")
                    {
                        // We will use the first paragraph to display our message. Make it centered within the table. The other fields in other cells 
                        // within the table will be merged and won't be displayed so we don't need to do anything else with them.
                        parentParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center;
                        args.Text = "No records to display";
                    }

                    // Merge the cells of the table together. 
                    Cell cell = (Cell)parentParagraph.GetAncestor(NodeType.Cell);
                    if (cell != null)
                    {
                        if (cell.IsFirstCell)
                            cell.CellFormat.HorizontalMerge = CellMerge.First; // If this cell is the first cell in the table then the merge is started using "CellMerge.First".
                        else
                            cell.CellFormat.HorizontalMerge = CellMerge.Previous; // Otherwise the merge is continued using "CellMerge.Previous".
                    }
                }
                // ExEnd:MergeAllCells
            }
ApplyCustomLogicToEmptyRegions.EmptyRegionsHandler_MergeTable