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

Free Support Forum - aspose.com

Have different fonts/alignment on the same line in aspose words java

Actually my use case is to have one text left formatted and the other right formatted on the same line.

I set the paragraph alignment to left and insert text and then again set the paragraph alignment to right and insert the text.

But in the resultant document , all the text is right aligned.Please help.

@saurabh.arora,

Thanks for your inquiry. Please use the following code example to get the desired output. Hope this helps you.

Document doc = new Document();
Paragraph newPara = new Paragraph(doc);

TabStop tab = new TabStop(6.27 * 72, TabAlignment.RIGHT, TabLeader.NONE);
newPara.getParagraphFormat().getTabStops().add(tab);
newPara.appendChild(new Run(doc, "Text on left side " + ControlChar.TAB + "Text with right alignment"));

doc.getLastSection().getBody().insertAfter(newPara,doc.getLastSection().getBody().getLastParagraph());

doc.save(MyDir + "Out.docx");

Thanks for the reply.

My use case is also to have different fonts for the left aligned text and right aligned text.

@saurabh.arora,

Thanks for your inquiry. All text of the document is stored in runs of text. Please use following code to get the desired output.

Document doc = new Document();
Paragraph newPara = new Paragraph(doc);

TabStop tab = new TabStop(6.27 * 72, TabAlignment.RIGHT, TabLeader.NONE);
newPara.getParagraphFormat().getTabStops().add(tab);

Run run1 = new Run(doc, "Text on left side " + ControlChar.TAB );
run1.getFont().setSize(12.0);
newPara.appendChild(run1);
Run run2 = new Run(doc, "Text with right alignment");
run2.getFont().setSize(16.0);
newPara.appendChild(run2);

doc.getLastSection().getBody().insertAfter(newPara,doc.getLastSection().getBody().getLastParagraph());

doc.save(MyDir + "Out.docx");

Thanks for the help.

I need your help in this. I want to insert some text with specific formatting left aligned in footer and page number (page field like x of y) in the same line (i.e footer section) , right aligned with some specific formatting. This is my use case. I am unable to have different alignment on the same line. Help will be really appreciated.

Hi,

Is there any update. I am stuck at this.

@saurabh.arora,

Thanks for your inquiry. Please refer to the following articles:

Use DocumentBuilder to Insert Document Elements
Using DocumentBuilder to Modify a Document

Please check the following code example. Hope this helps you. If you still face problem, please share your expected output Word document here for our reference. We will investigate how you want your final Word output be generated like. We will then provide you more information on this along with code.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
builder.moveToHeaderFooter(HeaderFooterType.FOOTER_PRIMARY);
builder.getFont().setSize(10);
builder.getFont().setBold(true);
builder.write("Some text");

TabStop tab = new TabStop(6.27 * 72, TabAlignment.RIGHT, TabLeader.NONE);
builder.getParagraphFormat().getTabStops().add(tab);

builder.write(ControlChar.TAB);

builder.getFont().setSize(15);
builder.getFont().setBold(false);
builder.getFont().setItalic(true);
builder.insertField(FieldType.FIELD_PAGE, false);

builder.write(" of ");
builder.insertField(FieldType.FIELD_NUM_PAGES, false);

doc.updateFields();
doc.save(MyDir + "Out.docx");

Thanks for the help.

Actually my placement/alignment of footer text and page number is chosen by the user. I was wondering how could i solve the combinations of them , like for above example we have combination of left aligned and right aligned .

How to do ‘center aligned and right aligned’ and ‘left aligned and center aligned’. Changing the tab alignment did not work. I think we need to change the position. Please help.

@saurabh.arora,

Thanks for your inquiry. In your case, we suggest you please create a table with one row that have three cells with equal width. Please move the cursor to the desired cell and insert the contents. Hope this helps you.