How to Move Paragraph Content Above Table with same Formatting using .NET

Hi,

I’m trying to move content outside the table, when I try to do that I’m loosing style of subscripted content is there a way to retain entire line as ease without changing styles?

Hi,

PFA of docsdocs.zip (22.6 KB)

@kkumaranil485

Please note that Aspose.Words mimics the behavior of MS Word. If you move the content from table using MS Word, you will get the same output.

Could you please attach the following resources here for further testing?

  • Please attach screenshot of problematic section of output document.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a standalone console application (source code without compilation errors) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Hi,

PFA of documents
tableTitle.zip (753.2 KB)

Hi,

Please refer the code used.

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);


            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)
                    {
                        t.ParentNode.InsertBefore(new Paragraph(doc), t);
                        builder.MoveTo(t.PreviousSibling);
                        builder.InsertBreak(BreakType.ParagraphBreak);
                        builder.InsertField(@"SEQ Table \* ARABIC", "");
                        builder.Write(rowText);
                        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
                        t.Rows[0].Remove();
                    }
                    else
                    {
                        t.ParentNode.InsertBefore(new Paragraph(doc), t);
                        builder.MoveTo(t.PreviousSibling);
                        builder.InsertBreak(BreakType.ParagraphBreak);
                        builder.InsertField(@"SEQ Table \* ARABIC", "");
                        builder.Write(rowText);
                        builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
                        t.Rows[0].Remove();
                    }
                }
            }
            string dataDir = @"D:/PWS/Automation/ConvertedDOC.docx";
            doc.Save(dataDir);

        }
    }
}

@kkumaranil485

You can use Document.StartTrackRevisions method as shown below to achieve your requirement. Moreover, we suggest you please read the following article.

Aspose.Words.Document doc = new Aspose.Words.Document(MyDir  + "inputdoc.docx");
doc.StartTrackRevisions("aw");
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)
        {
            t.ParentNode.InsertBefore(new Paragraph(doc), t);
            builder.MoveTo(t.PreviousSibling);
            builder.InsertBreak(BreakType.ParagraphBreak);
            builder.InsertField(@"SEQ Table \* ARABIC", "");
            builder.Write(rowText);
            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
            t.Rows[0].Remove();
        }
        else
        {
            t.ParentNode.InsertBefore(new Paragraph(doc), t);
            builder.MoveTo(t.PreviousSibling);
            builder.InsertBreak(BreakType.ParagraphBreak);
            builder.InsertField(@"SEQ Table \* ARABIC", "");
            builder.Write(rowText);
            builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Caption;
            t.Rows[0].Remove();
        }
    }
}
                
doc.Save(MyDir + "21.9.docx");

@tahir.manzoor

Issue is not Track Revisions. But subscripted value are appending back as normal text.

@kkumaranil485

You are inserting the row text as simple text using DocumentBuilder.Write method. So, it is inserted with normal style. You need to clone the nodes of table’s cell and insert them using CompositeNode.InsertAfter method.

Following code example shows how to insert cells paragraphs before table. Hope this helps you.

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

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();

Hi,

Thank you that worked.