Apply justification to selected paragraphs in word document generated by SSRS

Hello,

We have downloaded evaluation copy of Aspose.Words for .NET. We have prepared a SSRS report and we are rendering this report in WORD format. Once the report is rendered in WORD format, we are trying to justify this report using Aspose.Words.dll. Looks like the justification is getting applied to the all items for which it should not. Below is the image for better example:

image.png (40.1 KB)

In the attached example, the highlighted part should not be justified as it is a “To Address” part which should be left aligned.

Below is the code which we are using to justify the document -

Aspose.Words.NodeCollection paragraphsCollection = wordDocument.GetChildNodes(Aspose.Words.NodeType.Paragraph,true);
                Aspose.Words.Node[] paragraphs = paragraphsCollection.ToArray();

                foreach (Aspose.Words.Paragraph para in paragraphs)
                {
                    if (para. .NodeType == Aspose.Words.NodeType.Paragraph)
                        para.ParagraphFormat.Alignment = Aspose.Words.ParagraphAlignment.Justify;
                }

Is there a different way to justify the text for only selected paragraphs\tables? Or is there a different way to justify the document in proper format?

Thanks,
Sameer

@LISHELP,

Please ZIP and upload your 1) input Word document and 2) MS Word generated expected document which shows the correct output here for our reference. We will then provide you code to achieve this.

Hello,

I have attached both original and expected documents. Please check and let us know if you need further information.

Document.zip (18.8 KB)

Thank you,
Sameer

@LISHELP,

You can use the following code to skip all Paragraphs before the first Bold line:

Document doc = new Document(MyDir + @"Document (1)\Input_Document.doc");

NodeCollection paragraphs = doc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true);
bool flag = false;
foreach (Paragraph para in paragraphs)
{
    if (para.ToString(SaveFormat.Text).StartsWith("Evaluation Only. Created with Aspose.Words."))
        continue;

    if (para.Runs.Count > 0 && para.Runs[0].Font.Bold == true)
        flag = true;

    if (flag)
        para.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
}

doc.Save(MyDir + @"Document (1)\18.2.doc");

Hello Awais,

Thank you for the proposed solution. It is working for the document that we sent you earlier. However we have different types of document which has different formatting. In this case the proposed solution is not working. Do we have any generic solution which will work for all the documents?

I have attached few documents as below which needs justification -
SampleWordDocumentsNeedsJustification.zip (147.8 KB)

Thank you,
Sameer

@LISHELP,

We are checking these documents and will get back to you soon.

@LISHELP,

One way is to first apply Justify formatting to all Paragraphs in Document. After that you can use Find and Replace functionality of Aspose.Words for .NET to apply Left alignment to some Paragraphs. Please see following code example:

public class ReplacingCallbackHandler : IReplacingCallback
{
    ReplaceAction IReplacingCallback.Replacing(ReplacingArgs e)
    {
        Node currentNode = e.MatchNode;
        Paragraph para = (Paragraph)currentNode.GetAncestor(NodeType.Paragraph);
        if (para != null)
            para.ParagraphFormat.Alignment = ParagraphAlignment.Left;

        return ReplaceAction.Skip;
    }
}

Document doc = new Document(MyDir + @"Document (1)\Input_Document.doc");

NodeCollection paragraphs = doc.GetChildNodes(Aspose.Words.NodeType.Paragraph, true);
foreach (Paragraph para in paragraphs)
{
    para.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
}

FindReplaceOptions opts = new FindReplaceOptions();
opts.ReplacingCallback = new ReplacingCallbackHandler();
doc.Range.Replace("Pass Text here that you want to Left Align ", "", opts);
doc.Range.Replace("Pass another Text here that you want to Left Align ", "", opts);
doc.Range.Replace("Pass another Text here that you want to Left Align ", "", opts);

doc.Save(MyDir + @"Document (1)\18.3.doc");