簡體   English   中英

使用Apache-POI設置標簽大小

[英]Set Tab size with Apache-POI

我想通過Word文檔中的Apache-POI設置選項卡大小。

我有一個標頭,應該在標頭行中包含兩個字段,如下所示:

|    filed1                  ->                   field2    |

垂直線代表頁面的邊緣。 我希望兩個字段之間的選項卡盡可能大,以便第一個字段左對齊頁面,而右字段右對齊頁面。

使用Word本身很容易完成,但是我只發現了如何使用POI添加標簽,而不是如何設置標簽的寬度。

我嘗試使用Apaches tika工具調查Word文件,但沒有看到選項卡大小埋在文件中的位置。

感謝任何幫助,Maik

制表位是Word段落中的設置。 而且盡管使用制表位是很常見的事情,而且是文字處理中非常老的過程,但是如果不使用apache poi底層底層ooxml-schema對象,這是不可能的。

例:

注意:制表位的位置的度量單位是緹(二十分之一英寸)。

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();


 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM