Add TabStop to paragraph

Hi, everyone!
I have a question for u :laughing:
I have a Word doc file:
Made0001_1.docx (92.6 KB)

I want to change 4 paragraphs at the end of the document to 2 lines with TapStop using Aspose Word C#.
Such as:
Made0001_result.docx (92.8 KB)

Thank you for your help! :blush:

@quanghieumylo To achieve this you should add a tab at the beginning of the paragraphs and then concatenate neighbor pairs of the paragraphs. For example see the following code:

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

// Create a run with a tab.
Run tab = new Run(doc, "\t");

// Get the paragraphs to reformat.
Paragraph[] paragraphs = new Paragraph[] {
    doc.FirstSection.Body.Paragraphs[2],
    doc.FirstSection.Body.Paragraphs[3],
    doc.FirstSection.Body.Paragraphs[4],
    doc.FirstSection.Body.Paragraphs[5]};

// Add tab at the beginning of each paragraph
foreach (Paragraph p in paragraphs)
    p.PrependChild(tab.Clone(true));

// Concatenate pairs of paragraphs.
while (paragraphs[1].HasChildNodes)
    paragraphs[0].AppendChild(paragraphs[1].FirstChild);
paragraphs[1].Remove();

while (paragraphs[3].HasChildNodes)
    paragraphs[2].AppendChild(paragraphs[3].FirstChild);
paragraphs[3].Remove();

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

Thank you, Alexey.noskov!
But it is not true what I want
I want to create 2 tapstop:

documentBuilder.ParagraphFormat.TabStops.Add(new Aspose.Words.TabStop((double)50, 0, 0));
documentBuilder.ParagraphFormat.TabStops.Add(new Aspose.Words.TabStop((double)300, 0, 0));

and add each paragraph to each tapstop
For example: Made0001_result.docx (92.8 KB)
Thank you!

@quanghieumylo Just add the tab stops to the remaining paragraphs. Please see the modified code:

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

// Create a run with a tab.
Run tab = new Run(doc, "\t");

// Get the paragraphs to reformat.
Paragraph[] paragraphs = new Paragraph[] {
    doc.FirstSection.Body.Paragraphs[2],
    doc.FirstSection.Body.Paragraphs[3],
    doc.FirstSection.Body.Paragraphs[4],
    doc.FirstSection.Body.Paragraphs[5]};

// Add tab at the beginning of each paragraph
foreach (Paragraph p in paragraphs)
    p.PrependChild(tab.Clone(true));

// Concatenate pairs of paragraphs.
while (paragraphs[1].HasChildNodes)
    paragraphs[0].AppendChild(paragraphs[1].FirstChild);
paragraphs[1].Remove();

while (paragraphs[3].HasChildNodes)
    paragraphs[2].AppendChild(paragraphs[3].FirstChild);
paragraphs[3].Remove();

// Add tab stops to the remaining paragraphs.
paragraphs[0].ParagraphFormat.TabStops.Add(new Aspose.Words.TabStop((double)50, 0, 0));
paragraphs[0].ParagraphFormat.TabStops.Add(new Aspose.Words.TabStop((double)300, 0, 0));

paragraphs[2].ParagraphFormat.TabStops.Add(new Aspose.Words.TabStop((double)50, 0, 0));
paragraphs[2].ParagraphFormat.TabStops.Add(new Aspose.Words.TabStop((double)300, 0, 0));

doc.Save(@"C:\Temp\out.docx");
1 Like

Wow, it’s really perfect!

1 Like

@alexey.noskov, I want to AppendChild a paragraph but I want to know whether it can be in one line or not.
How to know the width in points of a paragraph?

@quanghieumylo I have answered this question in another your thread here:
https://forum.aspose.com/t/how-to-know-the-width-in-points-of-a-paragraph/264308

1 Like

@alexey.noskov thank you so much :smiling_face_with_three_hearts:

1 Like