How to Add TabStop after SEQ Field using .NET

Hi team,

I was not able to find the desired output that is a table title is inserted as caption. In that i need to add a space and a tab after table number that is “Table 1.\t” where \t is Tab space where aspose.range.text have content "Table SEQ Table * ARABIC 1 " how to can we check for pattern Table 1 without peroid and tab or Table 1. without tab or Table 1 without period with tab and replace it with “Table 1.\t” and also remove period at end of table title.

Hi,

PFA of documents TableIssue.zip (51.7 KB)

and code i have tried

using Aspose.Words;
using Aspose.Words.Drawing;
using Aspose.Words.Fields;
using Aspose.Words.Lists;
using Aspose.Words.Replacing;
using Aspose.Words.Tables;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
using Microsoft.Office.Interop.Word;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Linq;
using Paragraph = Aspose.Words.Paragraph;
using Table = Aspose.Words.Tables.Table;

namespace AsposeLibraryWord
{
    class Program
    {
        static void Main(string[] args)
        {
            string fileName = @"D:/PWS/Automation/inputdoc.docx";
            Aspose.Words.Document doc = new Aspose.Words.Document(fileName);

            doc.StartTrackRevisions("Test");
            DocumentBuilder builder = new DocumentBuilder(doc);

            foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
            {

                string rowText = t.Rows[0].ToString(SaveFormat.Text).Trim();
                var match = Regex.Match(rowText, @"^(Table\s(\d+))");
                if (match.Success)
                {

                    if (t.PreviousSibling == null)
                    {
                        Paragraph node = (Paragraph)t.ParentNode.InsertBefore(new Paragraph(doc), t);
                        builder.MoveTo(t.PreviousSibling);
                        builder.InsertField(@"SEQ Table \* ARABIC", "");

                        foreach (Aspose.Words.Tables.Cell cell in t.Rows[0].Cells)
                        {
                            foreach (Paragraph para in cell.Paragraphs)
                            {
                                node = (Paragraph)node.ParentNode.InsertAfter(para.Clone(true), node);
                            }
                        }
                        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
                        t.Rows[0].Remove();
                    }
                    else
                    {
                        Paragraph node = (Paragraph)t.ParentNode.InsertBefore(new Paragraph(doc), t);
                        builder.MoveTo(t.PreviousSibling);
                        builder.InsertField(@"SEQ Table \* ARABIC", "");

                        foreach (Aspose.Words.Tables.Cell cell in t.Rows[0].Cells)
                        {
                            foreach (Paragraph para in cell.Paragraphs)
                            {
                                node = (Paragraph)node.ParentNode.InsertAfter(para.Clone(true), node);
                            }
                        }
                        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
                        t.Rows[0].Remove();
                    }
                }
            }
            foreach (Table tbl in doc.GetChildNodes(NodeType.Table, true))
            {
                //check for previous paragraph is table title
                Paragraph caption = tbl.PreviousSibling as Paragraph;
                if (caption != null)
                {
                    string rowText = caption.ToString(SaveFormat.Text).Trim();
                    var match = Regex.Match(rowText, @"^(Table\s(\d+))");
                    if (match.Success)
                    {
                        FindReplaceOptions options = new FindReplaceOptions();
                        options.IgnoreDeleted = true;
                        options.UseSubstitutions = true;
                        //check for pattern Table 1 without peroid and tab or Table 1. without tab or Table 1 without period with tab
                        caption.Range.Replace(new Regex(@"^(Table\s+(\d+)(\.)?((\t)|(\s+))?)"), "Table $2.\t",options);
                        
                        //to remove period at end of table title
                        caption.Range.Replace(new Regex(@"\.\r"), "", options);

                    }
                }
            }
            doc.StopTrackRevisions();
            string dataDir = @"D:/PWS/Automation/ConvertedDOC.docx";
            doc.Save(dataDir);

        }
    }
}

@kkumaranil485

In your case, you need to get the SEQ field from the paragraph and insert full stop and tab (\t) after it. Following code example shows how to find the SEQ field from the paragraph and insert tab character. This code example also removes the full stop at the end of paragraph. You can use the same approach to get the desired output.

Document doc = new Document(MyDir + "inputdoc.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
{

    string rowText = t.Rows[0].ToString(SaveFormat.Text).Trim();
    var match = Regex.Match(rowText, @"^(Table\s(\d+))");
    if (match.Success)
    {
        Paragraph node = (Paragraph)t.ParentNode.InsertBefore(new Paragraph(doc), t);
        builder.MoveTo(t.PreviousSibling);
        builder.InsertBreak(BreakType.ParagraphBreak);
        foreach (Cell cell in t.Rows[0].Cells)
        {
            foreach (Paragraph para in cell.Paragraphs)
            {
                node = (Paragraph)node.ParentNode.InsertAfter(para.Clone(true), node);
            }
        }

        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
        t.Rows[0].Remove();

        //Insert dot (.) and tab after SEQ field.
        foreach (Field field in node.Range.Fields)
        {
            if (field.Type == FieldType.FieldSequence)
            {
                field.End.ParentNode.InsertAfter(new Run(doc, ". \t"), field.End);
                break;
            }
        }

        //Remvoe full stop from the paragraph.
        node.LastChild.Range.Replace(".", "", new FindReplaceOptions());
    }
}

doc.Save(MyDir + "21.9.docx");

Hi,

In the same thread I need add period only if there is no period after Field Sequence and a tab only if there is no tab, how can achieve this.

@kkumaranil485

Please note that all text of the document is stored in runs of text. You can add Run node at your desired location in the paragraph using CompositeNode.InsertAfter method with same approach shared in my previous post.

Hi @tahir.manzoor,

Could you please provide with example please didn’t find how to resolve the above. After sequence number if period exists no need of adding period similarly if a tab exists stop adding tab and vice versa.

Here what stopping me to add condition is how to check whether period or tab exists or not soon after sequence number.

Hi @tahir.manzoor,

PFA of below documents.
AddingPeriodAndTab.zip (29.2 KB)

@kkumaranil485

Following code example shows how to insert dot and tab characters according to your three cases. Hope this helps you.

Your input document does not contain the SEQ field for all tables. So, we added it before each table. Please check the attached input and output documents.
21.9.docx (14.7 KB)
inputdoc_period_and_Tab.docx (17.5 KB)

Document doc = new Document(MyDir + "inputdoc_period_and_Tab.docx");
foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
{
    Paragraph node = (Paragraph)t.PreviousSibling;
    foreach (Field field in node.Range.Fields)
    {
        if (field.Type == FieldType.FieldSequence && field.End.NextSibling != null
            && field.End.NextSibling.NodeType == NodeType.Run)
        {
            Run run = (Run)field.End.NextSibling;
            if (run.Text.StartsWith("."))
            {
                run.Range.Replace(".", ". \t");
            }
            else if (run.Text.StartsWith(ControlChar.Tab))
            {
                field.End.ParentNode.InsertAfter(new Run(doc, "."), field.End);
            }
            else
            {
                field.End.ParentNode.InsertAfter(new Run(doc, ". \t"), field.End);
            }
            break;
        }
    }
}
doc.Save(MyDir + "21.9.docx");

Hi @tahir.manzoor

For above you mentioned “document does not contain the SEQ field for all tables strong text” how can we add this part programmatically before performing this operation.

@kkumaranil485

Please move the cursor to the desired location of document and insert the SEQ field using InsertField method as shown below.

We suggest you please read the following articles.

Document doc = new Document(MyDir + "inputdoc_period_and_Tab.docx");
NodeCollection paragraphs = doc.GetChildNodes(NodeType.Paragraph, true);
DocumentBuilder builder = new DocumentBuilder(doc);

foreach (Table t in doc.GetChildNodes(NodeType.Table, true))
{
    Paragraph node = (Paragraph)t.PreviousSibling;
    foreach (Field field in node.Range.Fields)
    {
        if (field.Type == FieldType.FieldSequence && field.End.NextSibling != null
            && field.End.NextSibling.NodeType == NodeType.Run)
        {
            Run run = (Run)field.End.NextSibling;
            if (run.Text.StartsWith("."))
            {
                run.Range.Replace(".", ". \t");
            }
            else if (run.Text.StartsWith(ControlChar.Tab))
            {
                field.End.ParentNode.InsertAfter(new Run(doc, "."), field.End);
            }
            else
            {
                field.End.ParentNode.InsertAfter(new Run(doc, ". \t"), field.End);
            }
            break;
        }
    }

    if (node.Range.Fields.Count == 0)
    {
        builder.MoveToParagraph(paragraphs.IndexOf(node), 0);
        FieldSeq fieldSeq = (FieldSeq)builder.InsertField(FieldType.FieldSequence, true);
        fieldSeq.SequenceIdentifier = "Table";
    }
}
doc.UpdateFields();
doc.Save(MyDir + "21.10.docx");