Extracting captions from table and centering them

What I want to do is I want to extract caption and center align them from each image but if one of the table does not have caption, then the next simple paragraph(or any text) that will also get center aligned.
Is there any way to solve this problem?

@alexey.noskov

@Amrinder_Singh can you please attach an example of a document input and the expected output.

@eduardo.canal

Input : -
image.png (6.5 KB)
Output should be same as I input but its coming captions should be centered aligned when i download the file
What is happening right now is that the caption is coming as nextSibling of table and separate paragraph.
So I tried to center align the next paragraph. But then the line which is not the caption of the table(added in image) is also being centered which is not the caption of table
Right now output is like that image.png (7.0 KB)
But i want it same as input after downloading file

@Amrinder_Singh Thank you for providing the additional information. Please note that captions use SEQ fields. One way to check if the paragraph after the table is a caption is to verify if the paragraph contains a FieldSequence field. Here’s an example:

Document doc = new Document(@"C:\Temp\input.docx");

var tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table table in tables)
{
    var p = table.NextSibling;
    if(p.NodeType == NodeType.Paragraph)
    {
        Paragraph para = (Paragraph)p;
        var fields = para.Range.Fields;
        if(fields.Any(f => f.Type == Aspose.Words.Fields.FieldType.FieldSequence))
        {
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        }
    }
}

doc.Save(@"C:\Temp\output.docx");

@eduardo.canal
I tried this code but fields are not coming. They are null onlyimage.png (5.9 KB)

@Amrinder_Singh Could you please attach your input document here for testing. Unfortunately, it is difficult to analyze the issue by screenshots.

Table2.0.docx (21.2 KB)

This is the file with which im testing.
I inserted 1 Table and to that table 1 inserted 1 caption also
(if you want to create or modify or add caption of your own choose table go to reference tab and insert Caption)

@Amrinder_Singh Please note that in your case, the caption is before the table. Therefore, you need to look for the PreviousSibling instead of the NextSibling . Alternatively, you can search for both in case you are uncertain about the exact location of the caption in the document. For example:

Document doc = new Document(@"C:\Temp\input.docx");

var tables = doc.GetChildNodes(NodeType.Table, true);
foreach (Table table in tables)
{
    var p = table.NextSibling;
    if(p.NodeType == NodeType.Paragraph)
    {
        Paragraph para = (Paragraph)p;
        var fields = para.Range.Fields;
        if(fields.Any(f => f.Type == Aspose.Words.Fields.FieldType.FieldSequence))
        {
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        }
    }
                
    p = table.PreviousSibling;
    if (p.NodeType == NodeType.Paragraph)
    {
        Paragraph para = (Paragraph)p;
        var fields = para.Range.Fields;
        if (fields.Any(f => f.Type == Aspose.Words.Fields.FieldType.FieldSequence))
        {
            para.ParagraphFormat.Alignment = ParagraphAlignment.Center;
        }
    }
}

doc.Save(@"C:\Temp\output.docx");

output.docx (16.8 KB)

Thank you @eduardo.canal. This is working. But still im having some issues maybe related to my code.

@Amrinder_Singh please let me know if I can help you in any way.