2016-08-06 2 views
1

나는 Apache poi word 3.8을 사용하여 페르시아어/아랍어로 단어 문서를 만들려고합니다.Apache word poi (XWPF)에서 테이블 방향을 변경하는 방법은 무엇입니까?

내 질문은 : 문서에서 테이블 방향을 변경하는 방법? 테이블의 열 순서 (테이블 셀 안의 텍스트 방향이 아님)를 의미합니다. 우리가 사용하는 테이블 속성을 변경할 수있는 MS 워드에서

rtl table vs lrt table direction

:

this option

+4

가능한 중복 http://stackoverflow.com/questions/38802115/how-change-text-directionnot -paragraph-alignment-in-document-in-apache-poi-wor) – Deduplicator

답변

3

전체 테이블이 양방향한다 경우는, 다음 org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPrbidiVisual 세트를 포함해야합니다 예를 들어

ON.

import java.io.FileOutputStream; 

import org.apache.poi.xwpf.usermodel.*; 

import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTcPr; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth; 
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth; 

import java.math.BigInteger; 

public class CreateWordRTLTable { 

public static void main(String[] args) throws Exception { 

    XWPFDocument doc= new XWPFDocument(); 

    XWPFParagraph paragraph = doc.createParagraph(); 
    XWPFRun run = paragraph.createRun(); 
    run.setText("Paragraph 1 LTR"); 

    paragraph = doc.createParagraph(); 

    //create table: 
    XWPFTable table = doc.createTable(); 
    //set the table itself to bidi visual 
    if (table.getCTTbl().getTblPr() == null) { 
    table.getCTTbl().addNewTblPr().addNewBidiVisual().setVal(STOnOff.ON); 
    } else { 
    table.getCTTbl().getTblPr().addNewBidiVisual().setVal(STOnOff.ON); 
    } 
    //create first row 
    XWPFTableRow tableRow = table.getRow(0); 
    tableRow.getCell(0).setText("first cell"); 
    tableRow.addNewTableCell().setText("السلام عليكم"); 
    tableRow.addNewTableCell().setText("third cell"); 
    //Each cell contains at least a paragraph. Those can be set to support bidi. 
    for (int col = 0 ; col < 3; col++) { 
    paragraph = tableRow.getCell(col).getParagraphs().get(0); 
    CTP ctp = paragraph.getCTP(); 
    CTPPr ctppr = ctp.getPPr(); 
    if (ctppr == null) ctppr = ctp.addNewPPr(); 
    ctppr.addNewBidi().setVal(STOnOff.ON); 
    } 

    //create CTTblGrid for this table with widths of the 3 columns. 
    //necessary for Libreoffice/Openoffice to accept the column widths. 
    //values are in unit twentieths of a point (1/1440 of an inch) 
    table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440)); 
    for (int col = 1 ; col < 3; col++) { 
    table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(2*1440)); 
    } 

    //create and set column widths for all columns in all rows 
    //most examples don't set the type of the CTTblWidth but this 
    //is necessary for working in all office versions 
    //values are in unit twentieths of a point (1/1440 of an inch) 
    for (int col = 0; col < 3; col++) { 
    CTTblWidth tblWidth = CTTblWidth.Factory.newInstance(); 
    tblWidth.setW(BigInteger.valueOf(2*1440)); 
    tblWidth.setType(STTblWidth.DXA); 
    for (int row = 0; row < 1; row++) { 
    CTTcPr cttcpr = table.getRow(row).getCell(col).getCTTc().getTcPr(); 
    if (cttcpr != null) { 
    cttcpr.setTcW(tblWidth); 
    } else { 
    cttcpr = CTTcPr.Factory.newInstance(); 
    cttcpr.setTcW(tblWidth); 
    table.getRow(row).getCell(col).getCTTc().setTcPr(cttcpr); 
    } 
    } 
    } 

    paragraph = doc.createParagraph(); 

    doc.write(new FileOutputStream("WordDocument.docx")); 

} 
} 
[아파치 POI 워드 문서에서 텍스트 방향 (안 단락의 배치)을 변경하는 방법? (XWPF)] (의
관련 문제