[英]Apache Poi - XWPF: How to change font color, size, style of the footer Page Number in docx
[英]How to change font size in style.xml for docx using apache poi
现在,我有一个docx文件已加载到XWPFDocument
XWPFDocument doc = new XWPFDocument(InputStream)
我可以通过执行以下操作查看存储在style.xml中的不同样式的当前字体大小
for (CTStyle ctstyle : doc.getStyle().getStyleList())
{
if (ctstyle.isSetRPr())
{
if (ctstyle.getRPr().isSetSz())
{
CTHpsMeasure size = ctstyle.getRPr().getSz();
System.out.println(size.getVal());
}
}
}
我想使用poi更新字体大小,所以我尝试了
for (CTStyle ctstyle : doc.getStyle().getStyleList())
{
if (ctstyle.isSetRPr())
{
if (ctstyle.getRPr().isSetSz())
{
CTHpsMeasure size = ctstyle.getRPr().getSz();
size.setVal(BigInteger.valueOf(12));
ctstyle.getRPr().setSz(size);
}
}
}
但是,完成上述代码后,如果我使用第一段代码检查文档( XWPFDocument doc
对象)的字体大小,则字体大小仍然是原始值,而不是我打算设置的12。
关于如何解决此问题有什么建议吗?
XWPFDocument.getStyle获取org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles
但不获取文档部分/word/styles.xml
。 因此, CTStyles
将不会提交对此CTStyles
的XWPFDocument
。
XWPFDocument.getStyles获取XWPFStyles
,它扩展POIXMLDocumentPart
并覆盖commit
方法。 因此,在XWPFStyles
将提交对XWPFDocument
。
不幸的是,没有一种方法可以从XWPFStyles
获取所有单一样式。 有一个private CTStyles ctStyles
字段private CTStyles ctStyles
,它也可以在protected void commit()
,但不能公共访问。 所以我用反射来得到它。
从先前的问题中,我知道您在解析*.docx
文件时遇到问题,该文件的字体大小错误地设置为doubles <w:sz w:val="24.0"/>
,这肯定是错误的,并导致size.getVal()
引发org.apache.xmlbeans.impl.values.XmlValueOutOfRangeException: Invalid integer value: 24.0
。
ctstyle.getRPr().getSz()
运行也不会崩溃,并且<w:sz w:val="24.0"/>
。 在这里,您可以获得实际的实际尺寸。 为此,请直接解析CTHpsMeasure size
的XML,然后将从那里截取的值设置为BigInteger
。 BigDecimal
为此提供了一种方法。
另外,您还应该获取并纠正ctstyle.getRPr().getSzCs()
,它是所使用的“复杂脚本”字体的大小,也可能是错误的。
以下对我有用,然后所有具有字体大小的样式都设置了整数字体大小。 例如, <w:sz w:val="24.0"/><w:szCs w:val="24.0"/>
是<w:sz w:val="24"/><w:szCs w:val="24"/>
然后是<w:sz w:val="24"/><w:szCs w:val="24"/>
。 请注意,此处的测量单位是半点24半点= 12磅。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyle;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTStyles;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.lang.reflect.Field;
public class WordChangeStyles {
public static void main(String[] args) throws Exception {
XWPFDocument document = new XWPFDocument(new FileInputStream("WordUsingStyles.docx"));
XWPFStyles styles = document.getStyles(); //XWPFStyles extends POIXMLDocumentPart and overwrites commit method
Field _ctStyles = XWPFStyles.class.getDeclaredField("ctStyles");
_ctStyles.setAccessible(true);
CTStyles ctStyles = (CTStyles)_ctStyles.get(styles);
CTHpsMeasure size;
String sz;
for (CTStyle ctstyle : ctStyles.getStyleList()) {
if (ctstyle.isSetRPr()) {
if (ctstyle.getRPr().isSetSz()) {
size = ctstyle.getRPr().getSz();
if (size != null) {
sz = size.selectAttribute("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "val").newCursor().getTextValue();
System.out.println(sz); //here you could get the real meant size out of
size.setVal(new BigDecimal(sz).toBigInteger()); //sets the sz got from size truncated to BigInteger
ctstyle.getRPr().setSz(size); //after that the font size should be integer
System.out.println(ctstyle.getRPr().getSz().getVal());
}
size = ctstyle.getRPr().getSzCs();
if (size != null) {
sz = size.selectAttribute("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "val").newCursor().getTextValue();
System.out.println(sz); //here you could get the real meant size out of
size.setVal(new BigDecimal(sz).toBigInteger()); //sets the sz got from size truncated to BigInteger
ctstyle.getRPr().setSzCs(size); //after that the font size should be integer
System.out.println(ctstyle.getRPr().getSzCs().getVal());
}
}
}
}
document.write(new FileOutputStream("WordUsingStyles.docx")); //while writing out XWPFStyles will be committed
document.close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.