有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用ApachePOI设置选项卡大小

我想通过Word文档中的Apache POI设置选项卡大小

我有一个标题,它的标题行应该有两个字段,如下所示:

|    filed1                  ->                   field2    |

垂直线代表页面的边缘。 我希望两个字段之间的选项卡都一样大,以便第一个字段与页面左对齐,右字段与页面右对齐

使用Word本身很容易,但我只了解了如何使用POI添加选项卡,而没有了解如何设置选项卡的宽度

我试图用Apaches tika工具调查Word文件,但没有看到标签大小在文件中的位置

感谢您的帮助, 麦克


共 (1) 个答案

  1. # 1 楼答案

    制表位是Word段落中的设置。尽管使用制表位是一件非常常见的事情,也是字处理中一个非常古老的过程,但如果不使用apache poi的底层ooxml模式对象,这是不可能的

    例如:

    注:止动片位置的测量单位为twips(二十分之一英寸点)

    import java.io.FileOutputStream;
    
    import org.apache.poi.xwpf.usermodel.*;
    import org.apache.poi.wp.usermodel.HeaderFooterType;
    
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTabStop;
    import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTabJc;
    
    import java.math.BigInteger;
    
    public class CreateWordHeaderWithTabStops {
    
     public static void main(String[] args) throws Exception {
    
      XWPFDocument doc = new XWPFDocument();
    
      // the body content
      XWPFParagraph paragraph = doc.createParagraph();
      XWPFRun run = paragraph.createRun();  
      run.setText("The Body...");
    
      // create header
      XWPFHeader header = doc.createHeader(HeaderFooterType.FIRST);
    
      // header's first paragraph
      paragraph = header.getParagraphArray(0);
      if (paragraph == null) paragraph = header.createParagraph();
      paragraph.setAlignment(ParagraphAlignment.LEFT);
    
      // create tab stops
      int twipsPerInch = 1440; //measurement unit for tab stop pos is twips (twentieth of an inch point)
    
      CTTabStop tabStop = paragraph.getCTP().getPPr().addNewTabs().addNewTab();
      tabStop.setVal(STTabJc.CENTER);
      tabStop.setPos(BigInteger.valueOf(3 * twipsPerInch));
    
      tabStop = paragraph.getCTP().getPPr().getTabs().addNewTab();
      tabStop.setVal(STTabJc.RIGHT);
      tabStop.setPos(BigInteger.valueOf(6 * twipsPerInch));
    
      // first run in header's first paragraph, to be for first text box
      run = paragraph.createRun(); 
      run.setText("Left");
      // add tab to run
      run.addTab();
    
      run = paragraph.createRun(); 
      run.setText("Center");
      // add tab to run
      run.addTab();
    
      run = paragraph.createRun(); 
      run.setText("Right");
    
      FileOutputStream out = new FileOutputStream("CreateWordHeaderWithTabStops.docx");
      doc.write(out);
      doc.close();
      out.close();
    
    
     }
    }