简体   繁体   中英

Apache POI - How to set character spacing for XWPFRun

I need to create an XWPFRun with character spacing expanded by 1 pt. I tried the methods setCharacterSpacing and setKerning but to no avail. I also found no code examples or documentation for either method.

Any advice?

For me XWPFRun.setCharacterSpacing works. What one needs to know is that the measurement unit is TWIP - twentieths of an inch point. So 1*20 is 1 pt.

Example:

import java.io.File;
import java.io.FileOutputStream;

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

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

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();
  XWPFRun run = paragraph.createRun();  
  run.setText("This is sample text to compare character spacing.");

  paragraph = document.createParagraph();
  run = paragraph.createRun();  
  run.setText("This is sample text to compare character spacing.");
  run.setCharacterSpacing(1*20); // measurement unit is TWIPS - twentieths of a point. So 1*20 is 1 pt.

  FileOutputStream out = new FileOutputStream("CreateXWPFRunCharacterSpacing.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM