1

i'am trying to generate a word document using apache poi api, and i want to set an arabic sentence into the word, but the words didn't stay on the order !!! for instead of "شهادة بالملك" i get بالملك شهادة

public class word {

    public static void main (String [] args) {
    XWPFDocument docx = new XWPFDocument();

    try {

    XWPFParagraph tmpParagraph = docx.createParagraph();   
    XWPFRun tmpRun = tmpParagraph.createRun();   
    tmpRun.setText("شهادة بالملك");
    tmpRun.setFontSize(18);  
    tmpRun.setFontFamily("Calibri (Corps)");
    tmpRun.setBold(true);
    tmpRun.setColor("003894");
    tmpParagraph.setAlignment(ParagraphAlignment.LEFT);
    tmpRun.setUnderline(UnderlinePatterns.SINGLE);
    tmpParagraph.setSpacingAfter(300);

    FileOutputStream fos = new FileOutputStream("Word2.docx");
    docx.write(fos);
    fos.close();
    }
    catch (Exception e ) {
        e.printStackTrace();
    }
    }
}
geosevda
  • 185
  • 12
  • Your words are exactly the same... – Dici Jan 08 '15 at 01:53
  • I edit my question it was just a mistake – geosevda Jan 08 '15 at 01:56
  • Arabic is right to left instead of left to right. Does XWPFDocument know that the writing direction is changed? – Thorbjørn Ravn Andersen Jan 08 '15 at 02:00
  • just cant fing any command doing what you had proposed....(tmpRun(reverse(word))) – geosevda Jan 08 '15 at 02:02
  • mm sorry I meant that you can implement a `reverse` method by yourself and then `tmpRun.setText(reverse("شهادة بالملك"))`. It is not a nice solution but that's a possible fix in case you don't find anything else – Dici Jan 08 '15 at 02:03
  • NO, how can i tell him to write from right to left ?!! – geosevda Jan 08 '15 at 02:05
  • It is not even sure that such a feature has been implemented... Programs or libraries cannot be adapted to every languages in the world. – Dici Jan 08 '15 at 02:13
  • i'am sure that apache poi can support this, and i should just change the direction right to left as Andersen said i just should find the command doing this ! – geosevda Jan 08 '15 at 02:20
  • possible duplicate of [Right to Left (RTL) text in XWPFDocument (Apache POI)](http://stackoverflow.com/questions/11171777/right-to-left-rtl-text-in-xwpfdocument-apache-poi) – Dici Jan 08 '15 at 02:23
  • AbstractWordView can not be resolved as type !!!!! – geosevda Jan 08 '15 at 09:05

1 Answers1

3

this is the answer :

public class word {

    public enum TextOrientation {
          LTR,
          RTL
       }

    public static void main (String [] args) {

    XWPFDocument docx = new XWPFDocument();

    try {

    XWPFParagraph tmpParagraph = docx.createParagraph();   
    XWPFRun tmpRun = tmpParagraph.createRun();   
    tmpRun.setText("شهادة بالملك");
    tmpRun.setFontSize(18);  
    tmpRun.setFontFamily("Calibri (Corps)");
    tmpRun.setBold(true);
    tmpRun.setColor("003894");
    tmpParagraph.setAlignment(ParagraphAlignment.CENTER);
    tmpRun.setUnderline(UnderlinePatterns.SINGLE);
    tmpParagraph.setSpacingAfter(300);
    setOrientation(tmpParagraph, TextOrientation.RTL);

    FileOutputStream fos = new FileOutputStream("Word2.docx");
    docx.write(fos);
    fos.close();
    }
    catch (Exception e ) {
        e.printStackTrace();
    }
    }

    private static void setOrientation(XWPFParagraph par, TextOrientation orientation) {
          if ( par.getCTP().getPPr()==null ) {
              par.getCTP().addNewPPr();
          }
          if ( par.getCTP().getPPr().getBidi()==null ) {
             par.getCTP().getPPr().addNewBidi();
          }
          par.getCTP().getPPr().getBidi().setVal(orientation==TextOrientation.RTL?STOnOff.ON:STOnOff.OFF);
       }
}
geosevda
  • 185
  • 12