We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

setFirstLineIndent at the beginning of right-aligned text

I have a right-aligned text (signature) and I need to have setFirstLineIndent in the beginning of this text.
Is it possible to accomplish?
I’m using Aspose Words Java 16.11.0, creating odt documents.

Thanks in advance for any help.
Emerson Roman

SignatureResult.JPG (10.5 KB)

@emersonroman,

Please ZIP and attach your input Word document and corresponding expected Word document showing the desired output here for our reference. Please create expected Word document by using MS Word. We will then provide you code to achieve the same by using Aspose.Words.

I’m using Aspose.Words for Java to generate .odt text document (OpenDocument is also supported by Aspose.Words). Please find the document in the zipped file attached. SignatureResult.zip (6.9 KB)
I appreciate for any help.

Regards,
Emerson Roman

In fact I need to set - for all signature lines - the left indentation based on the wider text. And the signature must be close to the right margin.
So, for the following signature:
Sales
Director
Emerson Roman
The wider text ‘Emerson Roman’ should be close to the right margin, and all the 3 lines (Sales, Director, Emerson Roman) should have the same value on FirstLineIndent or LeftIndent, in the beginning of of ‘Emerson Roman’ text.

Thanks in advance for any help.
Emerson Roman

@emersonroman,

Please try using the following code:

for (Paragraph para : (Iterable<Paragraph>)doc.getChildNodes(NodeType.PARAGRAPH, true))
{
    if(para.getParagraphFormat().getAlignment() == ParagraphAlignment.CENTER)
    {
        para.getParagraphFormat().setLeftIndent(5.58 * 72);
    }
}

Thanks for your reply.

Maybe I should have written what I’ve tried to do so far to accomplish this:

  • created a table with one row and 2 columns, inserted the text into the right cell, used DocumentBuilder.getCellFormat().setWrapText(false) but it still breaks the texts with spaces in the middle.
  • wrote all the lines of the signature right-aligned, inserted a bookmark on the left of each line, found the position of each bookmark to find the leftmost one, and used this position to set the FirstLineIndent and/or LeftIndent.

I got the best result with this second/latter approach, but I could not have the text really ending on the right margin.

I’m uploading a new odt file to help understand the result I need, preferably without using tables.
SignatureResultNew.zip (15.1 KB)

Thanks in advance for any further help.
Emerson Roman

@emersonroman,

You can also make use of TabStops. Please see the following sample code:

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

ParagraphFormat paragraphFormat = builder.getParagraphFormat();
paragraphFormat.getTabStops().add(288, TabAlignment.LEFT, TabLeader.NONE);

builder.writeln("Source:" + ControlChar.TAB + "Name");

builder.insertParagraph();
paragraphFormat = builder.getParagraphFormat();
paragraphFormat.clearFormatting();
paragraphFormat.getTabStops().add(396, TabAlignment.LEFT, TabLeader.DOTS);

builder.writeln("Source:" + ControlChar.TAB + "Name");

doc.save("D:\\Temp\\awjava-18.5.docx");

Thanks for your reply, but it didn’t work.

I need something that is dinamically setting the FirstLineIndent or LeftIndent of largest/biggest text.

Please, let change the side and make you questions:

Let’s suppose we have 2 Strings:

Awais Hafeez
Emerson Roman Moreira dos Santos

Which one is the biggest one?
Do you agree that is the second one? That is ‘Emerson Roman Moreira dos Santos’?

Ok.
If you have both written in the odt document, left-aligned, are you able to set the RightIndent of both lines based on the size of the largest/biggest one?

But a code to calculate it for any strings, not only for the ones I provided here (‘Awais Hafeez’ and ‘Emerson Roman Moreira dos Santos’).

Best regards,
Emerson

@emersonroman,

For this scenario, please ZIP and upload your sample 1) input Word document and 2) expected Word document showing the final output here for testing. We will investigate the issue on our end and provide you more information.

TestSignature.zip (4.8 KB)

@emersonroman,

You can build on the following logic to meet this requirement (see 18.5.zip (7.9 KB))

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

LayoutCollector collector = new LayoutCollector(doc);
LayoutEnumerator enumerator = new LayoutEnumerator(doc);

// Calculate Width of each Paragraph
Dictionary<Paragraph, float> dict = new Dictionary<Paragraph, float>();
foreach (Paragraph para in doc.FirstSection.Body.GetChildNodes(NodeType.Paragraph, true))
{
    if (para.Runs.Count > 0)
    {
        enumerator.Current = collector.GetEntity(para.Range.Bookmarks[0].BookmarkEnd);
        float left = enumerator.Rectangle.Right;

        enumerator.Current = collector.GetEntity(para);
        float right = enumerator.Rectangle.Right;

                    
        dict.Add(para, (right - left));
    }
}

// Get Paragrph with Max width
float max = dict.Values.Max();
Paragraph maxPara = dict.Where(kvp => kvp.Value == max).Select(kvp => kvp.Key).First();

// Calculate how much distance we have to move to right
ParagraphAlignment oldAlignment = maxPara.ParagraphFormat.Alignment;
maxPara.ParagraphFormat.Alignment = ParagraphAlignment.Left;

collector = new LayoutCollector(doc);
enumerator.Current = collector.GetEntity(maxPara);
float rightOld = enumerator.Rectangle.Right;
            
maxPara.ParagraphFormat.Alignment = ParagraphAlignment.Right;

collector = new LayoutCollector(doc);
enumerator.Current = collector.GetEntity(maxPara);
float rightNew = enumerator.Rectangle.Right;

float distance = rightNew - rightOld;
Console.WriteLine("Distance to move Right --> " + distance);

maxPara.ParagraphFormat.Alignment = oldAlignment;
// Add same distance to all Paragraphs in quesstion
foreach (Paragraph para in dict.Keys)
    para.ParagraphFormat.LeftIndent = maxPara.ParagraphFormat.LeftIndent + (distance - .72);
            
doc.Save(MyDir + @"TestSignature\18.5.docx");