Apache Poi for Word - 使用 TextAlignment 创建自定义样式

Apache Poi for Word - Create Custom style with TextAlignment

    private static void applyTextAlign(XWPFStyle style, TextAlignment value) {
        // Steps here
    }

如何将文本对齐(任何类型,如 TextAlignment.CENTER)应用于传递给此函数输入的样式对象?我一直无法找到合适的教程和示例。

        CTPPr ppr = style.getCTStyle().addNewPPr();
        CTTextAlignment textAlignment = ppr.addNewTextAlignment();
        STTextAlignment.Enum en = STTextAlignment.Enum.forInt(value.ordinal());
        textAlignment.setVal(en);

我试过类似上面的方法,但它自然是不正确的,它甚至在 docx 上损坏。我在这里找到的其他示例将文本对齐方式直接应用于段落,但我需要将文本对齐方式作为样式的一部分,以便可以为任何元素提供它,无论是段落还是其他元素。

使用低级CT* 类没那么简单。那些 类 是根据 XML 模式自动创建的。他们什么都不考虑。所以不能简单地向样式添加新的 PPr 。如果它已经有一个怎么办?它不能有两个,因为那会导致文件损坏。必须检查是否已经存在 PPr,如果没有,则添加一个新的。 PPr 中的 TextAlignment 也一样。

但当然还有更多问题,因为 XWPFapache poi 中处于非常实验性的状态。例如 XWPFStyle 到目前为止主要是为了阅读而制作的。直到现在还没有太多的二传手。当使用 CTStyle 时,需要检查是否真的存储了对此的更改。例如,XWPFStylesjava.util.List<XWPFStyle> 似乎失去了与其中 XWPFStyle 的单个 CTStyle 的联系。所以我不得不使用反射找到一种方法来更改 CTStyle 以便存储更改。

哦,当然 不能 确定 ST* 枚举的序数与 apache poi 枚举的序数相同枚举。那会不会太简单了,不是吗?

以下代码适用于我使用当前 apache poi 4.1.2:

import java.io.*;

import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.lang.reflect.Field;

public class CreateWordTextAlingmentStyles {

 private static XWPFStyle createNamedStyle(XWPFStyles styles, STStyleType.Enum styleType, String styleId) {
  if (styles == null || styleId == null) return null;
  XWPFStyle style = styles.getStyle(styleId);
  if (style == null) {
   CTStyle ctStyle = CTStyle.Factory.newInstance();
   ctStyle.addNewName().setVal(styleId);
   ctStyle.setCustomStyle(STOnOff.TRUE);
   style = new XWPFStyle(ctStyle, styles);
   style.setType(styleType);
   style.setStyleId(styleId);
   styles.addStyle(style);
  }
  return style;
 }

 private static void applyTextAlignment(XWPFStyle style, TextAlignment value) throws Exception {
  if (style == null || value == null) return;

  Field _ctStyles = XWPFStyles.class.getDeclaredField("ctStyles");
  _ctStyles.setAccessible(true);
  CTStyles ctStyles = (CTStyles)_ctStyles.get(style.getStyles());

  for (CTStyle ctStyle : ctStyles.getStyleList()) {
   if (ctStyle.getStyleId().equals(style.getStyleId())) {
    CTPPr ppr = ctStyle.getPPr(); 
    if (ppr == null) ppr = ctStyle.addNewPPr();
    CTTextAlignment ctTextAlignment = ppr.getTextAlignment(); 
    if (ctTextAlignment == null) ctTextAlignment = ppr.addNewTextAlignment();
    if (value == TextAlignment.AUTO) {
     ctTextAlignment.setVal(STTextAlignment.AUTO);
    } else if (value == TextAlignment.BASELINE) {
     ctTextAlignment.setVal(STTextAlignment.BASELINE);
    } else if (value == TextAlignment.BOTTOM) {
     ctTextAlignment.setVal(STTextAlignment.BOTTOM);
    } else if (value == TextAlignment.CENTER) {
     ctTextAlignment.setVal(STTextAlignment.CENTER);
    } else if (value == TextAlignment.TOP) {
     ctTextAlignment.setVal(STTextAlignment.TOP);
    }
    style.setStyle(ctStyle);
   }
  }
 }

 private static void applyJustification(XWPFStyle style, ParagraphAlignment value) throws Exception {
  if (style == null || value == null) return;

  Field _ctStyles = XWPFStyles.class.getDeclaredField("ctStyles");
  _ctStyles.setAccessible(true);
  CTStyles ctStyles = (CTStyles)_ctStyles.get(style.getStyles());

  for (CTStyle ctStyle : ctStyles.getStyleList()) {
   if (ctStyle.getStyleId().equals(style.getStyleId())) {
    CTPPr ppr = ctStyle.getPPr(); if (ppr == null) ppr = ctStyle.addNewPPr();
    CTJc jc = ppr.getJc(); if (jc == null) jc = ppr.addNewJc();
    if (value == ParagraphAlignment.BOTH) {
     jc.setVal(STJc.BOTH);
    } else if (value == ParagraphAlignment.CENTER) {
     jc.setVal(STJc.CENTER);
    } else if (value == ParagraphAlignment.DISTRIBUTE) {
     jc.setVal(STJc.DISTRIBUTE);
    } else if (value == ParagraphAlignment.HIGH_KASHIDA) {
     jc.setVal(STJc.HIGH_KASHIDA);
    } else if (value == ParagraphAlignment.LEFT) {
     jc.setVal(STJc.LEFT);
    } else if (value == ParagraphAlignment.LOW_KASHIDA) {
     jc.setVal(STJc.LOW_KASHIDA);
    } else if (value == ParagraphAlignment.MEDIUM_KASHIDA) {
     jc.setVal(STJc.MEDIUM_KASHIDA);
    } else if (value == ParagraphAlignment.NUM_TAB) {
     jc.setVal(STJc.NUM_TAB);
    } else if (value == ParagraphAlignment.RIGHT) {
     jc.setVal(STJc.RIGHT);
    } else if (value == ParagraphAlignment.THAI_DISTRIBUTE) {
     jc.setVal(STJc.THAI_DISTRIBUTE);
    }
    style.setStyle(ctStyle);
   }
  }
 }

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

  XWPFDocument document = new XWPFDocument();
  XWPFParagraph paragraph = null;
  XWPFRun run = null;

  XWPFStyles styles = document.createStyles();

  XWPFStyle style = createNamedStyle(styles, STStyleType.PARAGRAPH, "TextAlignmentAUTO");
  applyTextAlignment(style, TextAlignment.AUTO);
  paragraph = document.createParagraph();
  paragraph.setStyle(style.getStyleId());
  run = paragraph.createRun();
  run.setText("TextAlignment.AUTO");
  run.setFontSize(8);
  run = paragraph.createRun();
  run.setText("Bigger text");
  run.setFontSize(30);

  style = createNamedStyle(styles, STStyleType.PARAGRAPH, "TextAlignmentBASELINECentered");
  applyJustification(style, ParagraphAlignment.CENTER);
  applyTextAlignment(style, TextAlignment.BASELINE);
  paragraph = document.createParagraph();
  paragraph.setStyle(style.getStyleId());
  run = paragraph.createRun();
  run.setText("TextAlignment.BASELINE");
  run.setFontSize(8);
  run = paragraph.createRun();
  run.setText("Bigger text");
  run.setFontSize(30);

  style = createNamedStyle(styles, STStyleType.PARAGRAPH, "TextAlignmentBOTTOMRight");
  applyJustification(style, ParagraphAlignment.RIGHT);
  applyTextAlignment(style, TextAlignment.BOTTOM);
  paragraph = document.createParagraph();
  paragraph.setStyle(style.getStyleId());
  run = paragraph.createRun();
  run.setText("TextAlignment.BOTTOM");
  run.setFontSize(8);
  run = paragraph.createRun();
  run.setText("Bigger text");
  run.setFontSize(30);

  style = createNamedStyle(styles, STStyleType.PARAGRAPH, "TextAlignmentCENTERBoth");
  applyJustification(style, ParagraphAlignment.BOTH);
  applyTextAlignment(style, TextAlignment.CENTER);
  paragraph = document.createParagraph();
  paragraph.setStyle(style.getStyleId());
  run = paragraph.createRun();
  run.setText("TextAlignment.CENTER");
  run.setFontSize(8);
  run = paragraph.createRun();
  run.setText("Bigger text");
  run.setFontSize(30);

  style = createNamedStyle(styles, STStyleType.PARAGRAPH, "TextAlignmentTOPLeft");
  applyJustification(style, ParagraphAlignment.LEFT);
  applyTextAlignment(style, TextAlignment.TOP);
  paragraph = document.createParagraph();
  paragraph.setStyle(style.getStyleId());
  run = paragraph.createRun();
  run.setText("TextAlignment.TOP");
  run.setFontSize(8);
  run = paragraph.createRun();
  run.setText("Bigger text");
  run.setFontSize(30);

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

 }
}

它产生: