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

Free Support Forum - aspose.com

Right-to-left (RTL) for word table? Only supported for html?

Hi.


I’m trying to implement RTL capabilities for our aspose word exporting, and I’m having difficulties understanding how to do this and what’s supported. This is using .net.

Specifically, I’m trying to render some Tables in word. Our tables can be somewhat complex, but I’ll start with a really simple example. Imagine that you have a table with 1 row. The left most row has some text which is left aligned, and the rest of the cells has numbers which are right aligned.

If I render this in html and set RTL property, the right most cell will have the text (right aligned) and the rest of the cells has the numbers (left aligned). This is the behaviour I want.

I tried setting bidi=true for the paragraph the table is contained in, which changed the alignment but didn’t change the cell order. That’s as far as I got. Am I missing something obvious? Or is it not possible to get similar handling as what the browser does for me when I write HTML?

I’m looking at word first because I assumed it would be the easiest one, but I also want the same behaviour when exporting to pdf and powerpoint, so if the logic differs I’d highly appreciate examples for those as well.

Please see attached image for visualizations of what’s happening and what I actually want.

NOTE: I understand that in this simple scenario I could simply reverse the order myself, but it’s a lot more complex in our actual product and trying to handle it manually is probably not a good idea and certainly not a path I want to go down.


I did some more testing and thought that maybe I could clarify further.


Basically, I have a table like structure that I want to render to a new word document. Sometimes, this table might be RTL. The following aspose logic works and will display the table according to the screenshot in my post above:

StringBuilder sb = new StringBuilder();
StringWriter tw = new StringWriter(sb);
HtmlTextWriter hw = new HtmlTextWriter(tw);
if (IsRightToLeft()) {
_htmlTable.Attributes.Add(“dir”, “rtl”);
}
_htmlTable.RenderControl(hw);
docBuilder.InsertHtml(sb.ToString());

The key line being where I add the direction attribute. I always add the cells in the same order, but if this attribute is set, the order of the cells is reversed. This is what I’m after. However, due to a LOT of existing code that has been around and modified over many years, I can’t easily change the structure to take advantage of the “InsertHtml” functionality. Instead, the table is created like this:

Table table= tableBuilder.StartTable();

We then add cells etc similarly. What I’m after is the same functionality as the direction attribute in the html version above. I want to add cells to this table in the same manner regardless what the direction is, but I want a way of specifying whether it should be LTR or RTL. Does something like this exist at all? If not, what is the intended way of handling this? Is RTL support only available when using “InsertHtml” method?

Any help is much appreciated.
Hi there,

Thanks for your inquiry. Please use Table.Bidi property to get or set whether this is a right-to-left table. Use ParagraphFormat.Bidi property to get or set whether this is a right-to-left paragraph.

The Run.Font.Bidi property is used to specify whether the contents of this run shall have right-to-left characteristics. Moreover, PageSetup.Bidi property is used to specify that this section contains bidirectional (complex scripts) text.

Please check the following code example. Hope this helps you. If you still face problem, please share your input and expected output documents. We will then provide you more information about your query.

Document doc = new Document(MyDir + "in.docx");
for (Section section : doc.getSections())
{
section.getPageSetup().setBidi(true);
for (Table table : (Iterable
)doc.getChildNodes(NodeType.TABLE, true))
{
table.setBidi(true);
for (Paragraph para : (Iterable)table.getChildNodes(NodeType.PARAGRAPH, true)) {
para.getParagraphFormat().setBidi(true);
for (Run run : (Iterable)para.getChildNodes(NodeType.RUN, true))
run.getFont().setBidi(true);
}
}
}
doc.save(MyDir + "Out.docx");

That was very helpful, thank you!


My initial approach was to change the properties right away as the items are being created, but it was very difficult due to complexity in the code and I didn’t realise how easy it was to simply step through the whole structure afterwards.

Our structure is not exactly as your code assumed, but I basically just made a recursive function that steps through all childNodes and sets the bidi property for those node types. It seems to work at a first glance. Fingers crossed I don’t find any issues and that a similar approach can be done for ppt and pdf.
Hi there,

Thanks for your inquiry. Your query is related to Aspose.Slides and Aspose.Pdf APIs. So, I am moving this forum thread to Aspose.Total forum. My colleagues will answer your query shortly.

Hi,

Thank you for being patient. Aspose.Slides API (for PPT / PPTX formats) has support of RTL on paragraph level. You can access each cell of the table, and then set its paragraph format property as below:

C# .NET Code:

//Instantiate PresentationEx
Presentation pres = new Presentation();

//Get the first slide
ISlide sld = pres.Slides[0];

//Add an AutoShape of Rectangle type
IAutoShape ashp = sld.Shapes.AddAutoShape(Aspose.Slides.ShapeType.Rectangle, 150, 75, 350, 250);

//Add TextFrame to the Rectangle
ashp.AddTextFrame(" ");
ITextFrame tf = ashp.TextFrame;
IParagraph para = tf.Paragraphs[0];
para.ParagraphFormat.RightToLeft = NullableBool.True;
para.Text = "Aspose TextBox";
//para.setText(“Aspose TextBox”);
NullableBool val = para.ParagraphFormat.RightToLeft;

//Write the presentation to disk
pres.Save(@"c:\Aspose Data\TextBox.pptx", Aspose.Slides.Export.SaveFormat.Pptx);

Please also refer to this help topic: Creating Accessing and Updating Table in a Slide

Aspose.PDF for Java has support of RTL languages, but we have not tested it for table’s cells. We’re investigating and let you know soon.

Thanks for the reply.


Before topic was moved, I did however create a different topic for exporting to PDF (with some example code and questions). Feel free to answer in either of the threads though. Here is the other one:
https://www.aspose.com/community/forums/824639/righttoleft-rtl-lang-support-broken-for-pdf/showthread.aspx

Hi,


Thank you for the details. We’ll reply you in the Aspose.Pdf forum thread because the remaining part of the query is specific to Aspose.Pdf API.

Hi Tom,


Your query related to Aspose.Pdf for .NET has been replied in other forum thread.