0

I am writting text to a docx document using docx4j i want to change default text direction to right-to-left , this is my code:

WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addParagraphOfText("some plain text");
File exportFile = new File("test.docx");
wordPackage.save(exportFile);

2 Answers2

0

It is suggested to use Apache POI. Please check this page for a tutorial and use a template file for your goal as described here.

remo
  • 880
  • 2
  • 14
  • 32
0

In the WordML/OpenXML file format, r2l is a boolean property on a run: http://webapp.docx4java.org/OnlineDemo/ecma376/WordML/rtl.html

So:

 // Create object for rtl
BooleanDefaultTrue booleandefaulttrue = wmlObjectFactory.createBooleanDefaultTrue(); 
rpr.setRtl(booleandefaulttrue); 

You set rPr on a run (R object), for example:

RPr rpr = wmlObjectFactory.createRPr(); 
r.setRPr(rpr); 

In the above:

org.docx4j.wml.ObjectFactory wmlObjectFactory = new org.docx4j.wml.ObjectFactory();

You can generate code for docx4j by creating a sample document in Word which contains what you want, then using the docx4j webapp or docx4j Helper Word AddIn.

JasonPlutext
  • 15,352
  • 4
  • 44
  • 84